[5306751] | 1 | package it.finki.charitable.controller;
|
---|
| 2 |
|
---|
[ab49338] | 3 | import it.finki.charitable.entities.*;
|
---|
[5306751] | 4 | import it.finki.charitable.services.DonationPostService;
|
---|
| 5 | import it.finki.charitable.services.EmailService;
|
---|
[ab49338] | 6 | import it.finki.charitable.services.ReasonService;
|
---|
| 7 | import it.finki.charitable.services.ReportPostService;
|
---|
[0c37625] | 8 | import org.springframework.data.domain.Page;
|
---|
[5306751] | 9 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
| 10 | import org.springframework.stereotype.Controller;
|
---|
| 11 | import org.springframework.ui.Model;
|
---|
| 12 | import org.springframework.web.bind.annotation.ModelAttribute;
|
---|
| 13 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
| 14 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
| 15 |
|
---|
| 16 | import java.io.File;
|
---|
[ab49338] | 17 | import java.util.ArrayList;
|
---|
[5306751] | 18 | import java.util.List;
|
---|
| 19 |
|
---|
| 20 | @Controller
|
---|
| 21 | public class ModeratorController {
|
---|
| 22 |
|
---|
| 23 | private final DonationPostService donationPostService;
|
---|
| 24 | private final EmailService emailService;
|
---|
[ab49338] | 25 | private final ReportPostService reportPostService;
|
---|
| 26 | private final ReasonService reasonService;
|
---|
[5306751] | 27 |
|
---|
[ab49338] | 28 | public ModeratorController(DonationPostService donationPostService, EmailService emailService, ReportPostService reportPostService, ReasonService reasonService) {
|
---|
[5306751] | 29 | this.donationPostService = donationPostService;
|
---|
| 30 | this.emailService = emailService;
|
---|
[ab49338] | 31 | this.reportPostService = reportPostService;
|
---|
| 32 | this.reasonService = reasonService;
|
---|
[5306751] | 33 | }
|
---|
| 34 |
|
---|
| 35 | @RequestMapping("moderator/approval")
|
---|
[0c37625] | 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);
|
---|
[ee0e297] | 41 | if (postList.getTotalElements() == 0) {
|
---|
[5306751] | 42 | model.addAttribute("noPosts", true);
|
---|
| 43 | return "postApproval";
|
---|
| 44 | }
|
---|
[0c37625] | 45 | model.addAttribute("totalPages", postList.getTotalPages());
|
---|
[5306751] | 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());
|
---|
[ab49338] | 61 | if (post.getApproved()) {
|
---|
| 62 | model.addAttribute("approved", true);
|
---|
| 63 | }
|
---|
[5306751] | 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);
|
---|
[ab49338] | 71 | post.setModerator((Moderator) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
|
---|
[5306751] | 72 | donationPostService.save(post);
|
---|
[ab49338] | 73 | emailService.sendApprovalEmail(post.getUser().getEmail(), "CharitAbleMk: " + post.getTitle() + " has been approved", postid);
|
---|
[ee0e297] | 74 | return "redirect:/moderator/approval?page=1&sort=id";
|
---|
[5306751] | 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);
|
---|
[ab49338] | 82 | deleteDonationPost(post);
|
---|
[ee0e297] | 83 | return "redirect:/moderator/approval?page=1&sort=id";
|
---|
[ab49338] | 84 | }
|
---|
| 85 |
|
---|
| 86 | @RequestMapping("/moderator/myApprovedPosts")
|
---|
[ee0e297] | 87 | public String myApprovedPosts(Model model,
|
---|
| 88 | @RequestParam int page,
|
---|
| 89 | @RequestParam String sort,
|
---|
| 90 | @RequestParam(required = false,defaultValue = "") String order) {
|
---|
[ab49338] | 91 | Moderator moderator = (Moderator) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
---|
[ee0e297] | 92 | Page<DonationPost> postList = donationPostService.findAllByModerator(page, 6, sort, order, moderator);
|
---|
| 93 | if (postList.getTotalElements() == 0) {
|
---|
[ab49338] | 94 | model.addAttribute("noPosts", true);
|
---|
| 95 | return "postApproval";
|
---|
| 96 | }
|
---|
[ee0e297] | 97 | model.addAttribute("totalPages", postList.getTotalPages());
|
---|
[ab49338] | 98 | model.addAttribute("postList", postList);
|
---|
| 99 | return "postApproval";
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | @RequestMapping("/moderator/report")
|
---|
[0c37625] | 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);
|
---|
[ee0e297] | 108 | if (postList.getTotalElements() == 0) {
|
---|
[ab49338] | 109 | model.addAttribute("noPosts", true);
|
---|
| 110 | return "reportedPosts";
|
---|
| 111 | }
|
---|
[ee0e297] | 112 | model.addAttribute("totalPages",postList.getTotalPages());
|
---|
[ab49338] | 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);
|
---|
[ee0e297] | 137 | return "redirect:/moderator/report?page=1&sort=id";
|
---|
[ab49338] | 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);
|
---|
[ee0e297] | 148 | return "redirect:/moderator/approval?page=1&sort=id";
|
---|
[ab49338] | 149 | }
|
---|
| 150 |
|
---|
| 151 | public void deleteDonationPost(DonationPost donationPost) {
|
---|
| 152 | List<String> fileForDeletion = donationPost.getPhotosForDeletion();
|
---|
[5306751] | 153 | for (String f : fileForDeletion) {
|
---|
| 154 | File file = new File(f);
|
---|
| 155 | file.delete();
|
---|
| 156 | }
|
---|
[ab49338] | 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);
|
---|
[5306751] | 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 | }
|
---|