Commit 05cd5c07 by Rubén Ramírez

feat: [RepositorioBibliotecaPersonalRecurso]: Corregidos algunos métodos…

feat: [RepositorioBibliotecaPersonalRecurso]: Corregidos algunos métodos eliminando partes innecesarias
parent 35ab3b87
...@@ -6,9 +6,7 @@ import com.ujaen.tfg.mangaffinity.entidades.Categoria; ...@@ -6,9 +6,7 @@ import com.ujaen.tfg.mangaffinity.entidades.Categoria;
import com.ujaen.tfg.mangaffinity.entidades.Recurso; import com.ujaen.tfg.mangaffinity.entidades.Recurso;
import com.ujaen.tfg.mangaffinity.excepciones.RecursoSinCategoria; import com.ujaen.tfg.mangaffinity.excepciones.RecursoSinCategoria;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.NoResultException;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
import org.hibernate.NonUniqueResultException;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -17,6 +15,7 @@ import java.util.List; ...@@ -17,6 +15,7 @@ import java.util.List;
@Repository @Repository
@Transactional @Transactional
public class RepositorioBibliotecaPersonalRecurso { public class RepositorioBibliotecaPersonalRecurso {
@PersistenceContext @PersistenceContext
private EntityManager em; private EntityManager em;
...@@ -24,11 +23,10 @@ public class RepositorioBibliotecaPersonalRecurso { ...@@ -24,11 +23,10 @@ public class RepositorioBibliotecaPersonalRecurso {
em.persist(bibliotecaPersonalRecurso); em.persist(bibliotecaPersonalRecurso);
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<BibliotecaPersonalRecurso> listarPorCategoria(Long bibliotecaPersonalId, Categoria categoria) { public List<BibliotecaPersonalRecurso> listarPorCategoria(Long bibliotecaPersonalId, Categoria categoria) {
return em.createQuery( return em.createQuery(
"SELECT bpr FROM BibliotecaPersonalRecurso bpr WHERE bpr.bibliotecaPersonal.id = :bibliotecaPersonalId AND bpr.categoria = :categoria", "SELECT bpr FROM BibliotecaPersonalRecurso bpr WHERE bpr.bibliotecaPersonal.id = :bibliotecaPersonalId AND bpr.categoria = :categoria",
BibliotecaPersonalRecurso.class) BibliotecaPersonalRecurso.class)
.setParameter("bibliotecaPersonalId", bibliotecaPersonalId) .setParameter("bibliotecaPersonalId", bibliotecaPersonalId)
.setParameter("categoria", categoria) .setParameter("categoria", categoria)
...@@ -36,26 +34,18 @@ public class RepositorioBibliotecaPersonalRecurso { ...@@ -36,26 +34,18 @@ public class RepositorioBibliotecaPersonalRecurso {
} }
public void eliminarRecursoDeBiblioteca(Long bibliotecaPersonalId, Long recursoId) { public void eliminarRecursoDeBiblioteca(Long bibliotecaPersonalId, Long recursoId) {
int deleted = em.createQuery("DELETE FROM BibliotecaPersonalRecurso bpr WHERE bpr.bibliotecaPersonal.id = :bibliotecaPersonalId AND bpr.recurso.id = :recursoId") em.createQuery("DELETE FROM BibliotecaPersonalRecurso bpr WHERE bpr.bibliotecaPersonal.id = :bibliotecaPersonalId AND bpr.recurso.id = :recursoId")
.setParameter("bibliotecaPersonalId", bibliotecaPersonalId) .setParameter("bibliotecaPersonalId", bibliotecaPersonalId)
.setParameter("recursoId", recursoId) .setParameter("recursoId", recursoId)
.executeUpdate(); .executeUpdate();
} }
public void actualizarCategoriaRecurso(Long bibliotecaPersonalId, Long recursoId, Categoria nuevaCategoria) { public void actualizarCategoriaRecurso(Long bibliotecaPersonalId, Long recursoId, Categoria nuevaCategoria) {
int updated = em.createQuery("UPDATE BibliotecaPersonalRecurso bpr SET bpr.categoria = :nuevaCategoria WHERE bpr.bibliotecaPersonal.id = :bibliotecaPersonalId AND bpr.recurso.id = :recursoId") em.createQuery("UPDATE BibliotecaPersonalRecurso bpr SET bpr.categoria = :nuevaCategoria WHERE bpr.bibliotecaPersonal.id = :bibliotecaPersonalId AND bpr.recurso.id = :recursoId")
.setParameter("nuevaCategoria", nuevaCategoria) .setParameter("nuevaCategoria", nuevaCategoria)
.setParameter("bibliotecaPersonalId", bibliotecaPersonalId) .setParameter("bibliotecaPersonalId", bibliotecaPersonalId)
.setParameter("recursoId", recursoId) .setParameter("recursoId", recursoId)
.executeUpdate(); .executeUpdate();
if (updated == 0) {
throw new RecursoSinCategoria();
}
} }
public BibliotecaPersonal obtenerBibliotecaPorId(Long bibliotecaPersonalId) { public BibliotecaPersonal obtenerBibliotecaPorId(Long bibliotecaPersonalId) {
...@@ -68,23 +58,13 @@ public class RepositorioBibliotecaPersonalRecurso { ...@@ -68,23 +58,13 @@ public class RepositorioBibliotecaPersonalRecurso {
public BibliotecaPersonalRecurso buscarRecursoEnBiblioteca(Long bibliotecaPersonalId, Long recursoId) { public BibliotecaPersonalRecurso buscarRecursoEnBiblioteca(Long bibliotecaPersonalId, Long recursoId) {
List<BibliotecaPersonalRecurso> resultList = em.createQuery( List<BibliotecaPersonalRecurso> resultList = em.createQuery(
"SELECT bpr FROM BibliotecaPersonalRecurso bpr WHERE bpr.bibliotecaPersonal.id = :bibliotecaPersonalId AND bpr.recurso.id = :recursoId", "SELECT bpr FROM BibliotecaPersonalRecurso bpr WHERE bpr.bibliotecaPersonal.id = :bibliotecaPersonalId AND bpr.recurso.id = :recursoId",
BibliotecaPersonalRecurso.class) BibliotecaPersonalRecurso.class)
.setParameter("bibliotecaPersonalId", bibliotecaPersonalId) .setParameter("bibliotecaPersonalId", bibliotecaPersonalId)
.setParameter("recursoId", recursoId) .setParameter("recursoId", recursoId)
.getResultList(); // Usamos getResultList() para obtener todos los resultados .getResultList();
if (resultList.size() == 1) { if (resultList.size() == 1) return resultList.get(0);
return resultList.get(0); // Devuelve el único resultado else return null;
} else if (resultList.size() > 1) {
// Aquí puedes decidir qué hacer si hay más de un resultado
throw new IllegalStateException("Se encontraron múltiples recursos para esta biblioteca y recurso.");
} else {
return null; // Si no hay resultados, devuelve null
}
} }
} }
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