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

Last change on this file since f8007b3 was f0d5cb7, checked in by KostaFortumanov <kfortumanov@…>, 3 years ago

Added moderator class

  • Property mode set to 100644
File size: 7.8 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
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.findAll();
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 AppUser user = post.getUser();
142 model.addAttribute("post", post);
143 model.addAttribute("createdByFirstName", user.getFirstName());
144 model.addAttribute("createdByLastName", user.getLastName());
145 double total = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();
146 model.addAttribute("total", total);
147 return "post";
148 }
149
150 @RequestMapping("/deletePost")
151 public String deletePost(@RequestParam Long postid) {
152 DonationPost post = donationPostService.getById(postid);
153 if (post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName())) {
154 List<String> fileForDeletion = post.getPhotosForDeletion();
155 for (String f : fileForDeletion) {
156 File file = new File(f);
157 file.delete();
158 }
159 donationPostService.delete(post);
160 }
161
162 return "redirect:/myPosts";
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", 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 @ModelAttribute("user")
190 public AppUser addAttributes() {
191 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
192 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
193 }
194 return null;
195 }
196}
Note: See TracBrowser for help on using the repository browser.