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;
import com.ujaen.tfg.mangaffinity.entidades.Recurso;
import com.ujaen.tfg.mangaffinity.excepciones.RecursoSinCategoria;
import jakarta.persistence.EntityManager;
import jakarta.persistence.NoResultException;
import jakarta.persistence.PersistenceContext;
import org.hibernate.NonUniqueResultException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
......@@ -17,6 +15,7 @@ import java.util.List;
@Repository
@Transactional
public class RepositorioBibliotecaPersonalRecurso {
@PersistenceContext
private EntityManager em;
......@@ -24,11 +23,10 @@ public class RepositorioBibliotecaPersonalRecurso {
em.persist(bibliotecaPersonalRecurso);
}
@Transactional(readOnly = true)
public List<BibliotecaPersonalRecurso> listarPorCategoria(Long bibliotecaPersonalId, Categoria categoria) {
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)
.setParameter("bibliotecaPersonalId", bibliotecaPersonalId)
.setParameter("categoria", categoria)
......@@ -36,26 +34,18 @@ public class RepositorioBibliotecaPersonalRecurso {
}
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("recursoId", recursoId)
.executeUpdate();
}
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("bibliotecaPersonalId", bibliotecaPersonalId)
.setParameter("recursoId", recursoId)
.executeUpdate();
if (updated == 0) {
throw new RecursoSinCategoria();
}
}
public BibliotecaPersonal obtenerBibliotecaPorId(Long bibliotecaPersonalId) {
......@@ -68,23 +58,13 @@ public class RepositorioBibliotecaPersonalRecurso {
public BibliotecaPersonalRecurso buscarRecursoEnBiblioteca(Long bibliotecaPersonalId, Long recursoId) {
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)
.setParameter("bibliotecaPersonalId", bibliotecaPersonalId)
.setParameter("recursoId", recursoId)
.getResultList(); // Usamos getResultList() para obtener todos los resultados
.getResultList();
if (resultList.size() == 1) {
return resultList.get(0); // Devuelve el único resultado
} 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
}
if (resultList.size() == 1) return resultList.get(0);
else return 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