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