Ignore:
Timestamp:
01/05/22 15:57:29 (3 years ago)
Author:
NikolaCenevski <cenevskinikola@…>
Branches:
master
Children:
6fa3d09
Parents:
881a233
Message:

part 2

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  
    1414
    1515import java.io.IOException;
     16import java.time.Duration;
    1617import java.time.LocalDate;
    1718import java.util.*;
     
    8788        post.setBankAccount(bankAccount);
    8889        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);
    8994
    9095        List<String> phoneNumbers = Arrays.asList(telekom, a1);
     
    169174                    allPosts.sort(Comparator.comparing(DonationPost::getFundsNeeded).reversed());
    170175                }
     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                }
    171182            }
    172183
    173184            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());
    178188
    179189                int start = (int) pageable.getOffset();
     
    213223        }
    214224
    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())) {
    216227            AppUser user = post.getUser();
    217228            Moderator moderator = post.getModerator();
     
    223234                model.addAttribute("moderatorLastName", moderator.getLastName());
    224235            }
    225             double total = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();
    226             model.addAttribute("total", total);
    227236        } else {
    228237            model.addAttribute("notFound", true);
     
    250259                return String.format("redirect:/post?postid=%d&error", postid);
    251260            }
    252             post.setFundsNeeded(donatedAmount);
    253261        } catch (NumberFormatException e) {
    254262            return String.format("redirect:/post?postid=%d&error", postid);
     
    259267
    260268        post.getFundsCollected().add(funds);
     269        post.setTotalFundsCollected(post.getTotalFundsCollected() + donatedAmount);
     270
     271        if(post.getRiskFactor() != 101) {
     272            post.setRiskFactor(getRisk(post));
     273        }
     274
    261275        donationPostService.save(post);
    262276
     
    295309    public AppUser addAttributes() {
    296310        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);
    298314        }
    299315        return null;
    300316    }
     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    }
    301339}
  • src/main/java/it/finki/charitable/controller/HomeController.java

    r881a233 rb8dc761  
    129129    @ModelAttribute("user")
    130130    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);
    133135        }
    134136        return null;
  • src/main/java/it/finki/charitable/controller/UserProfileController.java

    r881a233 rb8dc761  
    115115            fundsCollectedService.save(funds);
    116116            post.getFundsCollected().add(funds);
     117            post.setTotalFundsCollected(post.getTotalFundsCollected() + amount);
    117118            donationPostService.save(post);
    118119        }
     
    122123    @ModelAttribute("user")
    123124    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);
    126129        }
    127130        return null;
Note: See TracChangeset for help on using the changeset viewer.