Commit 11d09547 by Antonio Rueda

Añadido repositorio de hoteles y servicios para crear y buscar hoteles

parent 33e4bcdc
......@@ -40,11 +40,6 @@
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
......
......@@ -3,6 +3,7 @@ package es.ujaen.dae.reservahoteles.entidades;
import es.ujaen.dae.reservahoteles.excepciones.ReservaNoValida;
import es.ujaen.dae.reservahoteles.excepciones.NoDisponibilidadReserva;
import static es.ujaen.dae.reservahoteles.util.UtilString.normalizar;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
......@@ -23,17 +24,21 @@ import java.util.List;
*/
@Entity
public class Hotel {
@Positive
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;
@NotBlank
String nombre;
@NotBlank
String nombreSimp;
// Dirección
@NotBlank
String localidad;
@NotBlank
String localidadSimp;
@NotBlank
String provincia;
@Pattern(regexp="^(?:0[1-9]|[1-4]\\d|5[0-2])\\d{3}$", message = "No es un código postal válido")
String cp;
......@@ -54,10 +59,15 @@ public class Hotel {
@JoinColumn(name = "hotel_id")
List<Reserva> reservas;
public Hotel(int id, String nombre, String localidad, String provincia, String cp, int numHabSimple, int numHabDoble, int precioHabSimple, int precioHabDoble) {
this.id = id;
public Hotel() {
}
public Hotel(String nombre, String localidad, String provincia, String cp, int numHabSimple, int numHabDoble, int precioHabSimple, int precioHabDoble) {
this.nombre = nombre;
this.nombreSimp = normalizar(nombre);
this.localidad = localidad;
this.localidadSimp = normalizar(localidad);
this.provincia = provincia;
this.cp = cp;
this.numHabSimple = numHabSimple;
......
package es.ujaen.dae.reservahoteles.repositorios;
import es.ujaen.dae.reservahoteles.entidades.Hotel;
import static es.ujaen.dae.reservahoteles.util.UtilString.normalizar;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import java.util.List;
import org.springframework.stereotype.Repository;
/**
*
* @author administrador
*/
@Transactional
@Repository
public class RepositorioHoteles {
@PersistenceContext
EntityManager em;
public List<Hotel> buscarHotel(String nombre, String localidad) {
return em.createQuery("select h from Hotel h where " +
"h.nombreSimp like ?1 and h.localidadSimp like ?2", Hotel.class)
.setParameter(1, "%" + normalizar(nombre) + "%")
.setParameter(2, "%" + normalizar(localidad) + "%")
.getResultList();
}
public void guardar(Hotel usuario) {
em.persist(usuario);
}
}
......@@ -6,6 +6,7 @@ import es.ujaen.dae.reservahoteles.entidades.Usuario;
import es.ujaen.dae.reservahoteles.entidades.Hotel;
import es.ujaen.dae.reservahoteles.entidades.Reserva;
import es.ujaen.dae.reservahoteles.excepciones.ClienteYaRegistrado;
import es.ujaen.dae.reservahoteles.repositorios.RepositorioHoteles;
import es.ujaen.dae.reservahoteles.repositorios.RepositorioUsuarios;
import es.ujaen.dae.reservahoteles.util.UtilString;
import jakarta.validation.Valid;
......@@ -35,6 +36,9 @@ public class ServicioReservas {
@Autowired
RepositorioUsuarios repositorioClientes;
@Autowired
RepositorioHoteles repositorioHoteles;
Map<Integer, Hotel> hoteles;
Map<String, Usuario> clientes;
......@@ -55,7 +59,8 @@ public class ServicioReservas {
if (!direccion.nombre().equals("direccion"))
throw new OperacionDeDireccion();
hoteles.put(hotel.id(), hotel);
//hoteles.put(hotel.id(), hotel);
repositorioHoteles.guardar(hotel);
}
public void nuevoCliente(@Valid Usuario cliente) {
......@@ -102,6 +107,7 @@ public class ServicioReservas {
UtilString.normalizar(h.localidad()).contains(localidadNorm) &&
h.disponible(fechaInicio, fechaFin, numHabSimple, numHabDoble)
).toList();
}
/**
......@@ -112,13 +118,15 @@ public class ServicioReservas {
* @return la lista de hoteles candidatos
*/
public List<Hotel> buscarHotel(@NotBlank String nombre, @NotBlank String localidad) {
var nombreNorm = UtilString.normalizar(nombre);
var localidadNorm = UtilString.normalizar(localidad);
// var nombreNorm = UtilString.normalizar(nombre);
// var localidadNorm = UtilString.normalizar(localidad);
return hoteles.values().stream().filter(h ->
UtilString.normalizar(h.localidad()).contains(localidadNorm) &&
UtilString.normalizar(h.nombre()).contains(nombreNorm))
.toList();
// return hoteles.values().stream().filter(h ->
// UtilString.normalizar(h.localidad()).contains(localidadNorm) &&
// UtilString.normalizar(h.nombre()).contains(nombreNorm))
// .toList();
return repositorioHoteles.buscarHotel(nombre, localidad);
}
/**
......
......@@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test;
public class TestHotel {
@Test
void testReserva() {
var hotel = new Hotel(1, "Gran Hotel Almería", "Almería", "Almería", "04001", 25, 50, 100, 180);
var hotel = new Hotel("Gran Hotel Almería", "Almería", "Almería", "04001", 25, 50, 100, 180);
var cliente = new Usuario("Pedro", "Jaén Jaén", "611203025", "pjaen@gmail.com", "miClAvE");
var reserva1 = new Reserva(1, cliente, LocalDate.now().plusDays(15), LocalDate.now().plusDays(17), 0, 1);
......
......@@ -56,8 +56,8 @@ public class TestServicioReservas {
@Test
@DirtiesContext
void testBuscarHotel() {
var hotel1 = new Hotel(1, "Gran Hotel Almería", "Almería", "Almería", "04001", 25, 50, 100, 180);
var hotel2 = new Hotel(2, "Hotel Infanta Cristina", "Jaén", "Jaén", "23009", 30, 60, 120, 200);
var hotel1 = new Hotel("Gran Hotel Almería", "Almería", "Almería", "04001", 25, 50, 100, 180);
var hotel2 = new Hotel("Hotel Infanta Cristina", "Jaén", "Jaén", "23009", 30, 60, 120, 200);
var direccion = servicio.login("direccion@hotelxyz.es", "SeCrEtO").get();
......@@ -73,9 +73,9 @@ public class TestServicioReservas {
@Test
@DirtiesContext
void testBuscarPorLocalidad() {
var hotel1 = new Hotel(1, "Gran Hotel Almería", "Almería", "Almería", "04001", 25, 50, 100, 180);
var hotel2 = new Hotel(2, "Hotel Espejo del Mar", "Almería", "Almería", "04001", 15, 35, 80, 110);
var hotel3 = new Hotel(3, "Hotel Infanta Cristina", "Jaén", "Jaén", "23009", 30, 60, 120, 200);
var hotel1 = new Hotel("Gran Hotel Almería", "Almería", "Almería", "04001", 25, 50, 100, 180);
var hotel2 = new Hotel("Hotel Espejo del Mar", "Almería", "Almería", "04001", 15, 35, 80, 110);
var hotel3 = new Hotel("Hotel Infanta Cristina", "Jaén", "Jaén", "23009", 30, 60, 120, 200);
var direccion = servicio.login("direccion@hotelxyz.es", "SeCrEtO").get();
......@@ -97,7 +97,7 @@ public class TestServicioReservas {
@DirtiesContext
void testReservaHotel() {
var direccion = servicio.login("direccion@hotelxyz.es", "SeCrEtO").get();
servicio.nuevoHotel(direccion, new Hotel(1, "Bed and Breakfast Almería", "Almería", "Almería", "04001", 2, 2, 60, 100));
servicio.nuevoHotel(direccion, new Hotel("Bed and Breakfast Almería", "Almería", "Almería", "04001", 2, 2, 60, 100));
servicio.nuevoCliente(new Usuario("Pedro", "Jaén Jaén", "611203025", "pjaen@gmail.com", "miClAvE"));
......@@ -115,7 +115,7 @@ public class TestServicioReservas {
@DirtiesContext
void testReservaHotelSinDisponibilidad() {
var direccion = servicio.login("direccion@hotelxyz.es", "SeCrEtO").get();
servicio.nuevoHotel(direccion, new Hotel(1, "Bed and Breakfast Almería", "Almería", "Almería", "04001", 2, 2, 60, 100));
servicio.nuevoHotel(direccion, new Hotel("Bed and Breakfast Almería", "Almería", "Almería", "04001", 2, 2, 60, 100));
servicio.nuevoCliente(new Usuario("Pedro", "Jaén Jaén", "611203025", "pjaen@gmail.com", "miClAvE"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment