Files
NTO-2025-Backend-TeamTask/src/main/java/com/example/nto/controller/BookingController.java
2025-12-04 17:20:04 +06:00

65 lines
2.5 KiB
Java

package com.example.nto.controller;
import com.example.nto.DTO.BookingDTO;
import com.example.nto.DTO.BookingRequest;
import com.example.nto.DTO.PlaceDTO;
import com.example.nto.entity.Booking;
import com.example.nto.entity.Place;
import com.example.nto.exceptions.BookingAlreadyExistsException;
import com.example.nto.exceptions.EmployeeNotFoundException;
import com.example.nto.service.BookingService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.RequiredArgsConstructor;
import org.springframework.cglib.core.Local;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* TODO: ДОРАБОТАТЬ в рамках задания
* =================================
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
@RestController
@RequestMapping("/api/{code}")
@RequiredArgsConstructor
public class BookingController {
private final BookingService bookingService;
@GetMapping("/booking")
public ResponseEntity<Map<LocalDate, List<Place>>> getBooking(@PathVariable String code){
try {
Map<LocalDate, List<Place>> availablePlaces = bookingService.freeBookingPlace(code);
return ResponseEntity.ok(availablePlaces);
} catch (EmployeeNotFoundException e) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
}
}
@PostMapping("/book")
public ResponseEntity<?> createBooking(@PathVariable String code, @RequestBody BookingRequest request){
try {
LocalDate date = request.getDate();
Long placeId = request.getPlaceId();
BookingDTO bookingDTO = bookingService.createBooking(code, date, placeId);
return ResponseEntity.status(HttpStatus.CREATED).build();
} catch (EmployeeNotFoundException e) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
} catch (BookingAlreadyExistsException e) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build();
}
}
}