source: src/main/java/it/finki/charitable/controller/ModeratorController.java

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

bug fix

  • Property mode set to 100644
File size: 7.8 KB
Line 
1package it.finki.charitable.controller;
2
3import it.finki.charitable.entities.*;
4import it.finki.charitable.services.DonationPostService;
5import it.finki.charitable.services.EmailService;
6import it.finki.charitable.services.ReasonService;
7import it.finki.charitable.services.ReportPostService;
8import org.springframework.data.domain.Page;
9import org.springframework.security.core.context.SecurityContextHolder;
10import org.springframework.stereotype.Controller;
11import org.springframework.ui.Model;
12import org.springframework.web.bind.annotation.ModelAttribute;
13import org.springframework.web.bind.annotation.RequestMapping;
14import org.springframework.web.bind.annotation.RequestParam;
15
16import java.io.File;
17import java.util.ArrayList;
18import java.util.List;
19
20@Controller
21public class ModeratorController {
22
23 private final DonationPostService donationPostService;
24 private final EmailService emailService;
25 private final ReportPostService reportPostService;
26 private final ReasonService reasonService;
27
28 public ModeratorController(DonationPostService donationPostService, EmailService emailService, ReportPostService reportPostService, ReasonService reasonService) {
29 this.donationPostService = donationPostService;
30 this.emailService = emailService;
31 this.reportPostService = reportPostService;
32 this.reasonService = reasonService;
33 }
34
35 @RequestMapping("moderator/approval")
36 public String approval(Model model,
37 @RequestParam int page,
38 @RequestParam String sort,
39 @RequestParam(required = false,defaultValue = "") String order) {
40 Page<DonationPost> postList = donationPostService.findPaginated(page, 6, sort, order, false);
41 if (postList.getTotalElements() == 0) {
42 model.addAttribute("noPosts", true);
43 return "postApproval";
44 }
45 model.addAttribute("totalPages", postList.getTotalPages());
46 model.addAttribute("postList", postList);
47 return "postApproval";
48 }
49
50 @RequestMapping("moderator/post")
51 public String post(Model model, @RequestParam Long postid) {
52 DonationPost post = donationPostService.getById(postid);
53 if (post == null) {
54 model.addAttribute("notFound", true);
55 return "post";
56 }
57 AppUser user = post.getUser();
58 model.addAttribute("post", post);
59 model.addAttribute("createdByFirstName", user.getFirstName());
60 model.addAttribute("createdByLastName", user.getLastName());
61 if (post.getApproved()) {
62 model.addAttribute("approved", true);
63 }
64 return "moderatorPost";
65 }
66
67 @RequestMapping("/moderator/approvePost")
68 public String approvePost(@RequestParam Long postid) {
69 DonationPost post = donationPostService.getById(postid);
70 post.setApproved(true);
71 post.setModerator((Moderator) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
72 donationPostService.save(post);
73 emailService.sendApprovalEmail(post.getUser().getEmail(), "CharitAbleMk: " + post.getTitle() + " has been approved", postid);
74 return "redirect:/moderator/approval?page=1&sort=id";
75 }
76
77 @RequestMapping("/moderator/dontApprove")
78 public String dontApprove(@RequestParam Long postid,
79 @RequestParam String description) {
80 DonationPost post = donationPostService.getById(postid);
81 emailService.sendNoApprovalEmail(post.getUser().getEmail(), "CharitAbleMk: " + post.getTitle() + " has not been approved", description);
82 deleteDonationPost(post);
83 return "redirect:/moderator/approval?page=1&sort=id";
84 }
85
86 @RequestMapping("/moderator/myApprovedPosts")
87 public String myApprovedPosts(Model model,
88 @RequestParam int page,
89 @RequestParam String sort,
90 @RequestParam(required = false,defaultValue = "") String order) {
91 Moderator moderator = (Moderator) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
92 Page<DonationPost> postList = donationPostService.findAllByModerator(page, 6, sort, order, moderator);
93 if (postList.getTotalElements() == 0) {
94 model.addAttribute("noPosts", true);
95 return "postApproval";
96 }
97 model.addAttribute("totalPages", postList.getTotalPages());
98 model.addAttribute("postList", postList);
99 return "postApproval";
100 }
101
102 @RequestMapping("/moderator/report")
103 public String reportedPosts(Model model,
104 @RequestParam int page,
105 @RequestParam String sort,
106 @RequestParam(required = false,defaultValue = "") String order) {
107 Page<ReportPost> postList = reportPostService.findAll(page, 6, sort, order);
108 if (postList.getTotalElements() == 0) {
109 model.addAttribute("noPosts", true);
110 return "reportedPosts";
111 }
112 model.addAttribute("totalPages",postList.getTotalPages());
113 model.addAttribute("postList", postList);
114 return "reportedPosts";
115 }
116
117 @RequestMapping("/moderator/reportPost")
118 public String report(@RequestParam Long postid, Model model) {
119 ReportPost post = reportPostService.findById(postid);
120 model.addAttribute("post", post);
121 DonationPost donationPost = post.getDonationPost();
122 AppUser user = donationPost.getUser();
123 model.addAttribute("createdByFirstName", user.getFirstName());
124 model.addAttribute("createdByLastName", user.getLastName());
125 model.addAttribute("report", true);
126 Moderator moderator = post.getDonationPost().getModerator();
127 model.addAttribute("moderatorFirstName", moderator.getFirstName());
128 model.addAttribute("moderatorLastName", moderator.getLastName());
129
130 return "reportPost";
131 }
132
133 @RequestMapping("/moderator/dismiss")
134 public String dismiss(@RequestParam Long postid) {
135 ReportPost post = reportPostService.findById(postid);
136 deleteReportPost(post);
137 return "redirect:/moderator/report?page=1&sort=id";
138 }
139
140 @RequestMapping("/moderator/deletePost")
141 public String deletePost(@RequestParam Long postid,
142 @RequestParam String description) {
143 ReportPost post = reportPostService.findById(postid);
144 DonationPost donationPost = post.getDonationPost();
145 emailService.sendDeletionEmail(donationPost.getUser().getEmail(), "CharitAbleMk: " + donationPost.getTitle() + " has been deleted", description);
146 deleteReportPost(post);
147 deleteDonationPost(donationPost);
148 return "redirect:/moderator/approval?page=1&sort=id";
149 }
150
151 public void deleteDonationPost(DonationPost donationPost) {
152 List<String> fileForDeletion = donationPost.getPhotosForDeletion();
153 for (String f : fileForDeletion) {
154 File file = new File(f);
155 file.delete();
156 }
157 donationPostService.delete(donationPost);
158 }
159
160 public void deleteReportPost(ReportPost reportPost) {
161 List<Reason> reasons = new ArrayList<>(reportPost.getReasons());
162 if (reportPost.getReasons().size() > 0) {
163 reportPost.getReasons().subList(0, reportPost.getReasons().size()).clear();
164 }
165 reportPostService.save(reportPost);
166 for(Reason r: reasons) {
167 reasonService.delete(r);
168 }
169 reportPostService.delete(reportPost);
170 }
171
172 @ModelAttribute("user")
173 public AppUser addAttributes() {
174 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
175 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
176 }
177 return null;
178 }
179}
Note: See TracBrowser for help on using the repository browser.