вторая попытка

This commit is contained in:
2025-11-29 19:38:16 +03:00
parent d5564b63eb
commit 0fc0678392
4 changed files with 23 additions and 7 deletions

View File

@@ -38,9 +38,6 @@ public class BookingController {
}
}
@GetMapping("/info")
public ResponseEntity<Map<String, Object>> info(@PathVariable String code) {
Optional<Employee> employeeOpt = employeeService.findByCode(code);
@@ -52,7 +49,7 @@ public class BookingController {
response.put("name", employee.getName());
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);
for (Booking b : bookingList) {
@@ -61,7 +58,7 @@ public class BookingController {
bookingData.put("id", b.getId());
bookingData.put("place", b.getPlace().getPlace());
bookingsMap.computeIfAbsent(dateKey, k -> new ArrayList<>()).add(bookingData);
bookingsMap.put(dateKey, bookingData);
}
response.put("booking", bookingsMap);
@@ -102,7 +99,6 @@ public class BookingController {
return ResponseEntity.ok(result);
}
@PostMapping("/book")
public ResponseEntity<Void> book(@PathVariable String code, @RequestBody Map<String, String> body) {
Optional<Employee> employeeOpt = employeeService.findByCode(code);
@@ -111,7 +107,12 @@ public class BookingController {
try {
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) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
@@ -120,12 +121,17 @@ public class BookingController {
Long placeId = Long.parseLong(placeIdStr);
Place place = placeService.getPlaceById(placeId);
if (bookingService.getBookingByEmployeeAndDate(employee, date).isPresent()) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
}
if (bookingService.getBookingByDateAndPlace(date, place).isPresent()) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
}
bookingService.createBooking(employee, place, date);
return ResponseEntity.status(HttpStatus.CREATED).build();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}

View File

@@ -13,4 +13,6 @@ public interface BookingRepository extends JpaRepository<Booking, Long> {
List<Booking> findByEmployee(Employee employee);
List<Booking> findByDate(LocalDate date);
Optional<Booking> findByDateAndPlace(LocalDate date, Place place);
Optional<Booking> findByEmployeeAndDate(Employee employee, LocalDate date);
}

View File

@@ -12,5 +12,8 @@ public interface BookingService {
List<Booking> getBookingsByEmployee(Employee employee);
List<Booking> getBookingsByDate(LocalDate date);
Optional<Booking> getBookingByDateAndPlace(LocalDate date, Place place);
Optional<Booking> getBookingByEmployeeAndDate(Employee employee, LocalDate date);
Booking createBooking(Employee employee, Place place, LocalDate date);
}

View File

@@ -35,6 +35,11 @@ public class BookingServiceImpl implements BookingService {
return bookingRepository.findByDateAndPlace(date, place);
}
@Override
public Optional<Booking> getBookingByEmployeeAndDate(Employee employee, LocalDate date) {
return bookingRepository.findByEmployeeAndDate(employee, date);
}
@Override
public Booking createBooking(Employee employee, Place place, LocalDate date) {
Booking booking = new Booking();