forked from Olympic/NTO-2025-Backend-TeamTask
вторая попытка
This commit is contained in:
@@ -38,9 +38,6 @@ public class BookingController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/info")
|
@GetMapping("/info")
|
||||||
public ResponseEntity<Map<String, Object>> info(@PathVariable String code) {
|
public ResponseEntity<Map<String, Object>> info(@PathVariable String code) {
|
||||||
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
||||||
@@ -52,7 +49,7 @@ public class BookingController {
|
|||||||
response.put("name", employee.getName());
|
response.put("name", employee.getName());
|
||||||
response.put("photoUrl", employee.getPhotoUrl());
|
response.put("photoUrl", employee.getPhotoUrl());
|
||||||
|
|
||||||
Map<String, List<Map<String, Object>>> bookingsMap = new HashMap<>();
|
Map<String, Map<String, Object>> bookingsMap = new HashMap<>();
|
||||||
List<Booking> bookingList = bookingService.getBookingsByEmployee(employee);
|
List<Booking> bookingList = bookingService.getBookingsByEmployee(employee);
|
||||||
|
|
||||||
for (Booking b : bookingList) {
|
for (Booking b : bookingList) {
|
||||||
@@ -61,7 +58,7 @@ public class BookingController {
|
|||||||
bookingData.put("id", b.getId());
|
bookingData.put("id", b.getId());
|
||||||
bookingData.put("place", b.getPlace().getPlace());
|
bookingData.put("place", b.getPlace().getPlace());
|
||||||
|
|
||||||
bookingsMap.computeIfAbsent(dateKey, k -> new ArrayList<>()).add(bookingData);
|
bookingsMap.put(dateKey, bookingData);
|
||||||
}
|
}
|
||||||
|
|
||||||
response.put("booking", bookingsMap);
|
response.put("booking", bookingsMap);
|
||||||
@@ -102,7 +99,6 @@ public class BookingController {
|
|||||||
return ResponseEntity.ok(result);
|
return ResponseEntity.ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("/book")
|
@PostMapping("/book")
|
||||||
public ResponseEntity<Void> book(@PathVariable String code, @RequestBody Map<String, String> body) {
|
public ResponseEntity<Void> book(@PathVariable String code, @RequestBody Map<String, String> body) {
|
||||||
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
||||||
@@ -111,7 +107,12 @@ public class BookingController {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
String dateStr = body.get("date");
|
String dateStr = body.get("date");
|
||||||
String placeIdStr = body.get("placeID");
|
|
||||||
|
// поддерживаем все варианты ключей
|
||||||
|
String placeIdStr = body.getOrDefault("placeID",
|
||||||
|
body.getOrDefault("placeId",
|
||||||
|
body.getOrDefault("place", null)));
|
||||||
|
|
||||||
if (dateStr == null || placeIdStr == null) {
|
if (dateStr == null || placeIdStr == null) {
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||||
}
|
}
|
||||||
@@ -120,12 +121,17 @@ public class BookingController {
|
|||||||
Long placeId = Long.parseLong(placeIdStr);
|
Long placeId = Long.parseLong(placeIdStr);
|
||||||
Place place = placeService.getPlaceById(placeId);
|
Place place = placeService.getPlaceById(placeId);
|
||||||
|
|
||||||
|
if (bookingService.getBookingByEmployeeAndDate(employee, date).isPresent()) {
|
||||||
|
return ResponseEntity.status(HttpStatus.CONFLICT).build();
|
||||||
|
}
|
||||||
|
|
||||||
if (bookingService.getBookingByDateAndPlace(date, place).isPresent()) {
|
if (bookingService.getBookingByDateAndPlace(date, place).isPresent()) {
|
||||||
return ResponseEntity.status(HttpStatus.CONFLICT).build();
|
return ResponseEntity.status(HttpStatus.CONFLICT).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
bookingService.createBooking(employee, place, date);
|
bookingService.createBooking(employee, place, date);
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,4 +13,6 @@ public interface BookingRepository extends JpaRepository<Booking, Long> {
|
|||||||
List<Booking> findByEmployee(Employee employee);
|
List<Booking> findByEmployee(Employee employee);
|
||||||
List<Booking> findByDate(LocalDate date);
|
List<Booking> findByDate(LocalDate date);
|
||||||
Optional<Booking> findByDateAndPlace(LocalDate date, Place place);
|
Optional<Booking> findByDateAndPlace(LocalDate date, Place place);
|
||||||
|
|
||||||
|
Optional<Booking> findByEmployeeAndDate(Employee employee, LocalDate date);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,5 +12,8 @@ public interface BookingService {
|
|||||||
List<Booking> getBookingsByEmployee(Employee employee);
|
List<Booking> getBookingsByEmployee(Employee employee);
|
||||||
List<Booking> getBookingsByDate(LocalDate date);
|
List<Booking> getBookingsByDate(LocalDate date);
|
||||||
Optional<Booking> getBookingByDateAndPlace(LocalDate date, Place place);
|
Optional<Booking> getBookingByDateAndPlace(LocalDate date, Place place);
|
||||||
|
|
||||||
|
Optional<Booking> getBookingByEmployeeAndDate(Employee employee, LocalDate date);
|
||||||
|
|
||||||
Booking createBooking(Employee employee, Place place, LocalDate date);
|
Booking createBooking(Employee employee, Place place, LocalDate date);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,11 @@ public class BookingServiceImpl implements BookingService {
|
|||||||
return bookingRepository.findByDateAndPlace(date, place);
|
return bookingRepository.findByDateAndPlace(date, place);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Booking> getBookingByEmployeeAndDate(Employee employee, LocalDate date) {
|
||||||
|
return bookingRepository.findByEmployeeAndDate(employee, date);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Booking createBooking(Employee employee, Place place, LocalDate date) {
|
public Booking createBooking(Employee employee, Place place, LocalDate date) {
|
||||||
Booking booking = new Booking();
|
Booking booking = new Booking();
|
||||||
|
|||||||
Reference in New Issue
Block a user