This commit is contained in:
@@ -7,6 +7,10 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
@@ -18,31 +22,31 @@ import org.springframework.web.bind.annotation.*;
|
||||
public class EmployeeController {
|
||||
@Autowired
|
||||
private EmployeeService employeeService;
|
||||
@GetMapping("/{code}/auth")
|
||||
public ResponseEntity<Void> checkAuth(
|
||||
@PathVariable String code,
|
||||
@RequestHeader(value = "Authorization", required = false) String authHeader) {
|
||||
if (authHeader == null || !authHeader.equals("Bearer valid-token")) {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||
}
|
||||
try {
|
||||
employeeService. getEmployeeWithBookings(code);
|
||||
return ResponseEntity.ok().build();
|
||||
} catch (IllegalArgumentException e) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||
} catch (EmployeeNotFoundException e) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
|
||||
}
|
||||
}
|
||||
@GetMapping("/{code}/info")
|
||||
public ResponseEntity<?> getEmployeeInfo(@PathVariable String code) {
|
||||
try {
|
||||
Employee response = employeeService.getEmployeeWithBookings(code);
|
||||
Employee employee = employeeService.getEmployeeByCode(code);
|
||||
Map<String, Object> response = new LinkedHashMap<>();
|
||||
response.put("name", employee.getName());
|
||||
response.put("photoUrl", employee.getPhotoUrl());
|
||||
Map<String, Object> bookingMap = employee.getBookings().stream()
|
||||
.collect(Collectors.toMap(
|
||||
b -> b.getDate().toString(),
|
||||
b -> Map.of(
|
||||
"id", b.getId(),
|
||||
"place", b.getPlace().getPlace()
|
||||
),
|
||||
(oldValue, newValue) -> newValue,
|
||||
LinkedHashMap::new
|
||||
));
|
||||
response.put("booking", bookingMap);
|
||||
return ResponseEntity.ok(response);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return ResponseEntity.status(400).body(e.getMessage());
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
|
||||
} catch (EmployeeNotFoundException e) {
|
||||
return ResponseEntity.status(404).body(e.getMessage());
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user