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

Last change on this file since f0d5cb7 was f0d5cb7, checked in by KostaFortumanov <kfortumanov@…>, 3 years ago

Added moderator class

  • Property mode set to 100644
File size: 4.6 KB
Line 
1package it.finki.charitable.controller;
2
3import it.finki.charitable.entities.AppUser;
4import it.finki.charitable.entities.DonationInformation;
5import it.finki.charitable.entities.DonationPost;
6import it.finki.charitable.entities.MainUser;
7import it.finki.charitable.security.PasswordEncoder;
8import it.finki.charitable.services.DonationPostService;
9import it.finki.charitable.services.UserService;
10import org.springframework.security.core.context.SecurityContextHolder;
11import org.springframework.stereotype.Controller;
12import org.springframework.ui.Model;
13import org.springframework.web.bind.annotation.ModelAttribute;
14import org.springframework.web.bind.annotation.RequestMapping;
15import org.springframework.web.bind.annotation.RequestMethod;
16import org.springframework.web.bind.annotation.RequestParam;
17
18import java.util.List;
19
20@Controller
21public class UserProfileController {
22
23 private final UserService userService;
24 private final DonationPostService donationPostService;
25
26 public UserProfileController(UserService userService, DonationPostService donationPostService) {
27 this.userService = userService;
28 this.donationPostService = donationPostService;
29 }
30
31 @RequestMapping("/userInformation")
32 public String userInformation(Model model) {
33 model.addAttribute("userInformation", true);
34 return "myProfile";
35 }
36
37 @RequestMapping("/myDonations")
38 public String myDonations(Model model) {
39 model.addAttribute("myDonations", true);
40 MainUser user = (MainUser) userService.loadUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
41 List<DonationInformation> donations = user.getDonationInformation();
42 model.addAttribute("donations", donations);
43 double total = donations.stream().mapToDouble(DonationInformation::getDonatedAmount).sum();
44 model.addAttribute("total", total);
45 return "myProfile";
46 }
47
48 @RequestMapping("/myPosts")
49 public String myPosts(Model model) {
50 AppUser user = (AppUser) model.getAttribute("user");
51 List<DonationPost> posts = donationPostService.findAllByUser(user);
52 model.addAttribute("postList", posts);
53 model.addAttribute("myPosts", true);
54 return "myProfile";
55 }
56
57 @RequestMapping(value = "/changePassword", method = RequestMethod.POST)
58 public String changePassword(Model model,
59 @RequestParam String oldPassword,
60 @RequestParam String newPassword,
61 @RequestParam String confirmPassword) {
62
63 AppUser user = (AppUser) model.getAttribute("user");
64 if(PasswordEncoder.bCryptPasswordEncoder().matches(oldPassword, user.getPassword())) {
65 if(newPassword.equals(confirmPassword)) {
66 user.setPassword(PasswordEncoder.bCryptPasswordEncoder().encode(newPassword));
67 userService.saveUser(user);
68 model.addAttribute("changedPassword", true);
69 model.addAttribute("userInformation", true);
70 return "myProfile";
71 }
72 }
73
74 model.addAttribute("notChangedPassword", true);
75 model.addAttribute("userInformation", true);
76 return "myProfile";
77 }
78
79 @RequestMapping(value = "/changeCardInfo", method = RequestMethod.POST)
80 public String changeCardInfo(Model model,
81 @RequestParam String cardName,
82 @RequestParam String cardNumber,
83 @RequestParam String expiryDate,
84 @RequestParam String cvv) {
85
86 if(cardName.isEmpty() || cardNumber.isEmpty() || expiryDate.isEmpty() || cvv.isEmpty()) {
87 model.addAttribute("creditCardError", true);
88 model.addAttribute("userInformation", true);
89 return "myProfile";
90 }
91
92 MainUser user = (MainUser) model.getAttribute("user");
93 user.setCreditCardInfo(cardName + "," + cardNumber + "," + expiryDate + "," + cvv);
94 userService.saveUser(user);
95
96 return "redirect:/userInformation";
97 }
98
99 @RequestMapping("/removeCardInfo")
100 public String removeCardInfo(Model model) {
101 MainUser user = (MainUser) model.getAttribute("user");
102 user.setCreditCardInfo(null);
103 userService.saveUser(user);
104 return "redirect:/userInformation";
105 }
106
107 @ModelAttribute("user")
108 public AppUser addAttributes() {
109 if(SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
110 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
111 }
112 return null;
113 }
114}
Note: See TracBrowser for help on using the repository browser.