Changeset b8dc761 for src/main/java/it/finki/charitable/controller
- Timestamp:
- 01/05/22 15:57:29 (3 years ago)
- Branches:
- master
- Children:
- 6fa3d09
- Parents:
- 881a233
- Location:
- src/main/java/it/finki/charitable/controller
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/it/finki/charitable/controller/DonationPostController.java
r881a233 rb8dc761 14 14 15 15 import java.io.IOException; 16 import java.time.Duration; 16 17 import java.time.LocalDate; 17 18 import java.util.*; … … 87 88 post.setBankAccount(bankAccount); 88 89 post.setApproved(false); 90 post.setCreatedAt(LocalDate.now()); 91 long totalDays = Duration.between(post.getCreatedAt().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays(); 92 if(totalDays < 10) 93 post.setRiskFactor(0); 89 94 90 95 List<String> phoneNumbers = Arrays.asList(telekom, a1); … … 169 174 allPosts.sort(Comparator.comparing(DonationPost::getFundsNeeded).reversed()); 170 175 } 176 } else if (sort.equals("riskFactor")) { 177 if (order.equals("asc")) { 178 allPosts.sort(Comparator.comparing(DonationPost::getRiskFactor)); 179 } else { 180 allPosts.sort(Comparator.comparing(DonationPost::getRiskFactor).reversed()); 181 } 171 182 } 172 183 173 184 if (groupBy.equals("completed")) { 174 List<DonationPost> completed = allPosts.stream().filter(post -> { 175 double fundsCollected = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum(); 176 return fundsCollected >= post.getFundsNeeded(); 177 }).collect(Collectors.toList()); 185 List<DonationPost> completed = allPosts.stream() 186 .filter(post -> post.getTotalFundsCollected() >= post.getFundsNeeded()) 187 .collect(Collectors.toList()); 178 188 179 189 int start = (int) pageable.getOffset(); … … 213 223 } 214 224 215 if (post.getApproved() || (post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName()) && !post.getApproved())) { 225 AppUser currentUser = (AppUser) model.getAttribute("user"); 226 if (post.getApproved() || (post.getUser().getUsername().equals(currentUser.getUsername()) && !post.getApproved())) { 216 227 AppUser user = post.getUser(); 217 228 Moderator moderator = post.getModerator(); … … 223 234 model.addAttribute("moderatorLastName", moderator.getLastName()); 224 235 } 225 double total = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();226 model.addAttribute("total", total);227 236 } else { 228 237 model.addAttribute("notFound", true); … … 250 259 return String.format("redirect:/post?postid=%d&error", postid); 251 260 } 252 post.setFundsNeeded(donatedAmount);253 261 } catch (NumberFormatException e) { 254 262 return String.format("redirect:/post?postid=%d&error", postid); … … 259 267 260 268 post.getFundsCollected().add(funds); 269 post.setTotalFundsCollected(post.getTotalFundsCollected() + donatedAmount); 270 271 if(post.getRiskFactor() != 101) { 272 post.setRiskFactor(getRisk(post)); 273 } 274 261 275 donationPostService.save(post); 262 276 … … 295 309 public AppUser addAttributes() { 296 310 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 297 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 311 312 String email = SecurityContextHolder.getContext().getAuthentication().getName(); 313 return userService.loadUserByUsername(email); 298 314 } 299 315 return null; 300 316 } 317 318 private Integer getRisk(DonationPost post) { 319 float dailyAverage = post.getTotalFundsCollected() / (Duration.between(post.getCreatedAt().atTime(0, 0, 0), LocalDate.now().atTime(0, 0, 0)).toDays()+1); 320 float neededAverage = (post.getFundsNeeded() - post.getTotalFundsCollected()) / (Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays()+1); 321 322 System.out.println(dailyAverage + " " + neededAverage); 323 int risk = (int) (dailyAverage / neededAverage * 100); 324 325 if(risk > 100) { 326 risk = 100; 327 } 328 329 if(Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays() == 0) { 330 risk = 0; 331 } 332 333 if(post.getFundsNeeded() <= post.getTotalFundsCollected()) { 334 risk = 101; 335 } 336 337 return risk; 338 } 301 339 } -
src/main/java/it/finki/charitable/controller/HomeController.java
r881a233 rb8dc761 129 129 @ModelAttribute("user") 130 130 public AppUser addAttributes() { 131 if(SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 132 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 131 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 132 String email = SecurityContextHolder.getContext().getAuthentication().getName(); 133 System.out.println(email); 134 return userService.loadUserByUsername(email); 133 135 } 134 136 return null; -
src/main/java/it/finki/charitable/controller/UserProfileController.java
r881a233 rb8dc761 115 115 fundsCollectedService.save(funds); 116 116 post.getFundsCollected().add(funds); 117 post.setTotalFundsCollected(post.getTotalFundsCollected() + amount); 117 118 donationPostService.save(post); 118 119 } … … 122 123 @ModelAttribute("user") 123 124 public AppUser addAttributes() { 124 if(SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 125 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 125 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 126 127 String email = SecurityContextHolder.getContext().getAuthentication().getName(); 128 return userService.loadUserByUsername(email); 126 129 } 127 130 return null;
Note:
See TracChangeset
for help on using the changeset viewer.