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

Last change on this file since 0c37625 was 0c37625, checked in by NikolaCenevski <cenevskinikola@…>, 3 years ago

Moderator pagination

  • Property mode set to 100644
File size: 7.3 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.getSize() == 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";
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";
84 }
85
86 @RequestMapping("/moderator/myApprovedPosts")
87 public String myApprovedPosts(Model model) {
88 Moderator moderator = (Moderator) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
89 List<DonationPost> postList = donationPostService.findAllByModerator(moderator);
90 if (postList.size() == 0) {
91 model.addAttribute("noPosts", true);
92 return "postApproval";
93 }
94 model.addAttribute("postList", postList);
95 return "postApproval";
96 }
97
98 @RequestMapping("/moderator/report")
99 public String reportedPosts(Model model,
100 @RequestParam int page,
101 @RequestParam String sort,
102 @RequestParam(required = false,defaultValue = "") String order) {
103 Page<ReportPost> postList = reportPostService.findAll(page, 6, sort, order);
104 if (postList.getSize() == 0) {
105 model.addAttribute("noPosts", true);
106 return "reportedPosts";
107 }
108 model.addAttribute("postList", postList);
109 return "reportedPosts";
110 }
111
112 @RequestMapping("/moderator/reportPost")
113 public String report(@RequestParam Long postid, Model model) {
114 ReportPost post = reportPostService.findById(postid);
115 model.addAttribute("post", post);
116 DonationPost donationPost = post.getDonationPost();
117 AppUser user = donationPost.getUser();
118 model.addAttribute("createdByFirstName", user.getFirstName());
119 model.addAttribute("createdByLastName", user.getLastName());
120 model.addAttribute("report", true);
121 Moderator moderator = post.getDonationPost().getModerator();
122 model.addAttribute("moderatorFirstName", moderator.getFirstName());
123 model.addAttribute("moderatorLastName", moderator.getLastName());
124
125 return "reportPost";
126 }
127
128 @RequestMapping("/moderator/dismiss")
129 public String dismiss(@RequestParam Long postid) {
130 ReportPost post = reportPostService.findById(postid);
131 deleteReportPost(post);
132 return "redirect:/moderator/report";
133 }
134
135 @RequestMapping("/moderator/deletePost")
136 public String deletePost(@RequestParam Long postid,
137 @RequestParam String description) {
138 ReportPost post = reportPostService.findById(postid);
139 DonationPost donationPost = post.getDonationPost();
140 emailService.sendDeletionEmail(donationPost.getUser().getEmail(), "CharitAbleMk: " + donationPost.getTitle() + " has been deleted", description);
141 deleteReportPost(post);
142 deleteDonationPost(donationPost);
143 return "redirect:/moderator/approval";
144 }
145
146 public void deleteDonationPost(DonationPost donationPost) {
147 List<String> fileForDeletion = donationPost.getPhotosForDeletion();
148 for (String f : fileForDeletion) {
149 File file = new File(f);
150 file.delete();
151 }
152 donationPostService.delete(donationPost);
153 }
154
155 public void deleteReportPost(ReportPost reportPost) {
156 List<Reason> reasons = new ArrayList<>(reportPost.getReasons());
157 if (reportPost.getReasons().size() > 0) {
158 reportPost.getReasons().subList(0, reportPost.getReasons().size()).clear();
159 }
160 reportPostService.save(reportPost);
161 for(Reason r: reasons) {
162 reasonService.delete(r);
163 }
164 reportPostService.delete(reportPost);
165 }
166
167 @ModelAttribute("user")
168 public AppUser addAttributes() {
169 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
170 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
171 }
172 return null;
173 }
174}
Note: See TracBrowser for help on using the repository browser.