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.format.annotation.DateTimeFormat;
|
---|
7 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
8 | import org.springframework.stereotype.Controller;
|
---|
9 | import org.springframework.ui.Model;
|
---|
10 | import org.springframework.util.StringUtils;
|
---|
11 | import org.springframework.web.bind.annotation.*;
|
---|
12 | import org.springframework.web.multipart.MultipartFile;
|
---|
13 |
|
---|
14 | import java.io.IOException;
|
---|
15 | import java.time.LocalDate;
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.Arrays;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.Objects;
|
---|
20 | import java.util.stream.Collectors;
|
---|
21 |
|
---|
22 | @Controller
|
---|
23 | public class DonationPostController {
|
---|
24 |
|
---|
25 | private final DonationPostService donationPostService;
|
---|
26 | private final UserService userService;
|
---|
27 | private final FundsCollectedService fundsCollectedService;
|
---|
28 | private final 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) {
|
---|
33 | this.donationPostService = donationPostService;
|
---|
34 | this.userService = userService;
|
---|
35 | this.fundsCollectedService = fundsCollectedService;
|
---|
36 | this.donationInformationService = donationInformationService;
|
---|
37 | this.reportPostService = reportPostService;
|
---|
38 | this.reasonService = reasonService;
|
---|
39 | }
|
---|
40 |
|
---|
41 | @RequestMapping("/upload")
|
---|
42 | public String upload() {
|
---|
43 | return "upload";
|
---|
44 | }
|
---|
45 |
|
---|
46 | @RequestMapping(value = "/newPost", method = RequestMethod.POST)
|
---|
47 | public String newPost(Model model,
|
---|
48 | @RequestParam String title,
|
---|
49 | @RequestParam String fundsNeeded,
|
---|
50 | @RequestParam String currency,
|
---|
51 | @RequestParam String description,
|
---|
52 | @RequestParam String telekom,
|
---|
53 | @RequestParam String a1,
|
---|
54 | @RequestParam(defaultValue = "2021-01-01") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate dateDue,
|
---|
55 | @RequestParam String bankAccount,
|
---|
56 | @RequestParam MultipartFile titleImage,
|
---|
57 | @RequestParam MultipartFile[] images,
|
---|
58 | @RequestParam MultipartFile[] moderatorImages) {
|
---|
59 |
|
---|
60 | DonationPost post = new DonationPost();
|
---|
61 | post.setTitle(title);
|
---|
62 |
|
---|
63 | try {
|
---|
64 | float funds = Float.parseFloat(fundsNeeded);
|
---|
65 | post.setFundsNeeded(funds);
|
---|
66 | } catch (NumberFormatException e) {
|
---|
67 | e.printStackTrace();
|
---|
68 | }
|
---|
69 |
|
---|
70 | post.setCurrency(currency);
|
---|
71 | post.setDescription(description);
|
---|
72 | post.setDateDue(dateDue);
|
---|
73 | post.setBankAccount(bankAccount);
|
---|
74 | post.setApproved(false);
|
---|
75 |
|
---|
76 | List<String> phoneNumbers = Arrays.asList(telekom, a1);
|
---|
77 |
|
---|
78 | List<String> photos = new ArrayList<>();
|
---|
79 | photos.add(StringUtils.cleanPath(Objects.requireNonNull(titleImage.getOriginalFilename())));
|
---|
80 | Arrays.stream(images).filter(i -> !i.isEmpty()).forEach(i -> photos.add(StringUtils.cleanPath(Objects.requireNonNull(i.getOriginalFilename()))));
|
---|
81 |
|
---|
82 | List<MultipartFile> files = new ArrayList<>();
|
---|
83 | files.add(titleImage);
|
---|
84 | files.addAll(Arrays.stream(images).filter(i -> !i.isEmpty()).collect(Collectors.toList()));
|
---|
85 |
|
---|
86 | List<String> moderatorPhotos = new ArrayList<>();
|
---|
87 | Arrays.stream(moderatorImages).forEach(i -> moderatorPhotos.add(StringUtils.cleanPath(Objects.requireNonNull(i.getOriginalFilename()))));
|
---|
88 |
|
---|
89 | post.setPhoneNumbers(phoneNumbers);
|
---|
90 | post.setImages(photos);
|
---|
91 | post.setModeratorImages(moderatorPhotos);
|
---|
92 |
|
---|
93 | AppUser user = (AppUser) model.getAttribute("user");
|
---|
94 | post.setUser(user);
|
---|
95 |
|
---|
96 | DonationPost savedPost = donationPostService.save(post);
|
---|
97 |
|
---|
98 | for (MultipartFile file : files) {
|
---|
99 | String fileName = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
|
---|
100 | String uploadDir = "post-photos/" + savedPost.getId();
|
---|
101 | try {
|
---|
102 | FileUploadUtil.saveFile(uploadDir, fileName, file);
|
---|
103 | } catch (IOException e) {
|
---|
104 | e.printStackTrace();
|
---|
105 | }
|
---|
106 |
|
---|
107 | }
|
---|
108 |
|
---|
109 | for (MultipartFile file : moderatorImages) {
|
---|
110 |
|
---|
111 | String fileName = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
|
---|
112 | String uploadDir = "moderator-photos/" + savedPost.getId();
|
---|
113 | try {
|
---|
114 | FileUploadUtil.saveFile(uploadDir, fileName, file);
|
---|
115 | } catch (IOException e) {
|
---|
116 | e.printStackTrace();
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | return "upload";
|
---|
121 | }
|
---|
122 |
|
---|
123 | @RequestMapping("/album")
|
---|
124 | public String album(Model model) {
|
---|
125 | List<DonationPost> postList = donationPostService.findAllByApproved(true);
|
---|
126 | if (postList.size() == 0) {
|
---|
127 | model.addAttribute("noPosts", true);
|
---|
128 | return "album";
|
---|
129 | }
|
---|
130 | model.addAttribute("postList", postList);
|
---|
131 | return "album";
|
---|
132 | }
|
---|
133 |
|
---|
134 | @RequestMapping("/post")
|
---|
135 | public String showPost(Model model, @RequestParam Long postid) {
|
---|
136 | DonationPost post = donationPostService.getById(postid);
|
---|
137 | if (post == null) {
|
---|
138 | model.addAttribute("notFound", true);
|
---|
139 | return "post";
|
---|
140 | }
|
---|
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 | }
|
---|
157 | return "post";
|
---|
158 | }
|
---|
159 |
|
---|
160 | @RequestMapping("/donate")
|
---|
161 | public String donate(Model model, @RequestParam Long postid,
|
---|
162 | @RequestParam String cardName,
|
---|
163 | @RequestParam String cardNumber,
|
---|
164 | @RequestParam String expiryDate,
|
---|
165 | @RequestParam String cvv,
|
---|
166 | @RequestParam float amount) {
|
---|
167 |
|
---|
168 | DonationPost post = donationPostService.getById(postid);
|
---|
169 | FundsCollected funds = new FundsCollected("Online donation", amount);
|
---|
170 | fundsCollectedService.save(funds);
|
---|
171 |
|
---|
172 | post.getFundsCollected().add(funds);
|
---|
173 | donationPostService.save(post);
|
---|
174 |
|
---|
175 | DonationInformation donationInformation = new DonationInformation(amount, post.getId(), post.getTitle());
|
---|
176 | donationInformationService.save(donationInformation);
|
---|
177 | MainUser user = (MainUser) userService.loadUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
|
---|
178 | user.getDonationInformation().add(donationInformation);
|
---|
179 | userService.saveUser(user);
|
---|
180 |
|
---|
181 | return String.format("redirect:/post?postid=%d", postid);
|
---|
182 | }
|
---|
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 |
|
---|
205 | @ModelAttribute("user")
|
---|
206 | public AppUser addAttributes() {
|
---|
207 | if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
|
---|
208 | return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
---|
209 | }
|
---|
210 | return null;
|
---|
211 | }
|
---|
212 | }
|
---|