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
d0ee0162
authored
Nov 21, 2024
by
Antonio Rueda
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Primera versión del controlador REST con creación y login de usuarios
parent
f711c588
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
66 additions
and
16 deletions
pom.xml
src/main/java/es/ujaen/dae/reservahoteles/app/ReservaHoteles.java
src/main/java/es/ujaen/dae/reservahoteles/rest/ControladorReservas.java
src/main/java/es/ujaen/dae/reservahoteles/rest/dto/Mapeador.java
src/test/java/es/ujaen/dae/reservahoteles/servicios/TestServicioReservas.java
pom.xml
View file @
d0ee0162
...
...
@@ -38,7 +38,12 @@
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-cache
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
com.mysql
</groupId>
<artifactId>
mysql-connector-j
</artifactId>
...
...
src/main/java/es/ujaen/dae/reservahoteles/app/ReservaHoteles.java
View file @
d0ee0162
...
...
@@ -14,7 +14,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
(
scanBasePackages
={
"es.ujaen.dae.reservahoteles.servicios"
,
"es.ujaen.dae.reservahoteles.repositorios"
,
"es.ujaen.dae.reservahoteles.rest
.dto
"
"es.ujaen.dae.reservahoteles.rest"
})
@EntityScan
(
basePackages
=
"es.ujaen.dae.reservahoteles.entidades"
)
@EnableScheduling
...
...
src/main/java/es/ujaen/dae/reservahoteles/rest/ControladorReservas.java
View file @
d0ee0162
package
es
.
ujaen
.
dae
.
reservahoteles
.
rest
;
import
es.ujaen.dae.reservahoteles.entidades.Usuario
;
import
es.ujaen.dae.reservahoteles.excepciones.UsuarioNoRegistrado
;
import
es.ujaen.dae.reservahoteles.excepciones.UsuarioYaRegistrado
;
import
es.ujaen.dae.reservahoteles.rest.dto.DUsuario
;
import
es.ujaen.dae.reservahoteles.rest.dto.Mapeador
;
import
es.ujaen.dae.reservahoteles.servicios.ServicioReservas
;
import
jakarta.validation.ConstraintViolationException
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
import
org.springframework.web.bind.annotation.RestController
;
/**
*
* @author administrador
*/
@Service
@RestController
@RequestMapping
(
"/reservas"
)
public
class
ControladorReservas
{
@Autowired
Mapeador
mapeador
;
@Autowired
ServicioReservas
servicioReservas
;
// Definir un mapeado global para cualquier excepción de validación de beans
@ResponseStatus
(
HttpStatus
.
UNPROCESSABLE_ENTITY
)
@ExceptionHandler
(
ConstraintViolationException
.
class
)
public
void
mapeadoExcepcionConstraintViolationException
()
{}
@PostMapping
(
"/usuarios"
)
public
ResponseEntity
<
Void
>
nuevoCliente
(
@RequestBody
DUsuario
usuario
)
{
try
{
servicioReservas
.
nuevoCliente
(
mapeador
.
entidad
(
usuario
));
}
catch
(
UsuarioYaRegistrado
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
CONFLICT
).
build
();
}
return
ResponseEntity
.
ok
().
build
();
}
@GetMapping
(
"/usuarios/{email}"
)
public
ResponseEntity
<
DUsuario
>
loginCliente
(
@PathVariable
String
email
,
@RequestBody
String
clave
)
{
try
{
Usuario
usuario
=
servicioReservas
.
login
(
email
,
clave
).
orElseThrow
(
UsuarioNoRegistrado:
:
new
);
return
ResponseEntity
.
ok
(
mapeador
.
dto
(
usuario
));
}
catch
(
UsuarioNoRegistrado
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
UNAUTHORIZED
).
build
();
}
}
}
src/main/java/es/ujaen/dae/reservahoteles/rest/dto/Mapeador.java
View file @
d0ee0162
...
...
@@ -5,9 +5,6 @@ import es.ujaen.dae.reservahoteles.entidades.Reserva;
import
es.ujaen.dae.reservahoteles.entidades.Usuario
;
import
es.ujaen.dae.reservahoteles.excepciones.UsuarioNoRegistrado
;
import
es.ujaen.dae.reservahoteles.repositorios.RepositorioUsuarios
;
import
es.ujaen.dae.reservahoteles.rest.dto.DHotel
;
import
es.ujaen.dae.reservahoteles.rest.dto.DReserva
;
import
es.ujaen.dae.reservahoteles.rest.dto.DUsuario
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
...
...
@@ -20,16 +17,16 @@ public class Mapeador {
@Autowired
RepositorioUsuarios
repositorioUsuarios
;
DUsuario
dto
(
Usuario
usuario
)
{
public
DUsuario
dto
(
Usuario
usuario
)
{
// Nunca extraemos la clave de la entidad
return
new
DUsuario
(
usuario
.
nombre
(),
usuario
.
direccion
(),
usuario
.
tlf
(),
usuario
.
email
(),
""
);
}
Usuario
entidad
(
DUsuario
dUsuario
)
{
public
Usuario
entidad
(
DUsuario
dUsuario
)
{
return
new
Usuario
(
dUsuario
.
nombre
(),
dUsuario
.
direccion
(),
dUsuario
.
tlf
(),
dUsuario
.
email
(),
dUsuario
.
clave
());
}
DHotel
dto
(
Hotel
hotel
)
{
public
DHotel
dto
(
Hotel
hotel
)
{
return
new
DHotel
(
hotel
.
id
(),
hotel
.
nombre
(),
...
...
@@ -42,7 +39,7 @@ public class Mapeador {
hotel
.
precioHabDoble
());
}
Hotel
entidad
(
DHotel
dHotel
)
{
public
Hotel
entidad
(
DHotel
dHotel
)
{
return
new
Hotel
(
dHotel
.
id
(),
dHotel
.
nombre
(),
...
...
@@ -55,7 +52,7 @@ public class Mapeador {
dHotel
.
precioHabDoble
());
}
stat
ic
DReserva
dto
(
Reserva
reserva
)
{
publ
ic
DReserva
dto
(
Reserva
reserva
)
{
return
new
DReserva
(
reserva
.
num
(),
reserva
.
fechaInicio
(),
...
...
@@ -65,7 +62,7 @@ public class Mapeador {
reserva
.
cliente
().
email
());
}
Reserva
entidad
(
DReserva
dReserva
)
{
public
Reserva
entidad
(
DReserva
dReserva
)
{
Usuario
usuario
=
repositorioUsuarios
.
buscar
(
dReserva
.
emailUsuario
())
.
orElseThrow
(
UsuarioNoRegistrado:
:
new
);
...
...
src/test/java/es/ujaen/dae/reservahoteles/servicios/TestServicioReservas.java
View file @
d0ee0162
package
es
.
ujaen
.
dae
.
reservahoteles
.
servicios
;
import
es.ujaen.dae.reservahoteles.entidades.Usuario
;
import
es.ujaen.dae.reservahoteles.entidades.Hotel
;
import
es.ujaen.dae.reservahoteles.e
xcepciones.ClienteYaRegistrad
o
;
import
es.ujaen.dae.reservahoteles.e
ntidades.Usuari
o
;
import
es.ujaen.dae.reservahoteles.excepciones.NoDisponibilidadReserva
;
import
es.ujaen.dae.reservahoteles.excepciones.UsuarioYaRegistrado
;
import
jakarta.validation.ConstraintViolationException
;
import
java.time.LocalDate
;
import
java.util.List
;
...
...
@@ -17,6 +17,8 @@ import org.springframework.boot.test.context.SpringBootTest;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.context.ActiveProfiles
;
/**
* Test de integración del servicio principal del sistema
* @author ajrueda
...
...
@@ -40,7 +42,7 @@ public class TestServicioReservas {
// Test de intento de creación repetido
var
cliente2
=
new
Usuario
(
"Pedro"
,
"Jaén Jaén"
,
"611301114"
,
"pjaen@gmail.com"
,
"miClAvE"
);
servicio
.
nuevoCliente
(
cliente2
);
assertThatThrownBy
(()
->
servicio
.
nuevoCliente
(
cliente2
)).
isInstanceOf
(
Cliente
YaRegistrado
.
class
);
assertThatThrownBy
(()
->
servicio
.
nuevoCliente
(
cliente2
)).
isInstanceOf
(
Usuario
YaRegistrado
.
class
);
}
@Test
...
...
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