Skip to content
Snippets Groups Projects
Commit bf8a6be5 authored by Stian Fjæran Mogen's avatar Stian Fjæran Mogen
Browse files

Removed duplicate search results

removed duplicate search results creating method searchresult which uses the new method remove duplicates
creates one search result that returns a list with no duplicate images

- changed name from searchImageByTags to findAllByTags for continuity
parent 49295d7b
No related branches found
No related tags found
No related merge requests found
Pipeline #78184 failed
......@@ -49,11 +49,12 @@ public class ImageRepository extends GenericRepository<Image, Long> {
.getResultList();
}
public List<Image> searchImageByTags(String tag){
public List<Image> findAllByTags(String tag){
return entityManager.createNamedQuery(IMAGE_FIND_BY_TAG, Image.class)
.setParameter("name",tag)
.getResultList();
}
}
......
......@@ -125,7 +125,29 @@ public class ImageService {
.collect(Collectors.toList());
}
public List<Image> searchImageWithTags(String query){
return imageRepository.searchImageByTags(query);
/**
* Searches images by tags and username, and merges the two list into one with all images
* uses removeDuplicates list to return a list with no duplicate images
* @param query
* @return a list with no duplicate images
*/
public List<Image> searchResult(String query){
List<Image> allFound = new ArrayList<>();
List<Image> byTags = imageRepository.findAllByTags(query);
List<Image> byUsername = imageRepository.findAllByUsername(query);
allFound.addAll(byTags);
allFound.addAll(byUsername);
return removeDuplicates(allFound);
}
/**
* takes a list and removes all duplicate elements
* @param images
* @return list without duplicates
*/
public List<Image> removeDuplicates(List<Image> images){
return images.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