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

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

Mali promeni vo azuriranje sredstva

  • Property mode set to 100644
File size: 5.4 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.util.List;
17
18@Controller
19public 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(value = "/addFunds", method = RequestMethod.POST)
108 public String addFunds(@RequestParam Long postid,
109 @RequestParam String type,
110 @RequestParam float amount) {
111
112 DonationPost post = donationPostService.getById(postid);
113 if(post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName())) {
114 FundsCollected funds = new FundsCollected(type, amount);
115 fundsCollectedService.save(funds);
116 post.getFundsCollected().add(funds);
117 donationPostService.save(post);
118 }
119 return "redirect:/myPosts";
120 }
121
122 @ModelAttribute("user")
123 public AppUser addAttributes() {
124 if(SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
125 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
126 }
127 return null;
128 }
129}
Note: See TracBrowser for help on using the repository browser.