Commit 11d09547 by Antonio Rueda

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

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