Files
NTO-2025-Backend-TeamTask/src/main/java/com/example/nto/controller/EmployeeController.java
Bhumi Shah 662b109709
Some checks failed
Android Test / validate-and-test (pull_request) Has been cancelled
112
2025-12-06 08:53:56 +03:00

67 lines
2.8 KiB
Java

package com.example.nto.controller;
import com.example.nto.entity.Employee;
import com.example.nto.excepation.EmployeeNotFoundException;
import com.example.nto.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
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: ДОРАБОТАТЬ в рамках задания
* =================================
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
@RestController
@RequestMapping("/api")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/{code}/auth")
public ResponseEntity<Void> checkAuth(
@PathVariable String code) {
if (code==null){
throw new IllegalArgumentException("Employee code is required");
}
try {
employeeService.getEmployeeByCode(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 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(HttpStatus.BAD_REQUEST).body(e.getMessage());
} catch (EmployeeNotFoundException e) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error");
}
}
}