1 | package it.finki.charitable.controller;
|
---|
2 |
|
---|
3 | import it.finki.charitable.entities.*;
|
---|
4 | import it.finki.charitable.security.PasswordEncoder;
|
---|
5 | import it.finki.charitable.services.DonationPostService;
|
---|
6 | import it.finki.charitable.services.FundsCollectedService;
|
---|
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 |
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | @Controller
|
---|
19 | public class UserProfileController {
|
---|
20 |
|
---|
21 | private final UserService userService;
|
---|
22 | private final DonationPostService donationPostService;
|
---|
23 | private final FundsCollectedService fundsCollectedService;
|
---|
24 |
|
---|
25 | public UserProfileController(UserService userService, DonationPostService donationPostService, FundsCollectedService fundsCollectedService) {
|
---|
26 | this.userService = userService;
|
---|
27 | this.donationPostService = donationPostService;
|
---|
28 | this.fundsCollectedService = fundsCollectedService;
|
---|
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 | @RequestMapping("/addFunds")
|
---|
108 | public String addFunds(@RequestParam Long postid,
|
---|
109 | @RequestParam String type,
|
---|
110 | @RequestParam float amount) {
|
---|
111 |
|
---|
112 | DonationPost post = donationPostService.getById(postid);
|
---|
113 | FundsCollected funds = new FundsCollected(type, amount);
|
---|
114 | fundsCollectedService.save(funds);
|
---|
115 |
|
---|
116 | post.getFundsCollected().add(funds);
|
---|
117 | donationPostService.save(post);
|
---|
118 | return "redirect:/myPosts";
|
---|
119 | }
|
---|
120 |
|
---|
121 | @ModelAttribute("user")
|
---|
122 | public AppUser addAttributes() {
|
---|
123 | if(SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
|
---|
124 | return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
---|
125 | }
|
---|
126 | return null;
|
---|
127 | }
|
---|
128 | }
|
---|