Commit 4da14105 by Rubén Ramírez

feat: [BibliotecaPersonalController]: Creadas funciones en el controlador con su test

parent 25ef7c1b
...@@ -50,16 +50,24 @@ public class BibliotecaPersonalController { ...@@ -50,16 +50,24 @@ public class BibliotecaPersonalController {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
} }
boolean yaExiste = servicioBibliotecaPersonal.listarPorCategoria(biblioteca.getId(), dtoRecursoEnBiblioteca.getCategoria())
.stream()
.anyMatch(bpr -> bpr.getRecurso().getId().equals(recursoId));
if (yaExiste) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
}
servicioBibliotecaPersonal.anadirRecursoBiblioteca(biblioteca, recurso, dtoRecursoEnBiblioteca.getCategoria()); servicioBibliotecaPersonal.anadirRecursoBiblioteca(biblioteca, recurso, dtoRecursoEnBiblioteca.getCategoria());
return ResponseEntity.status(HttpStatus.CREATED).build(); return ResponseEntity.status(HttpStatus.CREATED).build();
} catch (RecursoNoExiste e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
} catch (Exception e) { } catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
} }
} }
@GetMapping("/{usuarioId}/recursos/categoria/{categoria}") @GetMapping("/{usuarioId}/recursos/categoria/{categoria}")
public ResponseEntity<List<DTORecursoEnBiblioteca>> listarRecursosPorCategoria(@PathVariable Long usuarioId, @PathVariable Categoria categoria) { public ResponseEntity<List<DTORecursoEnBiblioteca>> listarRecursosPorCategoria(@PathVariable Long usuarioId, @PathVariable Categoria categoria) {
try { try {
...@@ -89,7 +97,7 @@ public class BibliotecaPersonalController { ...@@ -89,7 +97,7 @@ public class BibliotecaPersonalController {
} }
servicioBibliotecaPersonal.eliminarRecurso(biblioteca.getId(), recursoId); servicioBibliotecaPersonal.eliminarRecurso(biblioteca.getId(), recursoId);
return ResponseEntity.status(HttpStatus.OK).body("Recurso eliminado correctamente"); return ResponseEntity.status(HttpStatus.OK).build();
} catch (Exception e) { } catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
} }
...@@ -107,7 +115,7 @@ public class BibliotecaPersonalController { ...@@ -107,7 +115,7 @@ public class BibliotecaPersonalController {
servicioBibliotecaPersonal.modificarCategoria(biblioteca.getId(), recursoId, dtoRecurso.getCategoria()); servicioBibliotecaPersonal.modificarCategoria(biblioteca.getId(), recursoId, dtoRecurso.getCategoria());
return ResponseEntity.status(HttpStatus.OK).build(); return ResponseEntity.status(HttpStatus.OK).build();
} catch (Exception e) { } catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error interno"); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
} }
} }
......
...@@ -28,6 +28,7 @@ public class ServicioSeguridad { ...@@ -28,6 +28,7 @@ public class ServicioSeguridad {
.httpBasic(httpBasic -> httpBasic.realmName("mangaffinity")) .httpBasic(httpBasic -> httpBasic.realmName("mangaffinity"))
.authorizeHttpRequests(request -> request .authorizeHttpRequests(request -> request
.requestMatchers(HttpMethod.POST, "/usuarios/{email}").permitAll() .requestMatchers(HttpMethod.POST, "/usuarios/{email}").permitAll()
.requestMatchers(HttpMethod.GET, "/usuarios/email/{email}").permitAll()
.requestMatchers(HttpMethod.POST, "/usuarios/").permitAll() .requestMatchers(HttpMethod.POST, "/usuarios/").permitAll()
.requestMatchers(HttpMethod.GET, "/recursos/titulo/**").permitAll() .requestMatchers(HttpMethod.GET, "/recursos/titulo/**").permitAll()
.requestMatchers(HttpMethod.GET, "/recursos/autor/**").permitAll() .requestMatchers(HttpMethod.GET, "/recursos/autor/**").permitAll()
...@@ -40,12 +41,13 @@ public class ServicioSeguridad { ...@@ -40,12 +41,13 @@ public class ServicioSeguridad {
.requestMatchers(HttpMethod.DELETE, "/recursos/{id}").hasAuthority("ROLE_ADMIN") .requestMatchers(HttpMethod.DELETE, "/recursos/{id}").hasAuthority("ROLE_ADMIN")
.requestMatchers(HttpMethod.POST, "/recursos/{id}/capitulos").hasAuthority("ROLE_ADMIN") .requestMatchers(HttpMethod.POST, "/recursos/{id}/capitulos").hasAuthority("ROLE_ADMIN")
.requestMatchers(HttpMethod.GET, "/recursos/{id}/capitulos").permitAll() .requestMatchers(HttpMethod.GET, "/recursos/{id}/capitulos").permitAll()
.requestMatchers(HttpMethod.GET, "/usuarios/{usuarioId}/biblioteca").permitAll()
// 🔹 Protección de las rutas de la biblioteca (solo usuarios autenticados)
.requestMatchers(HttpMethod.POST, "/biblioteca/{usuarioId}/recursos/{recursoId}/categoria").authenticated() .requestMatchers(HttpMethod.POST, "/biblioteca/{usuarioId}/recursos/{recursoId}/categoria").authenticated()
.requestMatchers(HttpMethod.GET, "/biblioteca/{usuarioId}/recursos/categoria/{categoria}").authenticated() .requestMatchers(HttpMethod.GET, "/biblioteca/{usuarioId}/recursos/categoria/{categoria}").authenticated()
.requestMatchers(HttpMethod.DELETE, "/biblioteca/{usuarioId}/recursos/{recursoId}").authenticated() // ✅ Nueva regla .requestMatchers(HttpMethod.DELETE, "/biblioteca/{usuarioId}/recursos/{recursoId}").authenticated()
.requestMatchers(HttpMethod.PUT, "/biblioteca/{usuarioId}/recursos/{recursoId}/categoria").authenticated() // ✅ Nueva regla .requestMatchers(HttpMethod.PUT, "/biblioteca/{usuarioId}/recursos/{recursoId}/categoria").authenticated()
.anyRequest().authenticated() .anyRequest().authenticated()
) )
......
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