main #8
@@ -1,6 +1,8 @@
|
|||||||
package com.example.nto.controller;
|
package com.example.nto.controller;
|
||||||
|
import com.example.nto.entity.Booking;
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.excepation.EmployeeNotFoundException;
|
import com.example.nto.excepation.EmployeeNotFoundException;
|
||||||
|
import com.example.nto.repository.BookingRepository;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
@@ -8,6 +10,7 @@ import org.springframework.http.ResponseEntity;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -22,10 +25,13 @@ import java.util.stream.Collectors;
|
|||||||
public class EmployeeController {
|
public class EmployeeController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private EmployeeService employeeService;
|
private EmployeeService employeeService;
|
||||||
|
@Autowired
|
||||||
|
private BookingRepository bookingRepository;
|
||||||
|
|
||||||
@GetMapping("/{code}/auth")
|
@GetMapping("/{code}/auth")
|
||||||
public ResponseEntity<String> checkAuth(
|
public ResponseEntity<String> checkAuth(
|
||||||
@PathVariable String code) {
|
@PathVariable String code) {
|
||||||
if (code==null){
|
if (code == null) {
|
||||||
throw new IllegalArgumentException("Employee code is required");
|
throw new IllegalArgumentException("Employee code is required");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
@@ -40,20 +46,16 @@ public class EmployeeController {
|
|||||||
@GetMapping("/{code}/info")
|
@GetMapping("/{code}/info")
|
||||||
public ResponseEntity<?> getEmployeeInfo(@PathVariable String code) {
|
public ResponseEntity<?> getEmployeeInfo(@PathVariable String code) {
|
||||||
try {
|
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);
|
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<>();
|
Map<String, Object> response = new LinkedHashMap<>();
|
||||||
response.put("name", employee.getName());
|
response.put("name", employee.getName());
|
||||||
response.put("photoUrl", employee.getPhotoUrl());
|
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);
|
response.put("booking", bookingMap);
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
@@ -61,7 +63,8 @@ public class EmployeeController {
|
|||||||
} catch (EmployeeNotFoundException e) {
|
} catch (EmployeeNotFoundException e) {
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
|
||||||
} catch (Exception e) {
|
} 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