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

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

Dodadeno posts to be approved

  • Property mode set to 100644
File size: 3.2 KB
Line 
1package it.finki.charitable.controller;
2
3import it.finki.charitable.entities.AppUser;
4import it.finki.charitable.entities.DonationPost;
5import it.finki.charitable.services.DonationPostService;
6import it.finki.charitable.services.EmailService;
7import org.springframework.security.core.context.SecurityContextHolder;
8import org.springframework.stereotype.Controller;
9import org.springframework.ui.Model;
10import org.springframework.web.bind.annotation.ModelAttribute;
11import org.springframework.web.bind.annotation.RequestMapping;
12import org.springframework.web.bind.annotation.RequestParam;
13
14import java.io.File;
15import java.util.List;
16
17@Controller
18public 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}
Note: See TracBrowser for help on using the repository browser.