source: src/main/java/it/finki/charitable/controller/UserProfileController.java@ 6fa3d09

Last change on this file since 6fa3d09 was 6fa3d09, checked in by NikolaCenevski <cenevskinikola@…>, 2 years ago

part 3

  • Property mode set to 100644
File size: 7.3 KB
Line 
1package it.finki.charitable.controller;
2
3import it.finki.charitable.entities.*;
4import it.finki.charitable.security.PasswordEncoder;
5import it.finki.charitable.services.DonationPostService;
6import it.finki.charitable.services.FundsCollectedService;
7import it.finki.charitable.services.UserService;
8import org.springframework.security.core.context.SecurityContextHolder;
9import org.springframework.stereotype.Controller;
10import org.springframework.ui.Model;
11import org.springframework.web.bind.annotation.ModelAttribute;
12import org.springframework.web.bind.annotation.RequestMapping;
13import org.springframework.web.bind.annotation.RequestMethod;
14import org.springframework.web.bind.annotation.RequestParam;
15
16import java.time.Duration;
17import java.time.LocalDate;
18import java.time.LocalDateTime;
19import java.util.List;
20
21@Controller
22public class UserProfileController {
23
24 private final UserService userService;
25 private final DonationPostService donationPostService;
26 private final FundsCollectedService fundsCollectedService;
27
28 public UserProfileController(UserService userService, DonationPostService donationPostService, FundsCollectedService fundsCollectedService) {
29 this.userService = userService;
30 this.donationPostService = donationPostService;
31 this.fundsCollectedService = fundsCollectedService;
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);
43 MainUser user = (MainUser) userService.loadUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
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
95 MainUser user = (MainUser) model.getAttribute("user");
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) {
104 MainUser user = (MainUser) model.getAttribute("user");
105 user.setCreditCardInfo(null);
106 userService.saveUser(user);
107 return "redirect:/userInformation";
108 }
109
110 @RequestMapping(value = "/addFunds", method = RequestMethod.POST)
111 public String addFunds(@RequestParam Long postid,
112 @RequestParam String type,
113 @RequestParam float amount) {
114
115 DonationPost post = donationPostService.getById(postid);
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);
120 post.setTotalFundsCollected(post.getTotalFundsCollected() + amount);
121 post.setRiskFactor(getRisk(post));
122 donationPostService.save(post);
123 }
124 return "redirect:/myPosts";
125 }
126
127 @ModelAttribute("user")
128 public AppUser addAttributes() {
129 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
130
131 String email = SecurityContextHolder.getContext().getAuthentication().getName();
132 return userService.loadUserByUsername(email);
133 }
134 return null;
135 }
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);
150 float neededAverage = (post.getFundsNeeded() - post.getTotalFundsCollected()) / (Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays()+1);
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();
155 hour=hour+(mins/(float)60);
156 float hourlyAverage=(dailyAverage/(float)24);
157 float neededhourlyAverage=(post.getFundsNeeded() - post.getTotalFundsCollected())/((float)24-hour);
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 }
178}
179
Note: See TracBrowser for help on using the repository browser.