Skip to content

Commit df4e504

Browse files
authored
Merge pull request #65 from TP-RENTPLACE/feature/return-sorted-images
feat: add images sorting for findAll methods
2 parents ca2a988 + bb309cc commit df4e504

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/PropertyServiceImpl.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,28 @@ public boolean allowedToCreatePropertyOrAdmin(PropertyCreateEditDTO propertyCrea
6666
@Transactional
6767
@Override
6868
public List<PropertyDTO> findAll() {
69-
return propertyMapper.fromProperties(propertyRepository.findAllWithRelations());
69+
return returnWithSortedImages(propertyMapper.fromProperties(propertyRepository.findAllWithRelations()));
7070
}
7171

7272
@Override
7373
public List<PropertyDTO> findAllByOwnerEmail(String email) {
74-
return propertyMapper.fromProperties(propertyRepository.findAllByOwnerEmail(email));
74+
return returnWithSortedImages(propertyMapper.fromProperties(propertyRepository.findAllByOwnerEmail(email)));
7575
}
7676

7777
@Override
7878
public List<PropertyDTO> findAllByFilter(PropertyFilterDTO filter) {
79-
return propertyMapper.fromProperties(
79+
return returnWithSortedImages(propertyMapper.fromProperties(
8080
propertyRepository.findAll(new PropertySpecification(filter), buildSort(filter.getSortType()))
81-
);
81+
));
82+
}
83+
84+
private List<PropertyDTO> returnWithSortedImages(List<PropertyDTO> propertyDTOS) {
85+
propertyDTOS.forEach(propertyDTO
86+
-> propertyDTO
87+
.getImagesDTOs()
88+
.sort(Comparator
89+
.comparing(ImageDTO::isPreviewImage).reversed().thenComparing(ImageDTO::getImageId)));
90+
return propertyDTOS;
8291
}
8392

8493
private Sort buildSort(SortType sortType) {

0 commit comments

Comments
 (0)