This commit is contained in:
@@ -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<String> checkAuth(
|
||||
@PathVariable String code) {
|
||||
if (code==null){
|
||||
public ResponseEntity<String> 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<Booking> bookings = bookingRepository.findByEmployeeId(employee.getId());
|
||||
Map<String, List<Map<String, Object>>> 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<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) {
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user