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
3c3b4c84
authored
Nov 04, 2024
by
Antonio Rueda
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Correcciones menores relacioandas con la operación de reserva
parent
daf6bd79
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
11 deletions
src/main/java/es/ujaen/dae/reservahoteles/repositorios/RepositorioHoteles.java
src/main/java/es/ujaen/dae/reservahoteles/servicios/ServicioReservas.java
src/test/java/es/ujaen/dae/reservahoteles/servicios/TestServicioReservas.java
src/main/java/es/ujaen/dae/reservahoteles/repositorios/RepositorioHoteles.java
View file @
3c3b4c84
...
...
@@ -6,6 +6,7 @@ import static es.ujaen.dae.reservahoteles.util.UtilString.normalizar;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.LockModeType
;
import
jakarta.persistence.PersistenceContext
;
import
java.time.LocalDate
;
import
java.util.List
;
import
java.util.Optional
;
import
org.springframework.stereotype.Repository
;
...
...
@@ -31,7 +32,7 @@ public class RepositorioHoteles {
public
Optional
<
Hotel
>
buscarPorIdBloqueando
(
int
id
)
{
return
Optional
.
ofNullable
(
em
.
find
(
Hotel
.
class
,
id
,
LockModeType
.
PESSIMISTIC_WRITE
));
}
@Transactional
(
propagation
=
Propagation
.
SUPPORTS
,
readOnly
=
true
)
public
List
<
Hotel
>
buscarPorNombreLocalidad
(
String
nombre
,
String
localidad
)
{
return
em
.
createQuery
(
"select h from Hotel h where "
+
...
...
@@ -55,8 +56,8 @@ public class RepositorioHoteles {
.
getResultList
();
}
public
void
guardar
(
Hotel
usuario
)
{
em
.
persist
(
usuario
);
public
void
guardar
(
Hotel
hotel
)
{
em
.
persist
(
hotel
);
}
public
Hotel
actualizar
(
Hotel
hotel
)
{
...
...
src/main/java/es/ujaen/dae/reservahoteles/servicios/ServicioReservas.java
View file @
3c3b4c84
...
...
@@ -139,6 +139,8 @@ public class ServicioReservas {
hotel
=
repositorioHoteles
.
actualizar
(
hotel
);
return
hotel
.
disponible
(
fechaInicio
,
fechaFin
,
numHabSimple
,
numHabDoble
);
}
/**
* Realiza una reserva en un hotel. La reserva debe ser correcta y haber disponibilidad.
*
...
...
@@ -150,6 +152,9 @@ public class ServicioReservas {
* @param numHabDoble número de habitaciones dobles solicitadas
* @return la reserva recien creada en caso de éxito
*/
// Opcion 1: con bloqueo pesimista (no requiere atributo version en entidad Hotel)
/*
@Transactional
public Reserva reserva(Usuario cliente, Hotel hotel,
LocalDate fechaInicio, LocalDate fechaFin,
...
...
@@ -157,13 +162,23 @@ public class ServicioReservas {
// Opción con bloqueo pesimista (no requiere atributo version en Hotel)
//
//
hotel = repositorioHoteles.buscarPorIdBloqueando(hotel.id()).get();
//
var reserva = new Reserva(cliente, fechaInicio, fechaFin, numHabSimple, numHabDoble);
hotel = repositorioHoteles.buscarPorIdBloqueando(hotel.id()).get();
var reserva = new Reserva(cliente, fechaInicio, fechaFin, numHabSimple, numHabDoble);
//
// hotel.nuevaReserva(reserva);
// repositorioHoteles.guardarReserva(reserva);
hotel.nuevaReserva(reserva);
repositorioHoteles.guardarReserva(reserva);
// No hace falta guardar explícitamente el hotel porque está conectado con la transacción
return reserva;
}
*/
// Opcion 2: con bloqueo optimista y transacción a nivel de operación de sevicio
// Opción con bloqueo optimista (requiere el atributo version en Hotel)
@Transactional
public
Reserva
reserva
(
Usuario
cliente
,
Hotel
hotel
,
LocalDate
fechaInicio
,
LocalDate
fechaFin
,
@PositiveOrZero
int
numHabSimple
,
@PositiveOrZero
int
numHabDoble
)
{
var
reserva
=
new
Reserva
(
cliente
,
fechaInicio
,
fechaFin
,
numHabSimple
,
numHabDoble
);
...
...
@@ -182,7 +197,7 @@ public class ServicioReservas {
// No hace falta guardar explícitamente el hotel porque está conectado con la transacción
return
reserva
;
}
}
@Transactional
@Scheduled
(
cron
=
"0 0 0 1 * ?"
)
...
...
src/test/java/es/ujaen/dae/reservahoteles/servicios/TestServicioReservas.java
View file @
3c3b4c84
...
...
@@ -145,14 +145,16 @@ public class TestServicioReservas {
var
cliente2
=
servicio
.
login
(
"jgranada@gmail.com"
,
"miClAvE"
).
get
();
// Reservar 2 habitaciones dobles
new
Thread
(()->
{
var
th
=
new
Thread
(()->
{
try
{
servicio
.
reserva
(
cliente1
,
hotel
,
LocalDate
.
now
().
plusDays
(
7
),
LocalDate
.
now
().
plusDays
(
10
),
0
,
2
);
}
catch
(
NoDisponibilidadReserva
e
)
{
Logger
.
getLogger
(
servicio
.
getClass
().
getName
()).
warning
(
"Reserva de cliente 1 sin disponibilidad"
);
}
}).
start
();
});
th
.
start
();
try
{
servicio
.
reserva
(
cliente2
,
hotel
,
LocalDate
.
now
().
plusDays
(
5
),
LocalDate
.
now
().
plusDays
(
12
),
0
,
2
);
...
...
@@ -161,6 +163,8 @@ public class TestServicioReservas {
Logger
.
getLogger
(
servicio
.
getClass
().
getName
()).
warning
(
"Reserva de cliente 2 sin disponibilidad"
);
}
try
{
th
.
join
();
}
catch
(
InterruptedException
e
)
{}
var
hotelConReservas
=
servicio
.
hotelConReservas
(
servicio
.
buscarHotel
(
"bed and breakfast"
,
"almeria"
).
get
(
0
));
assertThat
(
hotelConReservas
.
reservasEntre
(
LocalDate
.
MIN
,
LocalDate
.
MAX
)).
singleElement
();
}
...
...
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