Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Rubén Ramírez
/
MangAffinity
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
06a23eb2
authored
Mar 28, 2025
by
Rubén Ramírez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
fix: [UsuariosController]: Añadidos un par de métodos para más claridad
parent
1a1fa895
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
10 deletions
src/main/java/com/ujaen/tfg/mangaffinity/rest/UsuariosController.java
src/main/java/com/ujaen/tfg/mangaffinity/rest/UsuariosController.java
View file @
06a23eb2
...
...
@@ -3,14 +3,12 @@ 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
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.DTOBibliotecaPersonal
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.DTOLoginRespuesta
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.DTOUsuario
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.Mapper
;
import
com.ujaen.tfg.mangaffinity.seguridad.JwtUtil
;
import
com.ujaen.tfg.mangaffinity.servicios.ServicioUsuarios
;
import
jakarta.validation.ConstraintViolationException
;
import
jakarta.validation.Valid
;
...
...
@@ -18,7 +16,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.Map
;
@RestController
...
...
@@ -44,7 +41,7 @@ public class UsuariosController {
* - 500 INTERNAL SERVER ERROR en caso de error inesperado.
*/
@PostMapping
(
"/"
)
public
ResponseEntity
<?>
registrarUsuario
(
@RequestBody
DTOUsuario
dtoUsuario
)
{
public
ResponseEntity
<?>
registrarUsuario
(
@RequestBody
@Valid
DTOUsuario
dtoUsuario
)
{
try
{
Usuario
usuario
=
mapper
.
entity
(
dtoUsuario
);
DTOLoginRespuesta
respuesta
=
servicioUsuarios
.
crearUsuario
(
usuario
);
...
...
@@ -88,11 +85,19 @@ public class UsuariosController {
* Devuelve:
* - 200 OK con DTOBibliotecaPersonal si la biblioteca existe.
* - 404 NOT FOUND si el usuario no tiene una biblioteca.
* - 500 INTERNAL SERVER ERROR en caso de error inesperado.
*/
@GetMapping
(
"/{usuarioId}/biblioteca"
)
public
ResponseEntity
<
DTOBibliotecaPersonal
>
obtenerBiblioteca
(
@PathVariable
Long
usuarioId
)
{
BibliotecaPersonal
biblioteca
=
servicioUsuarios
.
obtenerBibliotecaDeUsuario
(
usuarioId
);
return
ResponseEntity
.
ok
(
new
DTOBibliotecaPersonal
(
biblioteca
));
public
ResponseEntity
<?>
obtenerBiblioteca
(
@PathVariable
Long
usuarioId
)
{
try
{
BibliotecaPersonal
biblioteca
=
servicioUsuarios
.
obtenerBibliotecaDeUsuario
(
usuarioId
);
if
(
biblioteca
==
null
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
NOT_FOUND
).
body
(
"No se encontró la biblioteca"
);
}
return
ResponseEntity
.
ok
(
new
DTOBibliotecaPersonal
(
biblioteca
));
}
catch
(
RuntimeException
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
INTERNAL_SERVER_ERROR
).
body
(
"Error inesperado"
);
}
}
/**
...
...
@@ -101,11 +106,18 @@ public class UsuariosController {
* Devuelve:
* - 200 OK con el ID del usuario si existe.
* - 404 NOT FOUND si el usuario no está registrado.
* - 500 INTERNAL SERVER ERROR en caso de error inesperado.
*/
@GetMapping
(
"/email/{email}"
)
public
ResponseEntity
<
Long
>
obtenerIdPorEmail
(
@PathVariable
String
email
)
{
Usuario
usuario
=
servicioUsuarios
.
buscaUsuario
(
email
);
return
ResponseEntity
.
ok
(
usuario
.
getId
());
public
ResponseEntity
<?>
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
();
}
catch
(
RuntimeException
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
INTERNAL_SERVER_ERROR
).
build
();
}
}
}
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