Commit ba747566 by Rubén Ramírez

fix: [UsuarioController]: Corregidas algunas funciones en el controlador a la…

fix: [UsuarioController]: Corregidas algunas funciones en el controlador a la hora de devolver el status
parent 73943933
......@@ -2,6 +2,7 @@ package com.ujaen.tfg.mangaffinity.rest;
import com.ujaen.tfg.mangaffinity.entidades.BibliotecaPersonal;
import com.ujaen.tfg.mangaffinity.entidades.Usuario;
import com.ujaen.tfg.mangaffinity.excepciones.NombreUsuarioYaCogido;
import com.ujaen.tfg.mangaffinity.excepciones.NombreUsuarioYaRegistrado;
import com.ujaen.tfg.mangaffinity.excepciones.UsuarioNoExiste;
import com.ujaen.tfg.mangaffinity.excepciones.UsuarioYaRegistrado;
......@@ -34,6 +35,14 @@ public class UsuariosController {
@ExceptionHandler(ConstraintViolationException.class)
public void mapeadoExcepcionConstraintViolationException() {}
/**
* Registra un nuevo usuario en el sistema.
*
* Devuelve:
* - 201 CREATED con DTOLoginRespuesta si el registro es exitoso.
* - 409 CONFLICT si el email o el nombre de usuario ya están en uso.
* - 500 INTERNAL SERVER ERROR en caso de error inesperado.
*/
@PostMapping("/")
public ResponseEntity<?> registrarUsuario(@RequestBody DTOUsuario dtoUsuario) {
try {
......@@ -42,55 +51,61 @@ public class UsuariosController {
return ResponseEntity.status(HttpStatus.CREATED).body(respuesta);
} catch (UsuarioYaRegistrado e) {
return ResponseEntity.status(HttpStatus.CONFLICT).body("correo");
} catch (NombreUsuarioYaRegistrado e) {
} catch (NombreUsuarioYaCogido e) {
return ResponseEntity.status(HttpStatus.CONFLICT).body("nombre de usuario");
} catch (Exception e) {
} catch (RuntimeException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error inesperado");
}
}
/**
* Inicia sesión en el sistema.
*
* Devuelve:
* - 200 OK con DTOLoginRespuesta si la autenticación es exitosa.
* - 400 BAD REQUEST si la contraseña no es válida.
* - 401 UNAUTHORIZED si las credenciales son incorrectas.
* - 500 INTERNAL SERVER ERROR en caso de error inesperado.
*/
@PostMapping("/{email}")
public ResponseEntity<?> iniciarSesion(@PathVariable String email, @RequestBody Map<String, String> clave) {
try {
String contrasenia = clave.get("clave");
if (contrasenia == null || contrasenia.isBlank()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
if (contrasenia == null || contrasenia.isBlank()) return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
DTOLoginRespuesta respuesta = servicioUsuarios.autenticarUsuario(email, contrasenia);
if (respuesta == null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
if (respuesta == null) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
return ResponseEntity.ok(respuesta);
} catch (SecurityException e) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
} catch (Exception e) {
} catch (RuntimeException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
/**
* Obtiene la biblioteca personal de un usuario.
*
* Devuelve:
* - 200 OK con DTOBibliotecaPersonal si la biblioteca existe.
* - 404 NOT FOUND si el usuario no tiene una biblioteca.
*/
@GetMapping("/{usuarioId}/biblioteca")
public ResponseEntity<DTOBibliotecaPersonal> obtenerBiblioteca(@PathVariable Long usuarioId) {
BibliotecaPersonal biblioteca = servicioUsuarios.obtenerBibliotecaDeUsuario(usuarioId);
if (biblioteca == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
return ResponseEntity.ok(new DTOBibliotecaPersonal(biblioteca));
}
/**
* Obtiene el ID de un usuario a partir de su correo electrónico.
*
* Devuelve:
* - 200 OK con el ID del usuario si existe.
* - 404 NOT FOUND si el usuario no está registrado.
*/
@GetMapping("/email/{email}")
public ResponseEntity<Long> obtenerIdPorEmail(@PathVariable String email) {
try {
Usuario usuario = servicioUsuarios.buscaUsuario(email);
return ResponseEntity.ok(usuario.getId());
} catch (UsuarioNoExiste e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
Usuario usuario = servicioUsuarios.buscaUsuario(email);
return ResponseEntity.ok(usuario.getId());
}
}
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