[5577566] | 1 | package it.finki.charitable.controller;
|
---|
| 2 |
|
---|
[f8007b3] | 3 | import it.finki.charitable.entities.*;
|
---|
[5577566] | 4 | import it.finki.charitable.security.PasswordEncoder;
|
---|
| 5 | import it.finki.charitable.services.DonationPostService;
|
---|
[f8007b3] | 6 | import it.finki.charitable.services.FundsCollectedService;
|
---|
[5577566] | 7 | import it.finki.charitable.services.UserService;
|
---|
| 8 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
| 9 | import org.springframework.stereotype.Controller;
|
---|
| 10 | import org.springframework.ui.Model;
|
---|
| 11 | import org.springframework.web.bind.annotation.ModelAttribute;
|
---|
| 12 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
| 13 | import org.springframework.web.bind.annotation.RequestMethod;
|
---|
| 14 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
| 15 |
|
---|
[6fa3d09] | 16 | import java.time.Duration;
|
---|
| 17 | import java.time.LocalDate;
|
---|
| 18 | import java.time.LocalDateTime;
|
---|
[5577566] | 19 | import java.util.List;
|
---|
| 20 |
|
---|
| 21 | @Controller
|
---|
| 22 | public class UserProfileController {
|
---|
| 23 |
|
---|
| 24 | private final UserService userService;
|
---|
| 25 | private final DonationPostService donationPostService;
|
---|
[f8007b3] | 26 | private final FundsCollectedService fundsCollectedService;
|
---|
[5577566] | 27 |
|
---|
[f8007b3] | 28 | public UserProfileController(UserService userService, DonationPostService donationPostService, FundsCollectedService fundsCollectedService) {
|
---|
[5577566] | 29 | this.userService = userService;
|
---|
| 30 | this.donationPostService = donationPostService;
|
---|
[f8007b3] | 31 | this.fundsCollectedService = fundsCollectedService;
|
---|
[5577566] | 32 | }
|
---|
| 33 |
|
---|
| 34 | @RequestMapping("/userInformation")
|
---|
| 35 | public String userInformation(Model model) {
|
---|
| 36 | model.addAttribute("userInformation", true);
|
---|
| 37 | return "myProfile";
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | @RequestMapping("/myDonations")
|
---|
| 41 | public String myDonations(Model model) {
|
---|
| 42 | model.addAttribute("myDonations", true);
|
---|
[f0d5cb7] | 43 | MainUser user = (MainUser) userService.loadUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
|
---|
[5577566] | 44 | List<DonationInformation> donations = user.getDonationInformation();
|
---|
| 45 | model.addAttribute("donations", donations);
|
---|
| 46 | double total = donations.stream().mapToDouble(DonationInformation::getDonatedAmount).sum();
|
---|
| 47 | model.addAttribute("total", total);
|
---|
| 48 | return "myProfile";
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | @RequestMapping("/myPosts")
|
---|
| 52 | public String myPosts(Model model) {
|
---|
| 53 | AppUser user = (AppUser) model.getAttribute("user");
|
---|
| 54 | List<DonationPost> posts = donationPostService.findAllByUser(user);
|
---|
| 55 | model.addAttribute("postList", posts);
|
---|
| 56 | model.addAttribute("myPosts", true);
|
---|
| 57 | return "myProfile";
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | @RequestMapping(value = "/changePassword", method = RequestMethod.POST)
|
---|
| 61 | public String changePassword(Model model,
|
---|
| 62 | @RequestParam String oldPassword,
|
---|
| 63 | @RequestParam String newPassword,
|
---|
| 64 | @RequestParam String confirmPassword) {
|
---|
| 65 |
|
---|
| 66 | AppUser user = (AppUser) model.getAttribute("user");
|
---|
| 67 | if(PasswordEncoder.bCryptPasswordEncoder().matches(oldPassword, user.getPassword())) {
|
---|
| 68 | if(newPassword.equals(confirmPassword)) {
|
---|
| 69 | user.setPassword(PasswordEncoder.bCryptPasswordEncoder().encode(newPassword));
|
---|
| 70 | userService.saveUser(user);
|
---|
| 71 | model.addAttribute("changedPassword", true);
|
---|
| 72 | model.addAttribute("userInformation", true);
|
---|
| 73 | return "myProfile";
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | model.addAttribute("notChangedPassword", true);
|
---|
| 78 | model.addAttribute("userInformation", true);
|
---|
| 79 | return "myProfile";
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | @RequestMapping(value = "/changeCardInfo", method = RequestMethod.POST)
|
---|
| 83 | public String changeCardInfo(Model model,
|
---|
| 84 | @RequestParam String cardName,
|
---|
| 85 | @RequestParam String cardNumber,
|
---|
| 86 | @RequestParam String expiryDate,
|
---|
| 87 | @RequestParam String cvv) {
|
---|
| 88 |
|
---|
| 89 | if(cardName.isEmpty() || cardNumber.isEmpty() || expiryDate.isEmpty() || cvv.isEmpty()) {
|
---|
| 90 | model.addAttribute("creditCardError", true);
|
---|
| 91 | model.addAttribute("userInformation", true);
|
---|
| 92 | return "myProfile";
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[f0d5cb7] | 95 | MainUser user = (MainUser) model.getAttribute("user");
|
---|
[5577566] | 96 | user.setCreditCardInfo(cardName + "," + cardNumber + "," + expiryDate + "," + cvv);
|
---|
| 97 | userService.saveUser(user);
|
---|
| 98 |
|
---|
| 99 | return "redirect:/userInformation";
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | @RequestMapping("/removeCardInfo")
|
---|
| 103 | public String removeCardInfo(Model model) {
|
---|
[f0d5cb7] | 104 | MainUser user = (MainUser) model.getAttribute("user");
|
---|
[5577566] | 105 | user.setCreditCardInfo(null);
|
---|
| 106 | userService.saveUser(user);
|
---|
| 107 | return "redirect:/userInformation";
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[194776a] | 110 | @RequestMapping(value = "/addFunds", method = RequestMethod.POST)
|
---|
[f8007b3] | 111 | public String addFunds(@RequestParam Long postid,
|
---|
| 112 | @RequestParam String type,
|
---|
| 113 | @RequestParam float amount) {
|
---|
| 114 |
|
---|
| 115 | DonationPost post = donationPostService.getById(postid);
|
---|
[194776a] | 116 | if(post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName())) {
|
---|
| 117 | FundsCollected funds = new FundsCollected(type, amount);
|
---|
| 118 | fundsCollectedService.save(funds);
|
---|
| 119 | post.getFundsCollected().add(funds);
|
---|
[b8dc761] | 120 | post.setTotalFundsCollected(post.getTotalFundsCollected() + amount);
|
---|
[6fa3d09] | 121 | post.setRiskFactor(getRisk(post));
|
---|
[194776a] | 122 | donationPostService.save(post);
|
---|
| 123 | }
|
---|
[f8007b3] | 124 | return "redirect:/myPosts";
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[5577566] | 127 | @ModelAttribute("user")
|
---|
| 128 | public AppUser addAttributes() {
|
---|
[b8dc761] | 129 | if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
|
---|
| 130 |
|
---|
| 131 | String email = SecurityContextHolder.getContext().getAuthentication().getName();
|
---|
| 132 | return userService.loadUserByUsername(email);
|
---|
[5577566] | 133 | }
|
---|
| 134 | return null;
|
---|
| 135 | }
|
---|
[6fa3d09] | 136 | private Integer getRisk(DonationPost post) {
|
---|
| 137 | int risk;
|
---|
| 138 | if(post.getFundsNeeded() <= post.getTotalFundsCollected()) {
|
---|
| 139 | risk = 102;
|
---|
| 140 | }
|
---|
| 141 | else
|
---|
| 142 | {
|
---|
| 143 | if (LocalDate.now().isAfter(post.getDateDue()))
|
---|
| 144 | {
|
---|
| 145 | risk=0;
|
---|
| 146 | }
|
---|
| 147 | else
|
---|
| 148 | {
|
---|
| 149 | float dailyAverage = post.getTotalFundsCollected() / (Duration.between(post.getCreatedAt().atTime(0, 0, 0), LocalDate.now().atTime(0, 0, 0)).toDays()+1);
|
---|
[a64f926] | 150 | float neededAverage = (post.getFundsNeeded() - post.getTotalFundsCollected()) / (Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays()+(24-LocalDateTime.now().getHour()/24f));
|
---|
[6fa3d09] | 151 |
|
---|
| 152 | if(Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays() == 0) {
|
---|
| 153 | float hour=(float) LocalDateTime.now().getHour();
|
---|
| 154 | float mins=(float) LocalDateTime.now().getMinute();
|
---|
[a64f926] | 155 | hour=hour+(mins/60f);
|
---|
| 156 | float hourlyAverage=(dailyAverage/24f);
|
---|
| 157 | float neededhourlyAverage=(post.getFundsNeeded() - post.getTotalFundsCollected())/(24f-hour);
|
---|
[6fa3d09] | 158 | risk = (int) (hourlyAverage/neededhourlyAverage*100);
|
---|
| 159 | if (risk>100)
|
---|
| 160 | {
|
---|
| 161 | risk=100;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | }
|
---|
| 165 | else
|
---|
| 166 | {
|
---|
| 167 | System.out.println(dailyAverage + " " + neededAverage);
|
---|
| 168 | risk = (int) (dailyAverage / neededAverage * 100);
|
---|
| 169 |
|
---|
| 170 | if(risk > 100) {
|
---|
| 171 | risk = 100;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 | return risk;
|
---|
| 177 | }
|
---|
[5577566] | 178 | }
|
---|
[6fa3d09] | 179 |
|
---|