1 | package it.finki.charitable.controller;
|
---|
2 |
|
---|
3 | import it.finki.charitable.entities.*;
|
---|
4 | import it.finki.charitable.services.*;
|
---|
5 | import it.finki.charitable.util.FileUploadUtil;
|
---|
6 | import org.springframework.data.domain.*;
|
---|
7 | import org.springframework.format.annotation.DateTimeFormat;
|
---|
8 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
9 | import org.springframework.stereotype.Controller;
|
---|
10 | import org.springframework.ui.Model;
|
---|
11 | import org.springframework.util.StringUtils;
|
---|
12 | import org.springframework.web.bind.annotation.*;
|
---|
13 | import org.springframework.web.multipart.MultipartFile;
|
---|
14 |
|
---|
15 | import java.io.IOException;
|
---|
16 | import java.time.LocalDate;
|
---|
17 | import java.util.*;
|
---|
18 | import java.util.stream.Collectors;
|
---|
19 |
|
---|
20 | @Controller
|
---|
21 | public class DonationPostController {
|
---|
22 |
|
---|
23 | private final DonationPostService donationPostService;
|
---|
24 | private final UserService userService;
|
---|
25 | private final FundsCollectedService fundsCollectedService;
|
---|
26 | private final DonationInformationService donationInformationService;
|
---|
27 | private final ReportPostService reportPostService;
|
---|
28 | private final ReasonService reasonService;
|
---|
29 |
|
---|
30 | public DonationPostController(DonationPostService donationPostService, UserService userService, FundsCollectedService fundsCollectedService, DonationInformationService donationInformationService, ReportPostService reportPostService, ReasonService reasonService) {
|
---|
31 | this.donationPostService = donationPostService;
|
---|
32 | this.userService = userService;
|
---|
33 | this.fundsCollectedService = fundsCollectedService;
|
---|
34 | this.donationInformationService = donationInformationService;
|
---|
35 | this.reportPostService = reportPostService;
|
---|
36 | this.reasonService = reasonService;
|
---|
37 | }
|
---|
38 |
|
---|
39 | @RequestMapping("/upload")
|
---|
40 | public String upload() {
|
---|
41 | return "upload";
|
---|
42 | }
|
---|
43 |
|
---|
44 | @RequestMapping(value = "/newPost", method = RequestMethod.POST)
|
---|
45 | public String newPost(Model model,
|
---|
46 | @RequestParam String title,
|
---|
47 | @RequestParam String fundsNeeded,
|
---|
48 | @RequestParam String currency,
|
---|
49 | @RequestParam String description,
|
---|
50 | @RequestParam String telekom,
|
---|
51 | @RequestParam String a1,
|
---|
52 | @RequestParam(defaultValue = "2020-01-01") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate dateDue,
|
---|
53 | @RequestParam String bankAccount,
|
---|
54 | @RequestParam MultipartFile titleImage,
|
---|
55 | @RequestParam MultipartFile[] images,
|
---|
56 | @RequestParam MultipartFile[] moderatorImages) {
|
---|
57 |
|
---|
58 | System.out.println(moderatorImages.length);
|
---|
59 | if(titleImage.isEmpty() || (moderatorImages.length == 1 && moderatorImages[0].isEmpty())) {
|
---|
60 | model.addAttribute("error", true);
|
---|
61 | return "upload";
|
---|
62 | }
|
---|
63 |
|
---|
64 | if(title.isBlank() || fundsNeeded.isBlank() || currency.isBlank() || description.isBlank() || bankAccount.isBlank() || dateDue.equals(LocalDate.of(2020,1,1))) {
|
---|
65 | model.addAttribute("error", true);
|
---|
66 | return "upload";
|
---|
67 | }
|
---|
68 |
|
---|
69 | DonationPost post = new DonationPost();
|
---|
70 | post.setTitle(title);
|
---|
71 |
|
---|
72 | try {
|
---|
73 | float funds = Float.parseFloat(fundsNeeded);
|
---|
74 | if (funds <= 0) {
|
---|
75 | model.addAttribute("error", true);
|
---|
76 | return "upload";
|
---|
77 | }
|
---|
78 | post.setFundsNeeded(funds);
|
---|
79 | } catch (NumberFormatException e) {
|
---|
80 | model.addAttribute("error", true);
|
---|
81 | return "upload";
|
---|
82 | }
|
---|
83 |
|
---|
84 | post.setCurrency(currency);
|
---|
85 | post.setDescription(description);
|
---|
86 | post.setDateDue(dateDue);
|
---|
87 | post.setBankAccount(bankAccount);
|
---|
88 | post.setApproved(false);
|
---|
89 |
|
---|
90 | List<String> phoneNumbers = Arrays.asList(telekom, a1);
|
---|
91 |
|
---|
92 | List<String> photos = new ArrayList<>();
|
---|
93 | photos.add(StringUtils.cleanPath(Objects.requireNonNull(titleImage.getOriginalFilename())));
|
---|
94 | Arrays.stream(images).filter(i -> !i.isEmpty()).forEach(i -> photos.add(StringUtils.cleanPath(Objects.requireNonNull(i.getOriginalFilename()))));
|
---|
95 |
|
---|
96 | List<MultipartFile> files = new ArrayList<>();
|
---|
97 | files.add(titleImage);
|
---|
98 | files.addAll(Arrays.stream(images).filter(i -> !i.isEmpty()).collect(Collectors.toList()));
|
---|
99 |
|
---|
100 | List<String> moderatorPhotos = new ArrayList<>();
|
---|
101 | Arrays.stream(moderatorImages).forEach(i -> moderatorPhotos.add(StringUtils.cleanPath(Objects.requireNonNull(i.getOriginalFilename()))));
|
---|
102 |
|
---|
103 | post.setPhoneNumbers(phoneNumbers);
|
---|
104 | post.setImages(photos);
|
---|
105 | post.setModeratorImages(moderatorPhotos);
|
---|
106 |
|
---|
107 | AppUser user = (AppUser) model.getAttribute("user");
|
---|
108 | post.setUser(user);
|
---|
109 |
|
---|
110 | DonationPost savedPost = donationPostService.save(post);
|
---|
111 |
|
---|
112 | for (MultipartFile file : files) {
|
---|
113 | String fileName = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
|
---|
114 | String uploadDir = "post-photos/" + savedPost.getId();
|
---|
115 | try {
|
---|
116 | FileUploadUtil.saveFile(uploadDir, fileName, file);
|
---|
117 | } catch (IOException e) {
|
---|
118 | e.printStackTrace();
|
---|
119 | }
|
---|
120 |
|
---|
121 | }
|
---|
122 |
|
---|
123 | for (MultipartFile file : moderatorImages) {
|
---|
124 |
|
---|
125 | String fileName = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
|
---|
126 | String uploadDir = "moderator-photos/" + savedPost.getId();
|
---|
127 | try {
|
---|
128 | FileUploadUtil.saveFile(uploadDir, fileName, file);
|
---|
129 | } catch (IOException e) {
|
---|
130 | e.printStackTrace();
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | return "upload";
|
---|
135 | }
|
---|
136 |
|
---|
137 | @RequestMapping("/album")
|
---|
138 | public String album(Model model,
|
---|
139 | @RequestParam int page,
|
---|
140 | @RequestParam String sort,
|
---|
141 | @RequestParam(required = false, defaultValue = "desc") String order,
|
---|
142 | @RequestParam(required = false, defaultValue = "all") String groupBy) {
|
---|
143 |
|
---|
144 | Sort s = Sort.by(sort);
|
---|
145 | s = order.equals("asc") ? s.ascending() : s.descending();
|
---|
146 | Pageable pageable = PageRequest.of(page - 1, 6, s);
|
---|
147 | Page<DonationPost> postList;
|
---|
148 | postList = donationPostService.findPaginated(page, 6, sort, order, true);
|
---|
149 |
|
---|
150 | if (!groupBy.equals("all")) {
|
---|
151 | List<DonationPost> allPosts = donationPostService.findAllByApproved(true);
|
---|
152 |
|
---|
153 | if (sort.equals("title")) {
|
---|
154 | if (order.equals("asc")) {
|
---|
155 | allPosts.sort(Comparator.comparing(DonationPost::getTitle, (s1, s2) -> s1.compareToIgnoreCase(s2)));
|
---|
156 | } else {
|
---|
157 | allPosts.sort(Comparator.comparing(DonationPost::getTitle, (s1, s2) -> s2.compareToIgnoreCase(s1)));
|
---|
158 | }
|
---|
159 | } else if (sort.equals("dateDue")) {
|
---|
160 | if (order.equals("asc")) {
|
---|
161 | allPosts.sort(Comparator.comparing(DonationPost::getDateDue));
|
---|
162 | } else {
|
---|
163 | allPosts.sort(Comparator.comparing(DonationPost::getDateDue).reversed());
|
---|
164 | }
|
---|
165 | } else if (sort.equals("fundsNeeded")) {
|
---|
166 | if (order.equals("asc")) {
|
---|
167 | allPosts.sort(Comparator.comparing(DonationPost::getFundsNeeded));
|
---|
168 | } else {
|
---|
169 | allPosts.sort(Comparator.comparing(DonationPost::getFundsNeeded).reversed());
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | if (groupBy.equals("completed")) {
|
---|
174 | List<DonationPost> completed = allPosts.stream().filter(post -> {
|
---|
175 | double fundsCollected = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();
|
---|
176 | return fundsCollected >= post.getFundsNeeded();
|
---|
177 | }).collect(Collectors.toList());
|
---|
178 |
|
---|
179 | int start = (int) pageable.getOffset();
|
---|
180 | int end = Math.min((start + pageable.getPageSize()), completed.size());
|
---|
181 | if (start <= end) {
|
---|
182 | postList = new PageImpl<>(completed.subList(start, end), pageable, completed.size());
|
---|
183 | }
|
---|
184 | } else if (groupBy.equals("expired")) {
|
---|
185 | List<DonationPost> expired = allPosts.stream().filter(post -> {
|
---|
186 | double fundsCollected = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();
|
---|
187 | return LocalDate.now().isAfter(post.getDateDue()) && fundsCollected < post.getFundsNeeded();
|
---|
188 | }).collect(Collectors.toList());
|
---|
189 |
|
---|
190 | int start = (int) pageable.getOffset();
|
---|
191 | int end = Math.min((start + pageable.getPageSize()), expired.size());
|
---|
192 | if (start <= end) {
|
---|
193 | postList = new PageImpl<>(expired.subList(start, end), pageable, expired.size());
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (postList.getTotalElements() == 0) {
|
---|
199 | model.addAttribute("noPosts", true);
|
---|
200 | return "album";
|
---|
201 | }
|
---|
202 | model.addAttribute("totalPages", postList.getTotalPages());
|
---|
203 | model.addAttribute("postList", postList);
|
---|
204 | return "album";
|
---|
205 | }
|
---|
206 |
|
---|
207 | @RequestMapping("/post")
|
---|
208 | public String showPost(Model model, @RequestParam Long postid) {
|
---|
209 | DonationPost post = donationPostService.getById(postid);
|
---|
210 | if (post == null) {
|
---|
211 | model.addAttribute("notFound", true);
|
---|
212 | return "post";
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (post.getApproved() || (post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName()) && !post.getApproved())) {
|
---|
216 | AppUser user = post.getUser();
|
---|
217 | Moderator moderator = post.getModerator();
|
---|
218 | model.addAttribute("post", post);
|
---|
219 | model.addAttribute("createdByFirstName", user.getFirstName());
|
---|
220 | model.addAttribute("createdByLastName", user.getLastName());
|
---|
221 | if (moderator != null) {
|
---|
222 | model.addAttribute("moderatorFirstName", moderator.getFirstName());
|
---|
223 | model.addAttribute("moderatorLastName", moderator.getLastName());
|
---|
224 | }
|
---|
225 | double total = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();
|
---|
226 | model.addAttribute("total", total);
|
---|
227 | } else {
|
---|
228 | model.addAttribute("notFound", true);
|
---|
229 | }
|
---|
230 | return "post";
|
---|
231 | }
|
---|
232 |
|
---|
233 | @RequestMapping(value="/donate", method = RequestMethod.POST)
|
---|
234 | public String donate(Model model, @RequestParam Long postid,
|
---|
235 | @RequestParam String cardName,
|
---|
236 | @RequestParam String cardNumber,
|
---|
237 | @RequestParam String expiryDate,
|
---|
238 | @RequestParam String cvv,
|
---|
239 | @RequestParam String amount) {
|
---|
240 |
|
---|
241 | DonationPost post = donationPostService.getById(postid);
|
---|
242 | if(post == null || !post.getApproved()) {
|
---|
243 | return "index";
|
---|
244 | }
|
---|
245 |
|
---|
246 | float donatedAmount;
|
---|
247 | try {
|
---|
248 | donatedAmount = Float.parseFloat(amount);
|
---|
249 | if (donatedAmount <= 0) {
|
---|
250 | return String.format("redirect:/post?postid=%d&error", postid);
|
---|
251 | }
|
---|
252 | post.setFundsNeeded(donatedAmount);
|
---|
253 | } catch (NumberFormatException e) {
|
---|
254 | return String.format("redirect:/post?postid=%d&error", postid);
|
---|
255 | }
|
---|
256 |
|
---|
257 | FundsCollected funds = new FundsCollected("Online donation", donatedAmount);
|
---|
258 | fundsCollectedService.save(funds);
|
---|
259 |
|
---|
260 | post.getFundsCollected().add(funds);
|
---|
261 | donationPostService.save(post);
|
---|
262 |
|
---|
263 | DonationInformation donationInformation = new DonationInformation(donatedAmount, post.getId(), post.getTitle());
|
---|
264 | donationInformationService.save(donationInformation);
|
---|
265 | MainUser user = (MainUser) userService.loadUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
|
---|
266 | user.getDonationInformation().add(donationInformation);
|
---|
267 | userService.saveUser(user);
|
---|
268 |
|
---|
269 | return String.format("redirect:/post?postid=%d", postid);
|
---|
270 | }
|
---|
271 |
|
---|
272 | @RequestMapping(value="/report", method = RequestMethod.POST)
|
---|
273 | public String report(@RequestParam Long postid,
|
---|
274 | @RequestParam String description) {
|
---|
275 |
|
---|
276 | DonationPost donationPost = donationPostService.getById(postid);
|
---|
277 | ReportPost reportPost = reportPostService.findByDonationPost(donationPost);
|
---|
278 | if (reportPost == null) {
|
---|
279 | reportPost = new ReportPost();
|
---|
280 | reportPost.setDonationPost(donationPost);
|
---|
281 | }
|
---|
282 |
|
---|
283 | Reason reason = new Reason();
|
---|
284 | AppUser user = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
---|
285 | reason.setUser(user);
|
---|
286 | reason.setDescription(description);
|
---|
287 | reasonService.save(reason);
|
---|
288 | reportPost.getReasons().add(reason);
|
---|
289 | reportPost.setNumReports(reportPost.getNumReports() + 1);
|
---|
290 | reportPostService.save(reportPost);
|
---|
291 | return String.format("redirect:/post?postid=%d", postid);
|
---|
292 | }
|
---|
293 |
|
---|
294 | @ModelAttribute("user")
|
---|
295 | public AppUser addAttributes() {
|
---|
296 | if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
|
---|
297 | return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
---|
298 | }
|
---|
299 | return null;
|
---|
300 | }
|
---|
301 | }
|
---|