From 65d3dd56cd53d04839a96c8648f2968967df92a0 Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Sun, 7 Dec 2025 00:48:21 +0300 Subject: [PATCH] 1234 --- .../nto/controller/EmployeeController.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index a976a6c..be274b2 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -1,6 +1,8 @@ package com.example.nto.controller; +import com.example.nto.entity.Booking; import com.example.nto.entity.Employee; import com.example.nto.excepation.EmployeeNotFoundException; +import com.example.nto.repository.BookingRepository; import com.example.nto.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -8,6 +10,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -22,10 +25,13 @@ import java.util.stream.Collectors; public class EmployeeController { @Autowired private EmployeeService employeeService; + @Autowired + private BookingRepository bookingRepository; + @GetMapping("/{code}/auth") - public ResponseEntity checkAuth( - @PathVariable String code) { - if (code==null){ + public ResponseEntity checkAuth( + @PathVariable String code) { + if (code == null) { throw new IllegalArgumentException("Employee code is required"); } try { @@ -40,20 +46,16 @@ public class EmployeeController { @GetMapping("/{code}/info") public ResponseEntity getEmployeeInfo(@PathVariable String code) { try { + if (code == null || code.trim().isEmpty()) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body("Employee code cannot be null or empty"); + } Employee employee = employeeService.getEmployeeByCode(code); + List bookings = bookingRepository.findByEmployeeId(employee.getId()); + Map>> bookingMap = bookings.stream().collect(Collectors.groupingBy(b -> b.getDate().toString(),LinkedHashMap::new,Collectors.mapping(b -> Map.of("id", b.getId(),"place", b.getPlace().getPlace()), Collectors.toList()))); 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) { @@ -61,7 +63,8 @@ public class EmployeeController { } catch (EmployeeNotFoundException e) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage()); } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error"); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Unexpected error"); } } } \ No newline at end of file