Skip to content
Snippets Groups Projects
Commit 24d934b7 authored by Stian Fjæran Mogen's avatar Stian Fjæran Mogen Committed by Eirik Steira
Browse files

album search methods

added methods for named query search with album
parent 9e3deaa7
No related branches found
No related tags found
1 merge request!165Weekly merge to Master
......@@ -16,9 +16,11 @@ import java.util.List;
public class AlbumRepository extends AbstractRepository<Album, Long> {
/**
* Mapping to @NamedQuery 'find all albums by users username' defined in {@link Album}
* Mapping to @NamedQuery 'find all albums by username, tags, and title' defined in {@link Album}
*/
public static final String ALBUM_FIND_BY_USERNAME = "Album.findAllByUsername";
public static final String ALBUM_FIND_BY_TAGS = "Album.findByTags";
public static final String ALBUM_FIND_BY_TITLE = "Album.findByTitle";
/**
* Constructor to inject {@link EntityManager} dependency and sets the class type to {@link Album}
......@@ -42,4 +44,17 @@ public class AlbumRepository extends AbstractRepository<Album, Long> {
.getResultList();
}
public List<Album> findAllByTags(String tag){
return entityManager.createNamedQuery(ALBUM_FIND_BY_TAGS, Album.class)
.setParameter("name",tag)
.getResultList();
}
public List<Album> findAllByTitle(String title){
return entityManager.createNamedQuery(ALBUM_FIND_BY_TITLE, Album.class)
.setParameter("title", title)
.getResultList();
}
}
......@@ -22,7 +22,7 @@ import java.util.List;
public class ImageRepository extends AbstractRepository<Image, Long> {
/**
* Mapping to @NamedQuery 'find all albums by users username' defined in {@link Image}
* Mapping to @NamedQuery 'find all albums by username and tags' defined in {@link Image}
*/
public static final String IMAGE_FIND_BY_USERNAME = "Image.findAllByUsername";
public static final String IMAGE_FIND_BY_TAG = "Image.findByTags";
......
......@@ -12,6 +12,7 @@ import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
......@@ -114,4 +115,32 @@ public class AlbumService {
return document;
}
/**
* Takes in a string and searched through all album by tags, username and title to find results
* @param query
* @return list of results without duplocates
*/
public List<Album> searchResult(String query){
List<Album> allFound = new ArrayList<>();
List<Album> byTags = albumRepository.findAllByTags(query);
List<Album> byUsername = albumRepository.findAllByUsername(query);
List<Album> byTitle = albumRepository.findAllByTitle(query);
allFound.addAll(byTags);
allFound.addAll(byUsername);
allFound.addAll(byTitle);
return removeDuplicates(allFound);
}
/**
* takes a list and removes all duplicate elements
* @param albums
* @return list without duplicates
*/
public List<Album> removeDuplicates(List<Album> albums){
return albums.stream().distinct().collect(Collectors.toList());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment