1 | package it.finki.charitable.controller;
|
---|
2 |
|
---|
3 | import it.finki.charitable.entities.AppUser;
|
---|
4 | import it.finki.charitable.entities.DonationPost;
|
---|
5 | import it.finki.charitable.services.DonationPostService;
|
---|
6 | import it.finki.charitable.services.EmailService;
|
---|
7 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
8 | import org.springframework.stereotype.Controller;
|
---|
9 | import org.springframework.ui.Model;
|
---|
10 | import org.springframework.web.bind.annotation.ModelAttribute;
|
---|
11 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
12 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
13 |
|
---|
14 | import java.io.File;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 | @Controller
|
---|
18 | public class ModeratorController {
|
---|
19 |
|
---|
20 | private final DonationPostService donationPostService;
|
---|
21 | private final EmailService emailService;
|
---|
22 |
|
---|
23 | public ModeratorController(DonationPostService donationPostService, EmailService emailService) {
|
---|
24 | this.donationPostService = donationPostService;
|
---|
25 | this.emailService = emailService;
|
---|
26 | }
|
---|
27 |
|
---|
28 | @RequestMapping("moderator/approval")
|
---|
29 | public String approval(Model model) {
|
---|
30 | List<DonationPost> postList = donationPostService.findAllByApproved(false);
|
---|
31 | if (postList.size() == 0) {
|
---|
32 | model.addAttribute("noPosts", true);
|
---|
33 | return "postApproval";
|
---|
34 | }
|
---|
35 | model.addAttribute("postList", postList);
|
---|
36 | return "postApproval";
|
---|
37 | }
|
---|
38 |
|
---|
39 | @RequestMapping("moderator/post")
|
---|
40 | public String post(Model model, @RequestParam Long postid) {
|
---|
41 | DonationPost post = donationPostService.getById(postid);
|
---|
42 | if (post == null) {
|
---|
43 | model.addAttribute("notFound", true);
|
---|
44 | return "post";
|
---|
45 | }
|
---|
46 | AppUser user = post.getUser();
|
---|
47 | model.addAttribute("post", post);
|
---|
48 | model.addAttribute("createdByFirstName", user.getFirstName());
|
---|
49 | model.addAttribute("createdByLastName", user.getLastName());
|
---|
50 | return "moderatorPost";
|
---|
51 | }
|
---|
52 |
|
---|
53 | @RequestMapping("/moderator/approvePost")
|
---|
54 | public String approvePost(@RequestParam Long postid) {
|
---|
55 | DonationPost post = donationPostService.getById(postid);
|
---|
56 | post.setApproved(true);
|
---|
57 | donationPostService.save(post);
|
---|
58 | emailService.sendApprovalEmail(post.getUser().getEmail(),"CharitAbleMk: " + post.getTitle() + " has been approved", postid);
|
---|
59 | return "redirect:/moderator/approval";
|
---|
60 | }
|
---|
61 |
|
---|
62 | @RequestMapping("/moderator/dontApprove")
|
---|
63 | public String dontApprove(@RequestParam Long postid,
|
---|
64 | @RequestParam String description) {
|
---|
65 | DonationPost post = donationPostService.getById(postid);
|
---|
66 | emailService.sendNoApprovalEmail(post.getUser().getEmail(), "CharitAbleMk: " + post.getTitle() + " has not been approved", description);
|
---|
67 | List<String> fileForDeletion = post.getPhotosForDeletion();
|
---|
68 | for (String f : fileForDeletion) {
|
---|
69 | File file = new File(f);
|
---|
70 | file.delete();
|
---|
71 | }
|
---|
72 | donationPostService.delete(post);
|
---|
73 | return "redirect:/moderator/approval";
|
---|
74 | }
|
---|
75 |
|
---|
76 | @ModelAttribute("user")
|
---|
77 | public AppUser addAttributes() {
|
---|
78 | if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
|
---|
79 | return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
---|
80 | }
|
---|
81 | return null;
|
---|
82 | }
|
---|
83 | }
|
---|