forked from Olympic/NTO-2025-Backend-TeamTask
87 lines
3.1 KiB
Java
87 lines
3.1 KiB
Java
package com.example.nto.controller;
|
|
|
|
import com.example.nto.dto.BookingRequestDto;
|
|
import com.example.nto.dto.FreePlaceDto;
|
|
import com.example.nto.entity.Place;
|
|
import com.example.nto.repository.EmployeeRepository;
|
|
import com.example.nto.service.BookingService;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.time.LocalDate;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/{code}")
|
|
public class BookingController {
|
|
|
|
private final BookingService service;
|
|
private final EmployeeRepository employeeRepository;
|
|
|
|
public BookingController(BookingService service, EmployeeRepository employeeRepository) {
|
|
this.service = service;
|
|
this.employeeRepository = employeeRepository;
|
|
}
|
|
|
|
private boolean isValidCode(String code) {
|
|
return code != null && code.matches("\\d+");
|
|
}
|
|
|
|
@GetMapping("/booking")
|
|
public ResponseEntity<?> getFree(@PathVariable String code) {
|
|
if (!isValidCode(code)) {
|
|
return ResponseEntity.status(400).body("Bad request");
|
|
}
|
|
|
|
if (employeeRepository.findByCode(code).isEmpty()) {
|
|
return ResponseEntity.status(401).body("Unauthorized");
|
|
}
|
|
|
|
Map<LocalDate, List<Place>> free = service.getAvailablePlacesForRange(LocalDate.now(), 4);
|
|
|
|
Map<String, List<FreePlaceDto>> dto = free.entrySet().stream()
|
|
.collect(Collectors.toMap(
|
|
e -> e.getKey().toString(),
|
|
e -> e.getValue().stream()
|
|
.map(p -> new FreePlaceDto(p.getId(), p.getPlace()))
|
|
.collect(Collectors.toList())
|
|
));
|
|
|
|
return ResponseEntity.ok(dto);
|
|
}
|
|
|
|
@PostMapping("/book")
|
|
public ResponseEntity<?> book(@PathVariable String code,
|
|
@RequestBody BookingRequestDto dto) {
|
|
if (!isValidCode(code)) {
|
|
return ResponseEntity.status(400).body("Bad request");
|
|
}
|
|
|
|
if (employeeRepository.findByCode(code).isEmpty()) {
|
|
return ResponseEntity.status(401).body("Unauthorized");
|
|
}
|
|
|
|
try {
|
|
service.createBooking(code, LocalDate.parse(dto.getDate()), dto.getPlaceId());
|
|
return ResponseEntity.status(201).body("Booking created");
|
|
} catch (IllegalArgumentException ex) {
|
|
String msg = ex.getMessage();
|
|
if ("Place not found".equals(msg)) {
|
|
return ResponseEntity.status(400).body("Bad request");
|
|
}
|
|
return ResponseEntity.status(400).body("Bad request");
|
|
} catch (IllegalStateException ex) {
|
|
return ResponseEntity.status(409).body("Conflict");
|
|
} catch (Exception ex) {
|
|
return ResponseEntity.status(400).body("Bad request");
|
|
}
|
|
}
|
|
|
|
@ExceptionHandler({org.springframework.http.converter.HttpMessageNotReadableException.class})
|
|
public ResponseEntity<String> handleBadRequest() {
|
|
return ResponseEntity.status(400).body("Bad request");
|
|
}
|
|
}
|