main #10
@@ -5,6 +5,8 @@ import com.example.nto.exception.BookingConflictException;
|
||||
import com.example.nto.exception.EmployeeNotFoundException;
|
||||
import com.example.nto.exception.InvalidBookingException;
|
||||
import com.example.nto.service.BookingService;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -55,42 +57,22 @@ public class BookingController {
|
||||
@RequestBody Booking.CreateBookingRequest request
|
||||
) {
|
||||
try {
|
||||
Booking booking = bookingService.createBooking(
|
||||
code,
|
||||
request.getDate(),
|
||||
request.getPlaceId()
|
||||
);
|
||||
|
||||
// Возвращаем только статус 201 без тела
|
||||
Booking booking = bookingService.createBooking(code, request.getDate(), request.getPlaceId());
|
||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||
|
||||
} catch (EmployeeNotFoundException e) {
|
||||
// 401 - кода не существует
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||
|
||||
} catch (BookingConflictException e) {
|
||||
// 409 - конфликт (уже забронировано)
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT).build();
|
||||
|
||||
} catch (InvalidBookingException e) {
|
||||
// 400 - невалидные данные
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||
|
||||
} catch (Exception e) {
|
||||
// 500 - внутренняя ошибка сервера
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
||||
}
|
||||
}
|
||||
|
||||
private Booking.BookingResponse convertToResponse(Booking booking) {
|
||||
Booking.BookingResponse response = new Booking.BookingResponse();
|
||||
response.setId(booking.getId());
|
||||
response.setDate(booking.getDate());
|
||||
response.setPlaceId(booking.getPlace().getId());
|
||||
response.setPlaceName(booking.getPlace().getPlace());
|
||||
response.setEmployeeId(booking.getEmployee().getId());
|
||||
response.setEmployeeName(booking.getEmployee().getName());
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -47,13 +47,13 @@ public class Booking {
|
||||
private String place;
|
||||
}
|
||||
@Data
|
||||
public class CreateBookingRequest {
|
||||
public static class CreateBookingRequest {
|
||||
@JsonFormat(pattern = "yyyy-MM-dd")
|
||||
private LocalDate date;
|
||||
private long placeId;
|
||||
}
|
||||
@Data
|
||||
public class ErrorResponse {
|
||||
public static class ErrorResponse {
|
||||
private String error;
|
||||
private String message;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user