source: src/main/java/it/finki/charitable/controller/DonationPostController.java@ 5577566

Last change on this file since 5577566 was 5577566, checked in by NikolaCenevski <cenevskinikola@…>, 3 years ago

prototip part 2

  • Property mode set to 100644
File size: 8.0 KB
Line 
1package it.finki.charitable.controller;
2
3import it.finki.charitable.entities.AppUser;
4import it.finki.charitable.entities.DonationInformation;
5import it.finki.charitable.entities.DonationPost;
6import it.finki.charitable.entities.FundsCollected;
7import it.finki.charitable.services.DonationInformationService;
8import it.finki.charitable.services.DonationPostService;
9import it.finki.charitable.services.FundsCollectedService;
10import it.finki.charitable.services.UserService;
11import it.finki.charitable.util.FileUploadUtil;
12import org.springframework.data.domain.Page;
13import org.springframework.format.annotation.DateTimeFormat;
14import org.springframework.security.core.context.SecurityContextHolder;
15import org.springframework.stereotype.Controller;
16import org.springframework.ui.Model;
17import org.springframework.util.StringUtils;
18import org.springframework.web.bind.annotation.*;
19import org.springframework.web.multipart.MultipartFile;
20
21import java.io.File;
22import java.io.IOException;
23import java.time.LocalDate;
24import java.util.ArrayList;
25import java.util.Arrays;
26import java.util.List;
27import java.util.Objects;
28import java.util.stream.Collectors;
29
30@Controller
31public class DonationPostController {
32
33 private final DonationPostService donationPostService;
34 private final UserService userService;
35 private final FundsCollectedService fundsCollectedService;
36 private final DonationInformationService donationInformationService;
37
38 public DonationPostController(DonationPostService donationPostService, UserService userService, FundsCollectedService fundsCollectedService, DonationInformationService donationInformationService) {
39 this.donationPostService = donationPostService;
40 this.userService = userService;
41 this.fundsCollectedService = fundsCollectedService;
42 this.donationInformationService = donationInformationService;
43 }
44
45 @RequestMapping("/upload")
46 public String upload() {
47 return "upload";
48 }
49
50 @RequestMapping(value = "/newPost", method = RequestMethod.POST)
51 public String newPost(Model model,
52 @RequestParam String title,
53 @RequestParam String fundsNeeded,
54 @RequestParam String currency,
55 @RequestParam String description,
56 @RequestParam String telekom,
57 @RequestParam String a1,
58 @RequestParam(defaultValue = "2021-01-01") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate dateDue,
59 @RequestParam String bankAccount,
60 @RequestParam MultipartFile titleImage,
61 @RequestParam MultipartFile[] images,
62 @RequestParam MultipartFile[] moderatorImages) {
63
64 DonationPost post = new DonationPost();
65 post.setTitle(title);
66
67 try {
68 float funds = Float.parseFloat(fundsNeeded);
69 post.setFundsNeeded(funds);
70 } catch (NumberFormatException e) {
71 e.printStackTrace();
72 }
73
74 post.setCurrency(currency);
75 post.setDescription(description);
76 post.setDateDue(dateDue);
77 post.setBankAccount(bankAccount);
78
79 List<String> phoneNumbers = Arrays.asList(telekom, a1);
80
81 List<String> photos = new ArrayList<>();
82 photos.add(StringUtils.cleanPath(Objects.requireNonNull(titleImage.getOriginalFilename())));
83 Arrays.stream(images).filter(i -> !i.isEmpty()).forEach(i -> photos.add(StringUtils.cleanPath(Objects.requireNonNull(i.getOriginalFilename()))));
84
85 List<MultipartFile> files = new ArrayList<>();
86 files.add(titleImage);
87 files.addAll(Arrays.stream(images).filter(i -> !i.isEmpty()).collect(Collectors.toList()));
88
89 List<String> moderatorPhotos = new ArrayList<>();
90 Arrays.stream(moderatorImages).forEach(i -> moderatorPhotos.add(StringUtils.cleanPath(Objects.requireNonNull(i.getOriginalFilename()))));
91
92 post.setPhoneNumbers(phoneNumbers);
93 post.setImages(photos);
94 post.setModeratorImages(moderatorPhotos);
95
96 AppUser user = (AppUser) model.getAttribute("user");
97 post.setUser(user);
98
99 DonationPost savedPost = donationPostService.save(post);
100
101 for (MultipartFile file : files) {
102 String fileName = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
103 String uploadDir = "post-photos/" + savedPost.getId();
104 try {
105 FileUploadUtil.saveFile(uploadDir, fileName, file);
106 } catch (IOException e) {
107 e.printStackTrace();
108 }
109
110 }
111
112 for (MultipartFile file : moderatorImages) {
113
114 String fileName = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
115 String uploadDir = "moderator-photos/" + savedPost.getId();
116 try {
117 FileUploadUtil.saveFile(uploadDir, fileName, file);
118 } catch (IOException e) {
119 e.printStackTrace();
120 }
121 }
122
123 return "upload";
124 }
125
126 @RequestMapping("/album")
127 public String album(Model model) {
128 List<DonationPost> postList = donationPostService.findAll();
129 if (postList.size() == 0) {
130 model.addAttribute("noPosts", true);
131 return "album";
132 }
133 model.addAttribute("postList", postList);
134 return "album";
135 }
136
137 @RequestMapping("/post")
138 public String showPost(Model model, @RequestParam Long postid) {
139 DonationPost post = donationPostService.getById(postid);
140 if (post == null) {
141 model.addAttribute("notFound", true);
142 return "post";
143 }
144 AppUser user = post.getUser();
145 model.addAttribute("post", post);
146 model.addAttribute("createdByFirstName", user.getFirstName());
147 model.addAttribute("createdByLastName", user.getLastName());
148 double total = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();
149 model.addAttribute("total", total);
150 return "post";
151 }
152
153 @RequestMapping("/deletePost")
154 public String deletePost(@RequestParam Long postid) {
155 DonationPost post = donationPostService.getById(postid);
156 if (post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName())) {
157 List<String> fileForDeletion = post.getPhotosForDeletion();
158 for (String f : fileForDeletion) {
159 File file = new File(f);
160 file.delete();
161 }
162 donationPostService.delete(post);
163 }
164
165 return "redirect:/myPosts";
166 }
167
168 @RequestMapping("/donate")
169 public String donate(Model model, @RequestParam Long postid,
170 @RequestParam String cardName,
171 @RequestParam String cardNumber,
172 @RequestParam String expiryDate,
173 @RequestParam String cvv,
174 @RequestParam float amount) {
175
176 DonationPost post = donationPostService.getById(postid);
177 FundsCollected funds = new FundsCollected("online", amount);
178 fundsCollectedService.save(funds);
179
180 post.getFundsCollected().add(funds);
181 donationPostService.save(post);
182
183 DonationInformation donationInformation = new DonationInformation(amount, post.getId(), post.getTitle());
184 donationInformationService.save(donationInformation);
185 AppUser user = userService.loadUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
186 user.getDonationInformation().add(donationInformation);
187 userService.saveUser(user);
188
189 return String.format("redirect:/post?postid=%d", postid);
190 }
191
192 @ModelAttribute("user")
193 public AppUser addAttributes() {
194 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
195 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
196 }
197 return null;
198 }
199}
Note: See TracBrowser for help on using the repository browser.