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

Arreglado error

parent 39eba64a
No related branches found
No related tags found
2 merge requests!10Add ts types and json mocks, remove poblate scripts and fix the cascade...,!3Hotel controller mas o menos terminado, añadidas request a repositorios y...
......@@ -9,6 +9,7 @@ import java.util.List;
@RestController
@RequestMapping("/bookings")
@CrossOrigin(origins = "http://localhost:4200")
public class BookingController {
private final BookingRepository bookingRepository;
......
package com.uva.roomBooking.exceptions;
package com.uva.roomBooking.Exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
......
......@@ -2,8 +2,6 @@ package com.uva.roomBooking.Models;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.Basic;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
......@@ -25,7 +23,7 @@ public class Booking {
@Basic(optional = false)
private int id;
@JoinColumn(name = "user_id", referencedColumnName = "id")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
private User userId;
@JoinColumn(name = "room_id", referencedColumnName = "id")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
......@@ -35,7 +33,8 @@ public class Booking {
@Column(name = "end_date", nullable = false)
private Date endDate;
public Booking(int id, User userId, Room roomID, Date startDate, Date endDate) {
public Booking (int id, User userId, Room roomID, Date startDate, Date endDate) {
this.id = id;
this.userId = userId;
this.roomID = roomID;
......@@ -43,43 +42,46 @@ public class Booking {
this.endDate = endDate;
}
public void setId(int id) {
public void setId (int id) {
this.id = id;
}
public int getId() {
public int getId () {
return this.id;
}
public void setUser(User userId) {
public void setUser (User userId) {
this.userId = userId;
}
public User getUser() {
public User getUser () {
return this.userId;
}
public void setRoom(Room roomID) {
public void setRoom (Room roomID) {
this.roomID = roomID;
}
public Room getRoom() {
public Room getRoom () {
return this.roomID;
}
public void setStartDate(Date startDate) {
public void setStartDate (Date startDate) {
this.startDate = startDate;
}
public Date getStartDate() {
public Date getStartDate () {
return this.startDate;
}
public void setEndDate(Date endDate) {
public void setEndDate (Date endDate) {
this.endDate = endDate;
}
public Date getEndDate() {
public Date getEndDate () {
return this.endDate;
}
}
......@@ -35,7 +35,7 @@ public class Hotel {
@OneToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Address address;
@OneToMany(mappedBy = "hotelId", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@OneToMany(mappedBy = "hotel", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Room> rooms;
public Hotel() {
......
......@@ -32,7 +32,7 @@ public class Room {
@ManyToOne
@JoinColumn(name = "hotel_id", referencedColumnName = "id")
@JsonIgnore
private Hotel hotelId;
private Hotel hotel;
@Column(name = "room_number", nullable = false)
private int roomNumber;
@Column(name = "type", nullable = false)
......@@ -48,7 +48,7 @@ public class Room {
public Room(int id, Hotel hotelId, int roomNumber, Tipo type, boolean available, List<Booking> bookings) {
this.id = id;
this.hotelId = hotelId;
this.hotel = hotelId;
this.roomNumber = roomNumber;
this.type = type;
this.available = available;
......@@ -64,11 +64,11 @@ public class Room {
}
public void setHotel(Hotel hotelId) {
this.hotelId = hotelId;
this.hotel = hotelId;
}
public Hotel getHotel() {
return this.hotelId;
return this.hotel;
}
public void setRoomNumber(int roomNumber) {
......
......@@ -8,6 +8,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
public interface RoomRepository extends JpaRepository<Room, Integer> {
......@@ -16,23 +17,24 @@ public interface RoomRepository extends JpaRepository<Room, Integer> {
// Borrar todas las habitaciones asociadas a un hotel
@Transactional
@Modifying
void deleteAllByHotelId(int hotelId);
// Encontrar habitaciones disponibles de un hotel en un rango de fechas
@Query("""
SELECT r FROM Room r
WHERE r.hotel.id = :hotelId
AND r.available = true
AND NOT EXISTS (
SELECT b FROM Booking b
WHERE b.room.id = r.id
AND (b.startDate <= :endDate AND b.endDate >= :startDate)
)
""")
SELECT r FROM Room r
WHERE r.hotel.id = :hotelId
AND r.available = true
AND NOT EXISTS (
SELECT b FROM Booking b
WHERE b.roomID.id = r.id
AND (b.startDate <= :endDate AND b.endDate >= :startDate)
)
""")
List<Room> findAvailableRoomsByHotelAndDates(
int hotelId, LocalDate startDate, LocalDate endDate);
// Encontrar una habitación específica por hotel y su ID
@Query("SELECT r FROM Room r WHERE r.hotel.id = :hotelId AND r.id = :roomId")
Optional<Room> findByHotelIdAndRoomId(int hotelId, int roomId);
Optional<Room> findByHotelIdAndRoomId(int hotelId, int roomId);
}
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