Commit d0ee0162 by Antonio Rueda

Primera versión del controlador REST con creación y login de usuarios

parent f711c588
...@@ -38,7 +38,12 @@ ...@@ -38,7 +38,12 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId> <artifactId>spring-boot-starter-cache</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.mysql</groupId> <groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId> <artifactId>mysql-connector-j</artifactId>
......
...@@ -14,7 +14,7 @@ import org.springframework.scheduling.annotation.EnableScheduling; ...@@ -14,7 +14,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication(scanBasePackages={ @SpringBootApplication(scanBasePackages={
"es.ujaen.dae.reservahoteles.servicios", "es.ujaen.dae.reservahoteles.servicios",
"es.ujaen.dae.reservahoteles.repositorios", "es.ujaen.dae.reservahoteles.repositorios",
"es.ujaen.dae.reservahoteles.rest.dto" "es.ujaen.dae.reservahoteles.rest"
}) })
@EntityScan(basePackages="es.ujaen.dae.reservahoteles.entidades") @EntityScan(basePackages="es.ujaen.dae.reservahoteles.entidades")
@EnableScheduling @EnableScheduling
......
package es.ujaen.dae.reservahoteles.rest; 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.rest.dto.Mapeador;
import es.ujaen.dae.reservahoteles.servicios.ServicioReservas;
import jakarta.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired; 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 * @author administrador
*/ */
@Service @RestController
@RequestMapping("/reservas")
public class ControladorReservas { public class ControladorReservas {
@Autowired @Autowired
Mapeador mapeador; 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();
}
}
} }
...@@ -5,9 +5,6 @@ import es.ujaen.dae.reservahoteles.entidades.Reserva; ...@@ -5,9 +5,6 @@ import es.ujaen.dae.reservahoteles.entidades.Reserva;
import es.ujaen.dae.reservahoteles.entidades.Usuario; import es.ujaen.dae.reservahoteles.entidades.Usuario;
import es.ujaen.dae.reservahoteles.excepciones.UsuarioNoRegistrado; import es.ujaen.dae.reservahoteles.excepciones.UsuarioNoRegistrado;
import es.ujaen.dae.reservahoteles.repositorios.RepositorioUsuarios; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -20,16 +17,16 @@ public class Mapeador { ...@@ -20,16 +17,16 @@ public class Mapeador {
@Autowired @Autowired
RepositorioUsuarios repositorioUsuarios; RepositorioUsuarios repositorioUsuarios;
DUsuario dto(Usuario usuario) { public DUsuario dto(Usuario usuario) {
// Nunca extraemos la clave de la entidad // Nunca extraemos la clave de la entidad
return new DUsuario(usuario.nombre(), usuario.direccion(), usuario.tlf(), usuario.email(), ""); 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()); 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( return new DHotel(
hotel.id(), hotel.id(),
hotel.nombre(), hotel.nombre(),
...@@ -42,7 +39,7 @@ public class Mapeador { ...@@ -42,7 +39,7 @@ public class Mapeador {
hotel.precioHabDoble()); hotel.precioHabDoble());
} }
Hotel entidad(DHotel dHotel) { public Hotel entidad(DHotel dHotel) {
return new Hotel( return new Hotel(
dHotel.id(), dHotel.id(),
dHotel.nombre(), dHotel.nombre(),
...@@ -55,7 +52,7 @@ public class Mapeador { ...@@ -55,7 +52,7 @@ public class Mapeador {
dHotel.precioHabDoble()); dHotel.precioHabDoble());
} }
static DReserva dto(Reserva reserva) { public DReserva dto(Reserva reserva) {
return new DReserva( return new DReserva(
reserva.num(), reserva.num(),
reserva.fechaInicio(), reserva.fechaInicio(),
...@@ -65,7 +62,7 @@ public class Mapeador { ...@@ -65,7 +62,7 @@ public class Mapeador {
reserva.cliente().email()); reserva.cliente().email());
} }
Reserva entidad(DReserva dReserva) { public Reserva entidad(DReserva dReserva) {
Usuario usuario = repositorioUsuarios.buscar(dReserva.emailUsuario()) Usuario usuario = repositorioUsuarios.buscar(dReserva.emailUsuario())
.orElseThrow(UsuarioNoRegistrado::new); .orElseThrow(UsuarioNoRegistrado::new);
......
package es.ujaen.dae.reservahoteles.servicios; 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.entidades.Hotel;
import es.ujaen.dae.reservahoteles.excepciones.ClienteYaRegistrado; import es.ujaen.dae.reservahoteles.entidades.Usuario;
import es.ujaen.dae.reservahoteles.excepciones.NoDisponibilidadReserva; import es.ujaen.dae.reservahoteles.excepciones.NoDisponibilidadReserva;
import es.ujaen.dae.reservahoteles.excepciones.UsuarioYaRegistrado;
import jakarta.validation.ConstraintViolationException; import jakarta.validation.ConstraintViolationException;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
...@@ -17,6 +17,8 @@ import org.springframework.boot.test.context.SpringBootTest; ...@@ -17,6 +17,8 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
/** /**
* Test de integración del servicio principal del sistema * Test de integración del servicio principal del sistema
* @author ajrueda * @author ajrueda
...@@ -40,7 +42,7 @@ public class TestServicioReservas { ...@@ -40,7 +42,7 @@ public class TestServicioReservas {
// Test de intento de creación repetido // Test de intento de creación repetido
var cliente2 = new Usuario("Pedro", "Jaén Jaén", "611301114", "pjaen@gmail.com", "miClAvE"); var cliente2 = new Usuario("Pedro", "Jaén Jaén", "611301114", "pjaen@gmail.com", "miClAvE");
servicio.nuevoCliente(cliente2); servicio.nuevoCliente(cliente2);
assertThatThrownBy(() -> servicio.nuevoCliente(cliente2)).isInstanceOf(ClienteYaRegistrado.class); assertThatThrownBy(() -> servicio.nuevoCliente(cliente2)).isInstanceOf(UsuarioYaRegistrado.class);
} }
@Test @Test
......
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