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

Arreglado error

parent 39eba64a
Branches
Tags
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,6 +33,7 @@ public class Booking {
@Column(name = "end_date", nullable = false)
private Date endDate;
public Booking (int id, User userId, Room roomID, Date startDate, Date endDate) {
this.id = id;
this.userId = userId;
......@@ -82,4 +81,7 @@ public class Booking {
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,6 +17,7 @@ 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
......@@ -25,7 +27,7 @@ public interface RoomRepository extends JpaRepository<Room, Integer> {
AND r.available = true
AND NOT EXISTS (
SELECT b FROM Booking b
WHERE b.room.id = r.id
WHERE b.roomID.id = r.id
AND (b.startDate <= :endDate AND b.endDate >= :startDate)
)
""")
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment