|
4 | 4 | import kattsyn.dev.rentplace.dtos.ImageDTO; |
5 | 5 | import kattsyn.dev.rentplace.dtos.PropertyCreateEditDTO; |
6 | 6 | import kattsyn.dev.rentplace.dtos.PropertyDTO; |
| 7 | +import kattsyn.dev.rentplace.dtos.filters.PropertyFilterDTO; |
7 | 8 | import kattsyn.dev.rentplace.entities.Image; |
8 | 9 | import kattsyn.dev.rentplace.entities.Property; |
9 | 10 | import kattsyn.dev.rentplace.entities.User; |
10 | 11 | import kattsyn.dev.rentplace.enums.ImageType; |
11 | 12 | import kattsyn.dev.rentplace.enums.Role; |
| 13 | +import kattsyn.dev.rentplace.enums.SortType; |
12 | 14 | import kattsyn.dev.rentplace.exceptions.ForbiddenException; |
13 | 15 | import kattsyn.dev.rentplace.exceptions.NotFoundException; |
14 | 16 | import kattsyn.dev.rentplace.mappers.ImageMapper; |
|
17 | 19 | import kattsyn.dev.rentplace.services.ImageService; |
18 | 20 | import kattsyn.dev.rentplace.services.PropertyService; |
19 | 21 | import kattsyn.dev.rentplace.services.UserService; |
| 22 | +import kattsyn.dev.rentplace.specifications.PropertySpecification; |
20 | 23 | import kattsyn.dev.rentplace.utils.PathResolver; |
21 | 24 | import lombok.RequiredArgsConstructor; |
| 25 | +import org.springframework.data.domain.Sort; |
22 | 26 | import org.springframework.stereotype.Service; |
23 | 27 | import org.springframework.web.multipart.MultipartFile; |
24 | 28 |
|
@@ -69,6 +73,24 @@ public List<PropertyDTO> findAllByOwnerEmail(String email) { |
69 | 73 | return propertyMapper.fromProperties(propertyRepository.findAllByOwnerEmail(email)); |
70 | 74 | } |
71 | 75 |
|
| 76 | + @Override |
| 77 | + public List<PropertyDTO> findAllByFilter(PropertyFilterDTO filter) { |
| 78 | + return propertyMapper.fromProperties( |
| 79 | + propertyRepository.findAll(new PropertySpecification(filter), buildSort(filter.getSortType())) |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + private Sort buildSort(SortType sortType) { |
| 84 | + if (sortType == null) return Sort.unsorted(); |
| 85 | + |
| 86 | + return switch (sortType) { |
| 87 | + case MOST_OLD -> Sort.by(Sort.Order.asc("propertyId")); |
| 88 | + case MOST_RECENT -> Sort.by(Sort.Order.desc("propertyId")); |
| 89 | + case MOST_EXPENSIVE -> Sort.by(Sort.Order.desc("cost")); |
| 90 | + case MOST_CHEAP -> Sort.by(Sort.Order.asc("cost")); |
| 91 | + }; |
| 92 | + } |
| 93 | + |
72 | 94 | @Override |
73 | 95 | @Transactional |
74 | 96 | public Property getPropertyById(long id) { |
|
0 commit comments