Commit 81785ec1 by Rubén Ramírez

fix: [Excepciones]: Corregidas algunas funciones en repositorio a la hora de…

fix: [Excepciones]: Corregidas algunas funciones en repositorio a la hora de devolver null y Optional
parent 74bdda27
......@@ -4,13 +4,12 @@ import com.ujaen.tfg.mangaffinity.entidades.BibliotecaPersonal;
import com.ujaen.tfg.mangaffinity.entidades.BibliotecaPersonalRecurso;
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.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Repository
@Transactional
......@@ -34,37 +33,28 @@ public class RepositorioBibliotecaPersonalRecurso {
}
public void eliminarRecursoDeBiblioteca(Long bibliotecaPersonalId, Long 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) {
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();
buscarRecursoEnBiblioteca(bibliotecaPersonalId, recursoId).ifPresent(em::remove);
}
public BibliotecaPersonal obtenerBibliotecaPorId(Long bibliotecaPersonalId) {
return em.find(BibliotecaPersonal.class, bibliotecaPersonalId);
@Transactional(readOnly = true)
public Optional<BibliotecaPersonal> obtenerBibliotecaPorId(Long bibliotecaPersonalId) {
return Optional.ofNullable(em.find(BibliotecaPersonal.class, bibliotecaPersonalId));
}
public Recurso obtenerRecursoPorId(Long recursoId) {
return em.find(Recurso.class, recursoId);
@Transactional(readOnly = true)
public Optional<Recurso> obtenerRecursoPorId(Long recursoId) {
return Optional.ofNullable(em.find(Recurso.class, recursoId));
}
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",
@Transactional(readOnly = true)
public Optional<BibliotecaPersonalRecurso> buscarRecursoEnBiblioteca(Long bibliotecaPersonalId, Long recursoId) {
return em.createQuery(
"SELECT bpr FROM BibliotecaPersonalRecurso bpr WHERE bpr.bibliotecaPersonal.id = :bibliotecaPersonalId AND bpr.recurso.id = :recursoId",
BibliotecaPersonalRecurso.class)
.setParameter("bibliotecaPersonalId", bibliotecaPersonalId)
.setParameter("recursoId", recursoId)
.getResultList();
if (resultList.size() == 1) return resultList.get(0);
else return null;
.getResultStream()
.findFirst();
}
}
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