From 15c3f45c9b1d14004a06a5a3b77dccbef321b2ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Brodin=20=C3=98stby?= <larsbost@stud.ntnu.no> Date: Thu, 19 Mar 2020 17:11:06 +0100 Subject: [PATCH] Fixed image repository --- .../IDATT1002/repository/ImageRepository.java | 113 +++++++++++++++--- 1 file changed, 99 insertions(+), 14 deletions(-) diff --git a/src/main/java/NTNU/IDATT1002/repository/ImageRepository.java b/src/main/java/NTNU/IDATT1002/repository/ImageRepository.java index 592024eb..7cf78359 100644 --- a/src/main/java/NTNU/IDATT1002/repository/ImageRepository.java +++ b/src/main/java/NTNU/IDATT1002/repository/ImageRepository.java @@ -6,54 +6,139 @@ import javax.persistence.EntityManager; import java.util.List; -public class ImageRepository implements Repository<Image, Long> { +/** + * Image Repository. + * <p> + * Implements {@link Repository} which supports regular Create, Read, Update and Delete operations. + * + * @author Lars Østby + * @version 1.0 19.03.20 + * @see NTNU.IDATT1002.repository.Repository + */ + +public class ImageRepository implements Repository<Image, Long> { private EntityManager entityManager; - public Optional<Image> save (Image image){ - try{ + /** + * Constructor to inject {@link EntityManager} dependency. + * + * @param entityManager the entity manager to utilize. + */ + + public ImageRepository(EntityManager entityManager) { + this.entityManager = entityManager; + } + + /** + * Saves a given image and returns the saved instance. + * + * @param image the image album to save + * @return the saved image + */ + + public Optional<Image> save(Image image) { + try { persist(image); return Optional.of(image); - } catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } return Optional.empty(); } - private void persist(Image image){ + /** + * Persists the given image + * + * @param image the image to persist + */ + + private void persist(Image image) { entityManager.getTransaction().begin(); entityManager.persist(image); entityManager.getTransaction().commit(); } - public Optional<Image> update(Image image){ + /** + * Retrieves all instances of the type image. + * + * @return all entities + */ + public Optional<Image> update(Image image) { return Optional.empty(); } - public List<Image> findAll(){ + /** + * Retrieves all instances of type image. + * + * @return all saved image + */ + + public List<Image> findAll() { return entityManager.createQuery("from Image").getResultList(); } - public Optional<Image> findById(Long id){ + + /** + * Retrieves an image with the given id. + * + * @param id the if of the image to find + * @return the entity with the given id if found, else Optional.empty() + */ + + public Optional<Image> findById(Long id) { Image image = entityManager.find(Image.class, id); return image != null ? Optional.of(image) : Optional.empty(); } - public void delete(Image entity){ - + /** + * Deletes the given image + * + * @param image the image to delete + */ + + public void delete(Image image) { + try { + entityManager.getTransaction().begin(); + entityManager.remove(image); + entityManager.getTransaction().commit(); + } catch (Exception e) { + e.printStackTrace(); + } } - public void deleteById(Long aLong){ + /** + * Deletes an image with the given id. + * + * @param id the id of the image to delete + */ + + public void deleteById(Long id) { + Optional<Image> image = findById(id); + image.ifPresent(this::delete); } - public long count(){ - return 0; + /** + * Return the number of images + * + * @return the number of images + */ + + public long count() { + return findAll().size(); } + /** + * Return whether the given image exists. + * + * @param image image album to check existence for + * @return true if the image album exist, else false + */ + @Override - public boolean exists(Image entity) { + public boolean exists(Image image) { return false; } } -- GitLab