Changeset ab49338 for src/main/java/it
- Timestamp:
- 09/04/21 12:17:17 (3 years ago)
- Branches:
- master
- Children:
- 0f4f552
- Parents:
- 5306751
- Location:
- src/main/java/it/finki/charitable
- Files:
-
- 6 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/it/finki/charitable/controller/DonationPostController.java
r5306751 rab49338 2 2 3 3 import it.finki.charitable.entities.*; 4 import it.finki.charitable.services.DonationInformationService; 5 import it.finki.charitable.services.DonationPostService; 6 import it.finki.charitable.services.FundsCollectedService; 7 import it.finki.charitable.services.UserService; 4 import it.finki.charitable.services.*; 8 5 import it.finki.charitable.util.FileUploadUtil; 9 import org.springframework.data.domain.Page;10 6 import org.springframework.format.annotation.DateTimeFormat; 11 7 import org.springframework.security.core.context.SecurityContextHolder; … … 16 12 import org.springframework.web.multipart.MultipartFile; 17 13 18 import java.io.File;19 14 import java.io.IOException; 20 15 import java.time.LocalDate; … … 32 27 private final FundsCollectedService fundsCollectedService; 33 28 private final DonationInformationService donationInformationService; 34 35 public DonationPostController(DonationPostService donationPostService, UserService userService, FundsCollectedService fundsCollectedService, DonationInformationService donationInformationService) { 29 private final ReportPostService reportPostService; 30 private final ReasonService reasonService; 31 32 public DonationPostController(DonationPostService donationPostService, UserService userService, FundsCollectedService fundsCollectedService, DonationInformationService donationInformationService, ReportPostService reportPostService, ReasonService reasonService) { 36 33 this.donationPostService = donationPostService; 37 34 this.userService = userService; 38 35 this.fundsCollectedService = fundsCollectedService; 39 36 this.donationInformationService = donationInformationService; 37 this.reportPostService = reportPostService; 38 this.reasonService = reasonService; 40 39 } 41 40 … … 140 139 return "post"; 141 140 } 142 AppUser user = post.getUser(); 143 model.addAttribute("post", post); 144 model.addAttribute("createdByFirstName", user.getFirstName()); 145 model.addAttribute("createdByLastName", user.getLastName()); 146 double total = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum(); 147 model.addAttribute("total", total); 141 142 if (post.getApproved() || (post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName()) && !post.getApproved())) { 143 AppUser user = post.getUser(); 144 Moderator moderator = post.getModerator(); 145 model.addAttribute("post", post); 146 model.addAttribute("createdByFirstName", user.getFirstName()); 147 model.addAttribute("createdByLastName", user.getLastName()); 148 if (moderator != null) { 149 model.addAttribute("moderatorFirstName", moderator.getFirstName()); 150 model.addAttribute("moderatorLastName", moderator.getLastName()); 151 } 152 double total = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum(); 153 model.addAttribute("total", total); 154 } else { 155 model.addAttribute("notFound", true); 156 } 148 157 return "post"; 149 158 } 150 151 // @RequestMapping("/deletePost")152 // public String deletePost(@RequestParam Long postid) {153 // DonationPost post = donationPostService.getById(postid);154 // if (post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName())) {155 // List<String> fileForDeletion = post.getPhotosForDeletion();156 // for (String f : fileForDeletion) {157 // File file = new File(f);158 // file.delete();159 // }160 // donationPostService.delete(post);161 // }162 //163 // return "redirect:/myPosts";164 // }165 159 166 160 @RequestMapping("/donate") … … 188 182 } 189 183 184 @RequestMapping("/report") 185 public String report(@RequestParam Long postid, 186 @RequestParam String description) { 187 188 DonationPost donationPost = donationPostService.getById(postid); 189 ReportPost reportPost = reportPostService.findByDonationPost(donationPost); 190 if(reportPost == null) { 191 reportPost = new ReportPost(); 192 reportPost.setDonationPost(donationPost); 193 } 194 195 Reason reason = new Reason(); 196 AppUser user = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 197 reason.setUser(user); 198 reason.setDescription(description); 199 reasonService.save(reason); 200 reportPost.getReasons().add(reason); 201 reportPostService.save(reportPost); 202 return String.format("redirect:/post?postid=%d", postid); 203 } 204 190 205 @ModelAttribute("user") 191 206 public AppUser addAttributes() { -
src/main/java/it/finki/charitable/controller/ModeratorController.java
r5306751 rab49338 1 1 package it.finki.charitable.controller; 2 2 3 import it.finki.charitable.entities.AppUser; 4 import it.finki.charitable.entities.DonationPost; 3 import it.finki.charitable.entities.*; 5 4 import it.finki.charitable.services.DonationPostService; 6 5 import it.finki.charitable.services.EmailService; 6 import it.finki.charitable.services.ReasonService; 7 import it.finki.charitable.services.ReportPostService; 7 8 import org.springframework.security.core.context.SecurityContextHolder; 8 9 import org.springframework.stereotype.Controller; … … 13 14 14 15 import java.io.File; 16 import java.util.ArrayList; 15 17 import java.util.List; 16 18 … … 20 22 private final DonationPostService donationPostService; 21 23 private final EmailService emailService; 24 private final ReportPostService reportPostService; 25 private final ReasonService reasonService; 22 26 23 public ModeratorController(DonationPostService donationPostService, EmailService emailService ) {27 public ModeratorController(DonationPostService donationPostService, EmailService emailService, ReportPostService reportPostService, ReasonService reasonService) { 24 28 this.donationPostService = donationPostService; 25 29 this.emailService = emailService; 30 this.reportPostService = reportPostService; 31 this.reasonService = reasonService; 26 32 } 27 33 … … 48 54 model.addAttribute("createdByFirstName", user.getFirstName()); 49 55 model.addAttribute("createdByLastName", user.getLastName()); 56 if (post.getApproved()) { 57 model.addAttribute("approved", true); 58 } 50 59 return "moderatorPost"; 51 60 } … … 55 64 DonationPost post = donationPostService.getById(postid); 56 65 post.setApproved(true); 66 post.setModerator((Moderator) SecurityContextHolder.getContext().getAuthentication().getPrincipal()); 57 67 donationPostService.save(post); 58 emailService.sendApprovalEmail(post.getUser().getEmail(), "CharitAbleMk: " + post.getTitle() + " has been approved", postid);68 emailService.sendApprovalEmail(post.getUser().getEmail(), "CharitAbleMk: " + post.getTitle() + " has been approved", postid); 59 69 return "redirect:/moderator/approval"; 60 70 } … … 65 75 DonationPost post = donationPostService.getById(postid); 66 76 emailService.sendNoApprovalEmail(post.getUser().getEmail(), "CharitAbleMk: " + post.getTitle() + " has not been approved", description); 67 List<String> fileForDeletion = post.getPhotosForDeletion(); 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(); 68 140 for (String f : fileForDeletion) { 69 141 File file = new File(f); 70 142 file.delete(); 71 143 } 72 donationPostService.delete(post); 73 return "redirect:/moderator/approval"; 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); 74 157 } 75 158 -
src/main/java/it/finki/charitable/entities/DonationPost.java
r5306751 rab49338 55 55 private AppUser user; 56 56 57 @ManyToOne 58 private Moderator moderator; 59 57 60 @OneToMany 58 61 private List<FundsCollected> fundsCollected = new ArrayList<>(); … … 204 207 } 205 208 209 public Moderator getModerator() { 210 return moderator; 211 } 212 213 public void setModerator(Moderator moderator) { 214 this.moderator = moderator; 215 } 216 206 217 public List<FundsCollected> getFundsCollected() { 207 218 return fundsCollected; -
src/main/java/it/finki/charitable/repository/DonationPostRepository.java
r5306751 rab49338 3 3 import it.finki.charitable.entities.AppUser; 4 4 import it.finki.charitable.entities.DonationPost; 5 import it.finki.charitable.entities.Moderator; 5 6 import org.springframework.data.jpa.repository.JpaRepository; 6 7 import org.springframework.stereotype.Repository; … … 12 13 List<DonationPost> findAllByUser(AppUser user); 13 14 List<DonationPost> findAllByApproved(Boolean approved); 15 List<DonationPost> findAllByModerator(Moderator moderator); 14 16 } -
src/main/java/it/finki/charitable/services/DonationPostService.java
r5306751 rab49338 3 3 import it.finki.charitable.entities.AppUser; 4 4 import it.finki.charitable.entities.DonationPost; 5 import it.finki.charitable.entities.Moderator; 5 6 import it.finki.charitable.repository.DonationPostRepository; 6 7 import org.springframework.data.domain.Page; … … 45 46 } 46 47 48 public List<DonationPost> findAllByModerator(Moderator moderator) { 49 return donationPostRepository.findAllByModerator(moderator); 50 } 51 47 52 public void delete(DonationPost donationPost) { 48 53 donationPostRepository.delete(donationPost); -
src/main/java/it/finki/charitable/services/EmailService.java
r5306751 rab49338 40 40 message.setSubject(subject); 41 41 42 String text = "Sorry, your post hasn't been approved" + "\n" + 42 String text = "Sorry, your post hasn't been approved\n" + 43 "Moderator:\n" + description; 44 message.setText(text); 45 javaMailSender.send(message); 46 } 47 48 public void sendDeletionEmail(String to, String subject, String description) { 49 SimpleMailMessage message = new SimpleMailMessage(); 50 message.setTo(to); 51 message.setSubject(subject); 52 53 String text = "Sorry, your post has been deleted\n" + 43 54 "Moderator:\n" + description; 44 55 message.setText(text);
Note:
See TracChangeset
for help on using the changeset viewer.