diff --git a/src/main/java/NTNU/IDATT1002/models/Image.java b/src/main/java/NTNU/IDATT1002/models/Image.java index 9b916316bc1813d4a90980fd5549898c3958ddb6..bd29936ec2ccce6f878e75377d7223e3fc57e49a 100644 --- a/src/main/java/NTNU/IDATT1002/models/Image.java +++ b/src/main/java/NTNU/IDATT1002/models/Image.java @@ -27,13 +27,13 @@ public class Image { @NotBlank - private int imageID; + private Long imageID; @NotBlank - private int albumID; + private Long albumID; @NotBlank - private int metaDataID; + private Long metaDataID; @NotBlank @CreationTimestamp @@ -45,7 +45,7 @@ public class Image { public Image() { } - public Image(int imageID, int albumID, int metaDataId, Date uploadAt, String path) { + public Image(Long imageID, Long albumID, Long metaDataId, Date uploadAt, String path) { this.imageID = imageID; this.albumID = albumID; this.metaDataID = metaDataId; @@ -57,15 +57,15 @@ public class Image { this(image.getImageID(), image.getAlbumID(), image.getMetaDataID(), image.getUploadAt(), image.getPath()); } - public void setImageID(int imageID) { + public void setImageID(Long imageID) { this.imageID = imageID; } - public void setAlbumID(int albumID) { + public void setAlbumID(Long albumID) { this.albumID = albumID; } - public void setMetaDataID(int metaDataID) { + public void setMetaDataID(Long metaDataID) { this.metaDataID = metaDataID; } @@ -77,15 +77,15 @@ public class Image { this.path = path; } - public int getImageID() { + public Long getImageID() { return imageID; } - public int getAlbumID() { + public Long getAlbumID() { return albumID; } - public int getMetaDataID() { + public Long getMetaDataID() { return metaDataID; } diff --git a/src/main/java/NTNU/IDATT1002/repository/ImageRepository.java b/src/main/java/NTNU/IDATT1002/repository/ImageRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..8e7e91214cf80cf88aec35b110ceb52d94720e0c --- /dev/null +++ b/src/main/java/NTNU/IDATT1002/repository/ImageRepository.java @@ -0,0 +1,144 @@ +package NTNU.IDATT1002.repository; + +import NTNU.IDATT1002.models.Image; +import java.util.Optional; +import javax.persistence.EntityManager; +import java.util.List; + + +/** + * 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; + + /** + * 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) { + e.printStackTrace(); + } + + return Optional.empty(); + } + + /** + * Persists the given image + * + * @param image the image to persist + */ + + private void persist(Image image) { + entityManager.getTransaction().begin(); + entityManager.persist(image); + entityManager.getTransaction().commit(); + } + + /** + * Retrieves all instances of the type image. + * + * @return all entities + */ + public Optional<Image> update(Image image) { + return Optional.empty(); + } + + /** + * Retrieves all instances of type image. + * + * @return all saved image + */ + + public List<Image> findAll() { + return entityManager.createQuery("from Image").getResultList(); + } + + + /** + * 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(); + } + + /** + * 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(); + } + } + + /** + * 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); + + } + + /** + * 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 image) { + return findById(image.getImageID()).isPresent(); + } +}