package com.example.nto.controller; import com.example.nto.entity.Booking; import com.example.nto.entity.Employee; import com.example.nto.entity.Place; import com.example.nto.service.BookingService; import com.example.nto.service.EmployeeService; import com.example.nto.service.PlaceService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.time.LocalDate; import java.util.*; import java.util.stream.Collectors; @RestController @RequestMapping("/api/{code}") public class BookingController { private final EmployeeService employeeService; private final BookingService bookingService; private final PlaceService placeService; public BookingController(EmployeeService employeeService, BookingService bookingService, PlaceService placeService) { this.employeeService = employeeService; this.bookingService = bookingService; this.placeService = placeService; } @GetMapping("/auth") public ResponseEntity auth(@PathVariable String code) { Optional employeeOpt = employeeService.findByCode(code); if (employeeOpt.isPresent()) { return ResponseEntity.ok().build(); } else { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); } } @GetMapping("/info") public ResponseEntity> info(@PathVariable String code) { Optional employeeOpt = employeeService.findByCode(code); if (employeeOpt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); Employee employee = employeeOpt.get(); Map response = new HashMap<>(); response.put("name", employee.getName()); response.put("photoUrl", employee.getPhotoUrl()); Map> bookingsMap = new HashMap<>(); List bookingList = bookingService.getBookingsByEmployee(employee); for (Booking b : bookingList) { String dateKey = b.getDate().toString(); Map bookingData = new HashMap<>(); bookingData.put("id", b.getId()); bookingData.put("place", b.getPlace().getPlace()); bookingsMap.put(dateKey, bookingData); } response.put("booking", bookingsMap); return ResponseEntity.ok(response); } @GetMapping("/booking") public ResponseEntity>>> availableBooking(@PathVariable String code) { Optional employeeOpt = employeeService.findByCode(code); if (employeeOpt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); Map>> result = new LinkedHashMap<>(); LocalDate today = LocalDate.now(); List allPlaces = placeService.getAllPlaces(); for (int i = 0; i < 4; i++) { LocalDate date = today.plusDays(i); List bookings = bookingService.getBookingsByDate(date); Set bookedPlaceIds = bookings.stream() .map(b -> b.getPlace().getId()) .collect(Collectors.toSet()); List> available = allPlaces.stream() .filter(p -> !bookedPlaceIds.contains(p.getId())) .map(p -> { Map m = new HashMap<>(); m.put("id", p.getId()); m.put("place", p.getPlace()); return m; }) .collect(Collectors.toList()); result.put(date.toString(), available); } return ResponseEntity.ok(result); } @PostMapping("/book") public ResponseEntity book(@PathVariable String code, @RequestBody Map body) { Optional employeeOpt = employeeService.findByCode(code); if (employeeOpt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); Employee employee = employeeOpt.get(); try { String dateStr = body.get("date"); // поддерживаем все варианты ключей String placeIdStr = body.getOrDefault("placeID", body.getOrDefault("placeId", body.getOrDefault("place", null))); if (dateStr == null || placeIdStr == null) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); } LocalDate date = LocalDate.parse(dateStr); 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(); } } }