Changeset 7888b17 for src/main/java
- Timestamp:
- 10/07/21 20:37:55 (3 years ago)
- Branches:
- master
- Children:
- b8a8d06
- Parents:
- ee0e297
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/it/finki/charitable/controller/DonationPostController.java
ree0e297 r7888b17 4 4 import it.finki.charitable.services.*; 5 5 import it.finki.charitable.util.FileUploadUtil; 6 import org.springframework.data.domain. Page;6 import org.springframework.data.domain.*; 7 7 import org.springframework.format.annotation.DateTimeFormat; 8 8 import org.springframework.security.core.context.SecurityContextHolder; … … 15 15 import java.io.IOException; 16 16 import java.time.LocalDate; 17 import java.util.ArrayList; 18 import java.util.Arrays; 19 import java.util.List; 20 import java.util.Objects; 17 import java.util.*; 21 18 import java.util.stream.Collectors; 22 19 … … 126 123 @RequestParam int page, 127 124 @RequestParam String sort, 128 @RequestParam(required = false,defaultValue = "") String order) { 129 Page<DonationPost> postList = donationPostService.findPaginated(page,6, sort, order, true); 125 @RequestParam(required = false, defaultValue = "desc") String order, 126 @RequestParam(required = false, defaultValue = "all") String groupBy) { 127 128 Sort s = Sort.by(sort); 129 s = order.equals("asc") ? s.ascending() : s.descending(); 130 Pageable pageable = PageRequest.of(page - 1, 6, s); 131 Page<DonationPost> postList; 132 postList = donationPostService.findPaginated(page, 6, sort, order, true); 133 134 if (!groupBy.equals("all")) { 135 List<DonationPost> allPosts = donationPostService.findAllByApproved(true); 136 137 if (sort.equals("title")) { 138 if (order.equals("asc")) { 139 allPosts.sort(Comparator.comparing(DonationPost::getTitle, (s1, s2) -> s1.compareToIgnoreCase(s2))); 140 } else { 141 allPosts.sort(Comparator.comparing(DonationPost::getTitle, (s1, s2) -> s2.compareToIgnoreCase(s1))); 142 } 143 } else if (sort.equals("dateDue")) { 144 if (order.equals("asc")) { 145 allPosts.sort(Comparator.comparing(DonationPost::getDateDue)); 146 } else { 147 allPosts.sort(Comparator.comparing(DonationPost::getDateDue).reversed()); 148 } 149 } else if (sort.equals("fundsNeeded")) { 150 if (order.equals("asc")) { 151 allPosts.sort(Comparator.comparing(DonationPost::getFundsNeeded)); 152 } else { 153 allPosts.sort(Comparator.comparing(DonationPost::getFundsNeeded).reversed()); 154 } 155 } 156 157 if (groupBy.equals("completed")) { 158 List<DonationPost> completed = allPosts.stream().filter(post -> { 159 double fundsCollected = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum(); 160 return fundsCollected >= post.getFundsNeeded(); 161 }).collect(Collectors.toList()); 162 163 int start = (int) pageable.getOffset(); 164 int end = Math.min((start + pageable.getPageSize()), completed.size()); 165 if (start <= end) { 166 postList = new PageImpl<>(completed.subList(start, end), pageable, completed.size()); 167 } 168 } else if (groupBy.equals("expired")) { 169 List<DonationPost> expired = allPosts.stream().filter(post -> { 170 double fundsCollected = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum(); 171 return LocalDate.now().isAfter(post.getDateDue()) && fundsCollected < post.getFundsNeeded(); 172 }).collect(Collectors.toList()); 173 174 int start = (int) pageable.getOffset(); 175 int end = Math.min((start + pageable.getPageSize()), expired.size()); 176 if (start <= end) { 177 postList = new PageImpl<>(expired.subList(start, end), pageable, expired.size()); 178 } 179 } 180 } 181 130 182 if (postList.getTotalElements() == 0) { 131 183 model.addAttribute("noPosts", true); … … 193 245 DonationPost donationPost = donationPostService.getById(postid); 194 246 ReportPost reportPost = reportPostService.findByDonationPost(donationPost); 195 if (reportPost == null) {247 if (reportPost == null) { 196 248 reportPost = new ReportPost(); 197 249 reportPost.setDonationPost(donationPost);
Note:
See TracChangeset
for help on using the changeset viewer.