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
No related branches found
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 {
@PostMapping
public Booking createBooking(@RequestBody Booking booking) {
User user = userRepository.findById(booking.getUserId().getId()).orElseThrow();
Room room = roomRepository.findById(booking.getRoomId().getId()).orElseThrow();
User user = userRepository.findById(booking.getUserId().getId())
.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.setRoomId(room);
return bookingRepository.save(booking);
......@@ -46,4 +62,6 @@ public class BookingController {
public void deleteBooking(@PathVariable Integer id) {
bookingRepository.deleteById(id);
}
}
......@@ -2,7 +2,16 @@
package com.uva.roomBooking.Repositories;
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.Query;
import org.springframework.data.repository.query.Param;
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.
Finish editing this message first!
Please register or to comment