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