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