Booking creation added

This commit is contained in:
indx0
2025-11-21 21:59:30 +03:00
parent 02a8c05142
commit b48aec3589
8 changed files with 63 additions and 8 deletions

View File

@@ -3,7 +3,6 @@ package com.example.nto.controller;
import com.example.nto.dto.CreateBookingDTO;
import com.example.nto.entity.Place;
import com.example.nto.service.BookingService;
import com.example.nto.service.EmployeeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
@@ -34,7 +33,7 @@ public class BookingController {
@PostMapping("/{code}/book")
public ResponseEntity<Void> createBooking(@RequestBody CreateBookingDTO createBookingDTO, @PathVariable String code) {
bookingService.createBooking(createBookingDTO);
return ResponseEntity.ok().build();
bookingService.createBooking(createBookingDTO, code);
return ResponseEntity.created(null).build();
}
}

View File

@@ -0,0 +1,7 @@
package com.example.nto.exception;
public class BookingExistsException extends RuntimeException {
public BookingExistsException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,7 @@
package com.example.nto.exception;
public class PlaceNotFoundException extends RuntimeException {
public PlaceNotFoundException(String message) {
super(message);
}
}

View File

@@ -1,6 +1,8 @@
package com.example.nto.exception.handler;
import com.example.nto.exception.BookingExistsException;
import com.example.nto.exception.CodeNotFoundException;
import com.example.nto.exception.PlaceNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
@@ -9,7 +11,17 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(CodeNotFoundException.class)
public ResponseEntity<Void> handleCodeNotFoundException(CodeNotFoundException e) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
public ResponseEntity<String> handleCodeNotFoundException(CodeNotFoundException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.UNAUTHORIZED);
}
@ExceptionHandler(PlaceNotFoundException.class)
public ResponseEntity<String> handlePlaceNotFoundException(PlaceNotFoundException e) {
return new ResponseEntity<>(e.getMessage(),HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(BookingExistsException.class)
public ResponseEntity<String> handleBookingExistsException(BookingExistsException e) {
return new ResponseEntity<>(e.getMessage(),HttpStatus.CONFLICT);
}
}

View File

@@ -14,4 +14,6 @@ import java.util.List;
*/
public interface BookingRepository extends JpaRepository<Booking, Long> {
List<Booking> findByDateBetween(LocalDate startDate, LocalDate endDate);
boolean existsByDateAndPlaceId(LocalDate date, Long placeId);
boolean existsByDateAndEmployeeId(LocalDate date, Long employeeId);
}

View File

@@ -10,4 +10,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
public interface PlaceRepository extends JpaRepository<Place, Long> {
}

View File

@@ -14,5 +14,5 @@ import java.util.Map;
*/
public interface BookingService {
Map<LocalDate, List<Place>> getAvailablePlaces();
void createBooking(CreateBookingDTO createBookingDTO);
void createBooking(CreateBookingDTO createBookingDTO, String code);
}

View File

@@ -1,8 +1,13 @@
package com.example.nto.service.impl;
import com.example.nto.dto.CreateBookingDTO;
import com.example.nto.entity.Booking;
import com.example.nto.entity.Place;
import com.example.nto.exception.BookingExistsException;
import com.example.nto.exception.PlaceNotFoundException;
import com.example.nto.repository.BookingRepository;
import com.example.nto.repository.EmployeeRepository;
import com.example.nto.repository.PlaceRepository;
import com.example.nto.service.BookingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -21,6 +26,12 @@ import java.util.Map;
public class BookingServiceImpl implements BookingService {
@Autowired
BookingRepository bookingRepository;
@Autowired
PlaceRepository placeRepository;
@Autowired
EmployeeServiceImpl employeeService;
@Autowired
EmployeeRepository employeeRepository;
@Override
public Map<LocalDate, List<Place>> getAvailablePlaces() {
@@ -28,7 +39,23 @@ public class BookingServiceImpl implements BookingService {
}
@Override
public void createBooking(CreateBookingDTO createBookingDTO) {
public void createBooking(CreateBookingDTO createBookingDTO, String code) {
employeeService.codeExists(code);
if(!placeRepository.existsById(createBookingDTO.getPlaceID())) {
throw new PlaceNotFoundException("Place Does Not Exist");
}
if(bookingRepository.existsByDateAndPlaceId(createBookingDTO.getDate(), createBookingDTO.getPlaceID())) {
throw new BookingExistsException("Booking Already Exists");
}
var employee = employeeRepository.findByCode(code);
if(bookingRepository.existsByDateAndEmployeeId(createBookingDTO.getDate(), employee.getId())) {
throw new BookingExistsException("Employee Already Has A Booking For This Date");
}
Booking booking = Booking.builder()
.date(createBookingDTO.getDate())
.place(placeRepository.findById(createBookingDTO.getPlaceID()).get())
.employee(employee)
.build();
bookingRepository.save(booking);
}
}