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 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 response = new LinkedHashMap<>(); response.put("name", employee.getName()); response.put("photoUrl", employee.getPhotoUrl()); Map 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"); } } }