Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Antonio Rueda
/
reserva-hoteles
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
f63846d1
authored
Oct 30, 2024
by
Antonio Rueda
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Fin de adaptación de código a JPA
parent
11d09547
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
72 additions
and
16 deletions
src/main/java/es/ujaen/dae/reservahoteles/entidades/Hotel.java
src/main/java/es/ujaen/dae/reservahoteles/entidades/Reserva.java
src/main/java/es/ujaen/dae/reservahoteles/repositorios/RepositorioHoteles.java
src/main/java/es/ujaen/dae/reservahoteles/repositorios/RepositorioUsuarios.java
src/main/java/es/ujaen/dae/reservahoteles/servicios/ServicioReservas.java
src/test/java/es/ujaen/dae/reservahoteles/entidades/TestHotel.java
src/main/java/es/ujaen/dae/reservahoteles/entidades/Hotel.java
View file @
f63846d1
...
...
@@ -4,6 +4,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.CascadeType
;
import
jakarta.persistence.Entity
;
import
jakarta.persistence.GeneratedValue
;
import
jakarta.persistence.GenerationType
;
...
...
src/main/java/es/ujaen/dae/reservahoteles/entidades/Reserva.java
View file @
f63846d1
...
...
@@ -20,7 +20,6 @@ public class Reserva {
// clave
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
@Positive
int
num
;
// Fechas de inicio y fin de reserva
...
...
@@ -36,9 +35,13 @@ public class Reserva {
@NotNull
@ManyToOne
Usuario
cliente
;
public
Reserva
()
{
}
public
Reserva
(
int
num
,
Usuario
cliente
,
LocalDate
fechaInicio
,
LocalDate
fechaFin
,
int
numHabSimple
,
int
numHabDoble
)
{
this
.
num
=
num
;
public
Reserva
(
Usuario
cliente
,
LocalDate
fechaInicio
,
LocalDate
fechaFin
,
int
numHabSimple
,
int
numHabDoble
)
{
this
.
num
=
0
;
this
.
cliente
=
cliente
;
this
.
fechaInicio
=
fechaInicio
;
this
.
fechaFin
=
fechaFin
;
...
...
src/main/java/es/ujaen/dae/reservahoteles/repositorios/RepositorioHoteles.java
View file @
f63846d1
package
es
.
ujaen
.
dae
.
reservahoteles
.
repositorios
;
import
es.ujaen.dae.reservahoteles.entidades.Hotel
;
import
es.ujaen.dae.reservahoteles.entidades.Reserva
;
import
static
es
.
ujaen
.
dae
.
reservahoteles
.
util
.
UtilString
.
normalizar
;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.PersistenceContext
;
import
ja
karta.transaction.Transactional
;
import
ja
va.time.LocalDate
;
import
java.util.List
;
import
org.springframework.stereotype.Repository
;
import
org.springframework.transaction.annotation.Propagation
;
import
org.springframework.transaction.annotation.Transactional
;
/**
*
...
...
@@ -18,7 +21,8 @@ public class RepositorioHoteles {
@PersistenceContext
EntityManager
em
;
public
List
<
Hotel
>
buscarHotel
(
String
nombre
,
String
localidad
)
{
@Transactional
(
propagation
=
Propagation
.
SUPPORTS
,
readOnly
=
true
)
public
List
<
Hotel
>
buscarPorNombreLocalidad
(
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
)
+
"%"
)
...
...
@@ -26,9 +30,33 @@ public class RepositorioHoteles {
.
getResultList
();
}
@Transactional
(
propagation
=
Propagation
.
SUPPORTS
,
readOnly
=
true
)
public
List
<
Hotel
>
buscarPorLocalidad
(
String
localidad
)
{
return
em
.
createQuery
(
"select h from Hotel h where "
+
"h.localidadSimp like ?1"
,
Hotel
.
class
)
.
setParameter
(
1
,
"%"
+
normalizar
(
localidad
)
+
"%"
)
.
getResultList
();
}
@Transactional
(
propagation
=
Propagation
.
SUPPORTS
,
readOnly
=
true
)
public
List
<
Integer
>
listadoIds
()
{
return
em
.
createQuery
(
"select h.id from Hotel h"
)
.
getResultList
();
}
public
void
guardar
(
Hotel
usuario
)
{
em
.
persist
(
usuario
);
}
public
Hotel
actualizar
(
Hotel
hotel
)
{
return
em
.
merge
(
hotel
);
}
public
void
guardarReserva
(
Reserva
reserva
)
{
em
.
persist
(
reserva
);
}
public
void
borrarReserva
(
Reserva
reserva
)
{
em
.
remove
(
reserva
);
}
}
src/main/java/es/ujaen/dae/reservahoteles/repositorios/RepositorioUsuarios.java
View file @
f63846d1
...
...
@@ -4,9 +4,10 @@ import es.ujaen.dae.reservahoteles.entidades.Usuario;
import
es.ujaen.dae.reservahoteles.excepciones.ClienteYaRegistrado
;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.PersistenceContext
;
import
jakarta.transaction.Transactional
;
import
java.util.Optional
;
import
org.springframework.stereotype.Repository
;
import
org.springframework.transaction.annotation.Propagation
;
import
org.springframework.transaction.annotation.Transactional
;
/**
*
...
...
@@ -18,6 +19,7 @@ public class RepositorioUsuarios {
@PersistenceContext
EntityManager
em
;
@Transactional
(
propagation
=
Propagation
.
SUPPORTS
,
readOnly
=
true
)
public
Optional
<
Usuario
>
buscar
(
String
email
)
{
return
Optional
.
ofNullable
(
em
.
find
(
Usuario
.
class
,
email
));
}
...
...
src/main/java/es/ujaen/dae/reservahoteles/servicios/ServicioReservas.java
View file @
f63846d1
...
...
@@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.validation.annotation.Validated
;
/**
...
...
@@ -97,17 +98,23 @@ public class ServicioReservas {
* @param numHabSimple número de habitaciones simples solicitadas
* @param numHabDoble número de habitaciones dobles solicitadas
* @return la lista de hoteles candidatos
*/
*/
@Transactional
public
List
<
Hotel
>
buscarHotelesDisponiblesPorLocalidad
(
@NotBlank
String
localidad
,
@FutureOrPresent
LocalDate
fechaInicio
,
@Future
LocalDate
fechaFin
,
@PositiveOrZero
int
numHabSimple
,
@PositiveOrZero
int
numHabDoble
)
{
var
localidadNorm
=
UtilString
.
normalizar
(
localidad
);
return
hoteles
.
values
().
stream
().
filter
(
h
->
// return hoteles.values().stream().filter(h ->
// UtilString.normalizar(h.localidad()).contains(localidadNorm) &&
// h.disponible(fechaInicio, fechaFin, numHabSimple, numHabDoble)
// ).toList();
List
<
Hotel
>
hotelesLocalidad
=
repositorioHoteles
.
buscarPorLocalidad
(
localidadNorm
);
return
hotelesLocalidad
.
stream
().
filter
(
h
->
UtilString
.
normalizar
(
h
.
localidad
()).
contains
(
localidadNorm
)
&&
h
.
disponible
(
fechaInicio
,
fechaFin
,
numHabSimple
,
numHabDoble
)
).
toList
();
}
/**
...
...
@@ -117,6 +124,7 @@ public class ServicioReservas {
* @param localidad el nombre total o parcial de la localidad
* @return la lista de hoteles candidatos
*/
@Transactional
public
List
<
Hotel
>
buscarHotel
(
@NotBlank
String
nombre
,
@NotBlank
String
localidad
)
{
// var nombreNorm = UtilString.normalizar(nombre);
// var localidadNorm = UtilString.normalizar(localidad);
...
...
@@ -126,7 +134,11 @@ public class ServicioReservas {
// UtilString.normalizar(h.nombre()).contains(nombreNorm))
// .toList();
return
repositorioHoteles
.
buscarHotel
(
nombre
,
localidad
);
List
<
Hotel
>
hoteles
=
repositorioHoteles
.
buscarPorNombreLocalidad
(
nombre
,
localidad
);
for
(
var
hotel:
hoteles
)
{
hotel
.
reservasEntre
(
LocalDate
.
MIN
,
LocalDate
.
MAX
);
}
return
hoteles
;
}
/**
...
...
@@ -160,18 +172,28 @@ public class ServicioReservas {
LocalDate
fechaInicio
,
LocalDate
fechaFin
,
@PositiveOrZero
int
numHabSimple
,
@PositiveOrZero
int
numHabDoble
)
{
var
reserva
=
new
Reserva
(
nReserva
++,
cliente
,
fechaInicio
,
fechaFin
,
numHabSimple
,
numHabDoble
);
var
reserva
=
new
Reserva
(
cliente
,
fechaInicio
,
fechaFin
,
numHabSimple
,
numHabDoble
);
repositorioHoteles
.
guardarReserva
(
reserva
);
hotel
.
nuevaReserva
(
reserva
);
repositorioHoteles
.
actualizar
(
hotel
);
return
reserva
;
}
@Transactional
@Scheduled
(
cron
=
"0 0 0 1 * ?"
)
public
void
eliminarReservasAntiguas
()
{
var
fechaLimite
=
LocalDate
.
now
().
minusMonths
(
mesesHistorico
);
for
(
var
hotel:
hoteles
.
values
())
{
hotel
.
eliminarReservasAnteriores
(
fechaLimite
);
}
List
<
Integer
>
idHoteles
=
repositorioHoteles
.
listadoIds
();
idHoteles
.
stream
()
.
map
(
id
->
repositorioHoteles
.
buscarPorId
(
id
).
get
())
.
forEach
(
hotel
->
hotel
.
eliminarReservasAnteriores
(
fechaLimite
));
//for (var hotel: hoteles.values()) {
// hotel.eliminarReservasAnteriores(fechaLimite);
//}
}
}
src/test/java/es/ujaen/dae/reservahoteles/entidades/TestHotel.java
View file @
f63846d1
...
...
@@ -15,10 +15,10 @@ public class TestHotel {
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
);
var
reserva1
=
new
Reserva
(
cliente
,
LocalDate
.
now
().
plusDays
(
15
),
LocalDate
.
now
().
plusDays
(
17
),
0
,
1
);
hotel
.
nuevaReserva
(
reserva1
);
var
reserva2
=
new
Reserva
(
2
,
cliente
,
LocalDate
.
now
().
plusDays
(
30
),
LocalDate
.
now
().
plusDays
(
35
),
1
,
1
);
var
reserva2
=
new
Reserva
(
cliente
,
LocalDate
.
now
().
plusDays
(
30
),
LocalDate
.
now
().
plusDays
(
35
),
1
,
1
);
hotel
.
nuevaReserva
(
reserva2
);
// ARREGLAR PARA NO COMPARAR REFERENCIAS
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment