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.Page;
|
---|
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.ArrayList;
|
---|
18 | import java.util.Arrays;
|
---|
19 | import java.util.List;
|
---|
20 | import java.util.Objects;
|
---|
21 | import java.util.stream.Collectors;
|
---|
22 |
|
---|
23 | @Controller
|
---|
24 | public class DonationPostController {
|
---|
25 |
|
---|
26 | private final DonationPostService donationPostService;
|
---|
27 | private final UserService userService;
|
---|
28 | private final FundsCollectedService fundsCollectedService;
|
---|
29 | private final DonationInformationService donationInformationService;
|
---|
30 | private final ReportPostService reportPostService;
|
---|
31 | private final ReasonService reasonService;
|
---|
32 |
|
---|
33 | public DonationPostController(DonationPostService donationPostService, UserService userService, FundsCollectedService fundsCollectedService, DonationInformationService donationInformationService, ReportPostService reportPostService, ReasonService reasonService) {
|
---|
34 | this.donationPostService = donationPostService;
|
---|
35 | this.userService = userService;
|
---|
36 | this.fundsCollectedService = fundsCollectedService;
|
---|
37 | this.donationInformationService = donationInformationService;
|
---|
38 | this.reportPostService = reportPostService;
|
---|
39 | this.reasonService = reasonService;
|
---|
40 | }
|
---|
41 |
|
---|
42 | @RequestMapping("/upload")
|
---|
43 | public String upload() {
|
---|
44 | return "upload";
|
---|
45 | }
|
---|
46 |
|
---|
47 | @RequestMapping(value = "/newPost", method = RequestMethod.POST)
|
---|
48 | public String newPost(Model model,
|
---|
49 | @RequestParam String title,
|
---|
50 | @RequestParam String fundsNeeded,
|
---|
51 | @RequestParam String currency,
|
---|
52 | @RequestParam String description,
|
---|
53 | @RequestParam String telekom,
|
---|
54 | @RequestParam String a1,
|
---|
55 | @RequestParam(defaultValue = "2021-01-01") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate dateDue,
|
---|
56 | @RequestParam String bankAccount,
|
---|
57 | @RequestParam MultipartFile titleImage,
|
---|
58 | @RequestParam MultipartFile[] images,
|
---|
59 | @RequestParam MultipartFile[] moderatorImages) {
|
---|
60 |
|
---|
61 | DonationPost post = new DonationPost();
|
---|
62 | post.setTitle(title);
|
---|
63 |
|
---|
64 | try {
|
---|
65 | float funds = Float.parseFloat(fundsNeeded);
|
---|
66 | post.setFundsNeeded(funds);
|
---|
67 | } catch (NumberFormatException e) {
|
---|
68 | e.printStackTrace();
|
---|
69 | }
|
---|
70 |
|
---|
71 | post.setCurrency(currency);
|
---|
72 | post.setDescription(description);
|
---|
73 | post.setDateDue(dateDue);
|
---|
74 | post.setBankAccount(bankAccount);
|
---|
75 | post.setApproved(false);
|
---|
76 |
|
---|
77 | List<String> phoneNumbers = Arrays.asList(telekom, a1);
|
---|
78 |
|
---|
79 | List<String> photos = new ArrayList<>();
|
---|
80 | photos.add(StringUtils.cleanPath(Objects.requireNonNull(titleImage.getOriginalFilename())));
|
---|
81 | Arrays.stream(images).filter(i -> !i.isEmpty()).forEach(i -> photos.add(StringUtils.cleanPath(Objects.requireNonNull(i.getOriginalFilename()))));
|
---|
82 |
|
---|
83 | List<MultipartFile> files = new ArrayList<>();
|
---|
84 | files.add(titleImage);
|
---|
85 | files.addAll(Arrays.stream(images).filter(i -> !i.isEmpty()).collect(Collectors.toList()));
|
---|
86 |
|
---|
87 | List<String> moderatorPhotos = new ArrayList<>();
|
---|
88 | Arrays.stream(moderatorImages).forEach(i -> moderatorPhotos.add(StringUtils.cleanPath(Objects.requireNonNull(i.getOriginalFilename()))));
|
---|
89 |
|
---|
90 | post.setPhoneNumbers(phoneNumbers);
|
---|
91 | post.setImages(photos);
|
---|
92 | post.setModeratorImages(moderatorPhotos);
|
---|
93 |
|
---|
94 | AppUser user = (AppUser) model.getAttribute("user");
|
---|
95 | post.setUser(user);
|
---|
96 |
|
---|
97 | DonationPost savedPost = donationPostService.save(post);
|
---|
98 |
|
---|
99 | for (MultipartFile file : files) {
|
---|
100 | String fileName = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
|
---|
101 | String uploadDir = "post-photos/" + savedPost.getId();
|
---|
102 | try {
|
---|
103 | FileUploadUtil.saveFile(uploadDir, fileName, file);
|
---|
104 | } catch (IOException e) {
|
---|
105 | e.printStackTrace();
|
---|
106 | }
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 | for (MultipartFile file : moderatorImages) {
|
---|
111 |
|
---|
112 | String fileName = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
|
---|
113 | String uploadDir = "moderator-photos/" + savedPost.getId();
|
---|
114 | try {
|
---|
115 | FileUploadUtil.saveFile(uploadDir, fileName, file);
|
---|
116 | } catch (IOException e) {
|
---|
117 | e.printStackTrace();
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | return "upload";
|
---|
122 | }
|
---|
123 |
|
---|
124 | @RequestMapping("/album")
|
---|
125 | public String album(Model model,
|
---|
126 | @RequestParam int page,
|
---|
127 | @RequestParam String sort,
|
---|
128 | @RequestParam(required = false,defaultValue = "") String order) {
|
---|
129 | Page<DonationPost> postList = donationPostService.findPaginated(page,6, sort, order);
|
---|
130 | if (postList.getSize() == 0) {
|
---|
131 | model.addAttribute("noPosts", true);
|
---|
132 | return "album";
|
---|
133 | }
|
---|
134 | model.addAttribute("totalPages", postList.getTotalPages());
|
---|
135 | model.addAttribute("postList", postList);
|
---|
136 | return "album";
|
---|
137 | }
|
---|
138 |
|
---|
139 | @RequestMapping("/post")
|
---|
140 | public String showPost(Model model, @RequestParam Long postid) {
|
---|
141 | DonationPost post = donationPostService.getById(postid);
|
---|
142 | if (post == null) {
|
---|
143 | model.addAttribute("notFound", true);
|
---|
144 | return "post";
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (post.getApproved() || (post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName()) && !post.getApproved())) {
|
---|
148 | AppUser user = post.getUser();
|
---|
149 | Moderator moderator = post.getModerator();
|
---|
150 | model.addAttribute("post", post);
|
---|
151 | model.addAttribute("createdByFirstName", user.getFirstName());
|
---|
152 | model.addAttribute("createdByLastName", user.getLastName());
|
---|
153 | if (moderator != null) {
|
---|
154 | model.addAttribute("moderatorFirstName", moderator.getFirstName());
|
---|
155 | model.addAttribute("moderatorLastName", moderator.getLastName());
|
---|
156 | }
|
---|
157 | double total = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();
|
---|
158 | model.addAttribute("total", total);
|
---|
159 | } else {
|
---|
160 | model.addAttribute("notFound", true);
|
---|
161 | }
|
---|
162 | return "post";
|
---|
163 | }
|
---|
164 |
|
---|
165 | @RequestMapping("/donate")
|
---|
166 | public String donate(Model model, @RequestParam Long postid,
|
---|
167 | @RequestParam String cardName,
|
---|
168 | @RequestParam String cardNumber,
|
---|
169 | @RequestParam String expiryDate,
|
---|
170 | @RequestParam String cvv,
|
---|
171 | @RequestParam float amount) {
|
---|
172 |
|
---|
173 | DonationPost post = donationPostService.getById(postid);
|
---|
174 | FundsCollected funds = new FundsCollected("Online donation", amount);
|
---|
175 | fundsCollectedService.save(funds);
|
---|
176 |
|
---|
177 | post.getFundsCollected().add(funds);
|
---|
178 | donationPostService.save(post);
|
---|
179 |
|
---|
180 | DonationInformation donationInformation = new DonationInformation(amount, post.getId(), post.getTitle());
|
---|
181 | donationInformationService.save(donationInformation);
|
---|
182 | MainUser user = (MainUser) userService.loadUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
|
---|
183 | user.getDonationInformation().add(donationInformation);
|
---|
184 | userService.saveUser(user);
|
---|
185 |
|
---|
186 | return String.format("redirect:/post?postid=%d", postid);
|
---|
187 | }
|
---|
188 |
|
---|
189 | @RequestMapping("/report")
|
---|
190 | public String report(@RequestParam Long postid,
|
---|
191 | @RequestParam String description) {
|
---|
192 |
|
---|
193 | DonationPost donationPost = donationPostService.getById(postid);
|
---|
194 | ReportPost reportPost = reportPostService.findByDonationPost(donationPost);
|
---|
195 | if(reportPost == null) {
|
---|
196 | reportPost = new ReportPost();
|
---|
197 | reportPost.setDonationPost(donationPost);
|
---|
198 | }
|
---|
199 |
|
---|
200 | Reason reason = new Reason();
|
---|
201 | AppUser user = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
---|
202 | reason.setUser(user);
|
---|
203 | reason.setDescription(description);
|
---|
204 | reasonService.save(reason);
|
---|
205 | reportPost.getReasons().add(reason);
|
---|
206 | reportPostService.save(reportPost);
|
---|
207 | return String.format("redirect:/post?postid=%d", postid);
|
---|
208 | }
|
---|
209 |
|
---|
210 | @ModelAttribute("user")
|
---|
211 | public AppUser addAttributes() {
|
---|
212 | if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
|
---|
213 | return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
---|
214 | }
|
---|
215 | return null;
|
---|
216 | }
|
---|
217 | }
|
---|