source: src/main/java/it/finki/charitable/controller/DonationPostController.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: 7.9 KB
Line 
1package it.finki.charitable.controller;
2
3import it.finki.charitable.entities.*;
4import it.finki.charitable.services.DonationInformationService;
5import it.finki.charitable.services.DonationPostService;
6import it.finki.charitable.services.FundsCollectedService;
7import it.finki.charitable.services.UserService;
8import it.finki.charitable.util.FileUploadUtil;
9import org.springframework.data.domain.Page;
10import org.springframework.format.annotation.DateTimeFormat;
11import org.springframework.security.core.context.SecurityContextHolder;
12import org.springframework.stereotype.Controller;
13import org.springframework.ui.Model;
14import org.springframework.util.StringUtils;
15import org.springframework.web.bind.annotation.*;
16import org.springframework.web.multipart.MultipartFile;
17
18import java.io.File;
19import java.io.IOException;
20import java.time.LocalDate;
21import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.List;
24import java.util.Objects;
25import java.util.stream.Collectors;
26
27@Controller
28public class DonationPostController {
29
30 private final DonationPostService donationPostService;
31 private final UserService userService;
32 private final FundsCollectedService fundsCollectedService;
33 private final DonationInformationService donationInformationService;
34
35 public DonationPostController(DonationPostService donationPostService, UserService userService, FundsCollectedService fundsCollectedService, DonationInformationService donationInformationService) {
36 this.donationPostService = donationPostService;
37 this.userService = userService;
38 this.fundsCollectedService = fundsCollectedService;
39 this.donationInformationService = donationInformationService;
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 List<DonationPost> postList = donationPostService.findAllByApproved(true);
127 if (postList.size() == 0) {
128 model.addAttribute("noPosts", true);
129 return "album";
130 }
131 model.addAttribute("postList", postList);
132 return "album";
133 }
134
135 @RequestMapping("/post")
136 public String showPost(Model model, @RequestParam Long postid) {
137 DonationPost post = donationPostService.getById(postid);
138 if (post == null) {
139 model.addAttribute("notFound", true);
140 return "post";
141 }
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);
148 return "post";
149 }
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
166 @RequestMapping("/donate")
167 public String donate(Model model, @RequestParam Long postid,
168 @RequestParam String cardName,
169 @RequestParam String cardNumber,
170 @RequestParam String expiryDate,
171 @RequestParam String cvv,
172 @RequestParam float amount) {
173
174 DonationPost post = donationPostService.getById(postid);
175 FundsCollected funds = new FundsCollected("Online donation", amount);
176 fundsCollectedService.save(funds);
177
178 post.getFundsCollected().add(funds);
179 donationPostService.save(post);
180
181 DonationInformation donationInformation = new DonationInformation(amount, post.getId(), post.getTitle());
182 donationInformationService.save(donationInformation);
183 MainUser user = (MainUser) userService.loadUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
184 user.getDonationInformation().add(donationInformation);
185 userService.saveUser(user);
186
187 return String.format("redirect:/post?postid=%d", postid);
188 }
189
190 @ModelAttribute("user")
191 public AppUser addAttributes() {
192 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
193 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
194 }
195 return null;
196 }
197}
Note: See TracBrowser for help on using the repository browser.