Skip to content
Snippets Groups Projects
Commit e77945e7 authored by hugcubi's avatar hugcubi
Browse files

Añadidas comprobaciones al hacer la reserva

parent 1da8d955
Branches
No related tags found
2 merge requests!10Add ts types and json mocks, remove poblate scripts and fix the cascade...,!9Dev booking request
...@@ -35,8 +35,24 @@ public class BookingController { ...@@ -35,8 +35,24 @@ public class BookingController {
@PostMapping @PostMapping
public Booking createBooking(@RequestBody Booking booking) { public Booking createBooking(@RequestBody Booking booking) {
User user = userRepository.findById(booking.getUserId().getId()).orElseThrow(); User user = userRepository.findById(booking.getUserId().getId())
Room room = roomRepository.findById(booking.getRoomId().getId()).orElseThrow(); .orElseThrow(() -> new RuntimeException("User not found"));
Room room = roomRepository.findById(booking.getRoomId().getId())
.orElseThrow(() -> new RuntimeException("Room not found"));
// Validar el tipo de habitación
if (!room.getType().equals(booking.getRoomId().getType())) {
throw new RuntimeException("Room type does not match the requested type");
}
// Verificar disponibilidad
List<Booking> existingBookings = bookingRepository.findByRoomIdAndDateRange(
room.getId(), booking.getStartDate(), booking.getEndDate());
if (!existingBookings.isEmpty()) {
throw new RuntimeException("Room is not available for the selected dates");
}
booking.setUserId(user); booking.setUserId(user);
booking.setRoomId(room); booking.setRoomId(room);
return bookingRepository.save(booking); return bookingRepository.save(booking);
...@@ -46,4 +62,6 @@ public class BookingController { ...@@ -46,4 +62,6 @@ public class BookingController {
public void deleteBooking(@PathVariable Integer id) { public void deleteBooking(@PathVariable Integer id) {
bookingRepository.deleteById(id); bookingRepository.deleteById(id);
} }
} }
...@@ -2,7 +2,16 @@ ...@@ -2,7 +2,16 @@
package com.uva.roomBooking.Repositories; package com.uva.roomBooking.Repositories;
import com.uva.roomBooking.Models.Booking; import com.uva.roomBooking.Models.Booking;
import java.time.LocalDate;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface BookingRepository extends JpaRepository<Booking, Integer> { public interface BookingRepository extends JpaRepository<Booking, Integer> {
@Query("SELECT b FROM Booking b WHERE b.room.id = :roomId AND b.startDate < :endDate AND b.endDate > :startDate")
List<Booking> findByRoomIdAndDateRange(@Param("roomId") int roomId, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment