Changeset b8dc761
- Timestamp:
- 01/05/22 15:57:29 (3 years ago)
- Branches:
- master
- Children:
- 6fa3d09
- Parents:
- 881a233
- Files:
-
- 4 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
pom.xml
r881a233 rb8dc761 69 69 <artifactId>spring-boot-starter-mail</artifactId> 70 70 </dependency> 71 <dependency> 72 <groupId>org.springframework.boot</groupId> 73 <artifactId>spring-boot-starter-oauth2-client</artifactId> 74 </dependency> 71 75 </dependencies> 72 76 -
src/main/java/it/finki/charitable/controller/DonationPostController.java
r881a233 rb8dc761 14 14 15 15 import java.io.IOException; 16 import java.time.Duration; 16 17 import java.time.LocalDate; 17 18 import java.util.*; … … 87 88 post.setBankAccount(bankAccount); 88 89 post.setApproved(false); 90 post.setCreatedAt(LocalDate.now()); 91 long totalDays = Duration.between(post.getCreatedAt().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays(); 92 if(totalDays < 10) 93 post.setRiskFactor(0); 89 94 90 95 List<String> phoneNumbers = Arrays.asList(telekom, a1); … … 169 174 allPosts.sort(Comparator.comparing(DonationPost::getFundsNeeded).reversed()); 170 175 } 176 } else if (sort.equals("riskFactor")) { 177 if (order.equals("asc")) { 178 allPosts.sort(Comparator.comparing(DonationPost::getRiskFactor)); 179 } else { 180 allPosts.sort(Comparator.comparing(DonationPost::getRiskFactor).reversed()); 181 } 171 182 } 172 183 173 184 if (groupBy.equals("completed")) { 174 List<DonationPost> completed = allPosts.stream().filter(post -> { 175 double fundsCollected = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum(); 176 return fundsCollected >= post.getFundsNeeded(); 177 }).collect(Collectors.toList()); 185 List<DonationPost> completed = allPosts.stream() 186 .filter(post -> post.getTotalFundsCollected() >= post.getFundsNeeded()) 187 .collect(Collectors.toList()); 178 188 179 189 int start = (int) pageable.getOffset(); … … 213 223 } 214 224 215 if (post.getApproved() || (post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName()) && !post.getApproved())) { 225 AppUser currentUser = (AppUser) model.getAttribute("user"); 226 if (post.getApproved() || (post.getUser().getUsername().equals(currentUser.getUsername()) && !post.getApproved())) { 216 227 AppUser user = post.getUser(); 217 228 Moderator moderator = post.getModerator(); … … 223 234 model.addAttribute("moderatorLastName", moderator.getLastName()); 224 235 } 225 double total = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();226 model.addAttribute("total", total);227 236 } else { 228 237 model.addAttribute("notFound", true); … … 250 259 return String.format("redirect:/post?postid=%d&error", postid); 251 260 } 252 post.setFundsNeeded(donatedAmount);253 261 } catch (NumberFormatException e) { 254 262 return String.format("redirect:/post?postid=%d&error", postid); … … 259 267 260 268 post.getFundsCollected().add(funds); 269 post.setTotalFundsCollected(post.getTotalFundsCollected() + donatedAmount); 270 271 if(post.getRiskFactor() != 101) { 272 post.setRiskFactor(getRisk(post)); 273 } 274 261 275 donationPostService.save(post); 262 276 … … 295 309 public AppUser addAttributes() { 296 310 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 297 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 311 312 String email = SecurityContextHolder.getContext().getAuthentication().getName(); 313 return userService.loadUserByUsername(email); 298 314 } 299 315 return null; 300 316 } 317 318 private Integer getRisk(DonationPost post) { 319 float dailyAverage = post.getTotalFundsCollected() / (Duration.between(post.getCreatedAt().atTime(0, 0, 0), LocalDate.now().atTime(0, 0, 0)).toDays()+1); 320 float neededAverage = (post.getFundsNeeded() - post.getTotalFundsCollected()) / (Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays()+1); 321 322 System.out.println(dailyAverage + " " + neededAverage); 323 int risk = (int) (dailyAverage / neededAverage * 100); 324 325 if(risk > 100) { 326 risk = 100; 327 } 328 329 if(Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays() == 0) { 330 risk = 0; 331 } 332 333 if(post.getFundsNeeded() <= post.getTotalFundsCollected()) { 334 risk = 101; 335 } 336 337 return risk; 338 } 301 339 } -
src/main/java/it/finki/charitable/controller/HomeController.java
r881a233 rb8dc761 129 129 @ModelAttribute("user") 130 130 public AppUser addAttributes() { 131 if(SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 132 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 131 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 132 String email = SecurityContextHolder.getContext().getAuthentication().getName(); 133 System.out.println(email); 134 return userService.loadUserByUsername(email); 133 135 } 134 136 return null; -
src/main/java/it/finki/charitable/controller/UserProfileController.java
r881a233 rb8dc761 115 115 fundsCollectedService.save(funds); 116 116 post.getFundsCollected().add(funds); 117 post.setTotalFundsCollected(post.getTotalFundsCollected() + amount); 117 118 donationPostService.save(post); 118 119 } … … 122 123 @ModelAttribute("user") 123 124 public AppUser addAttributes() { 124 if(SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 125 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 125 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 126 127 String email = SecurityContextHolder.getContext().getAuthentication().getName(); 128 return userService.loadUserByUsername(email); 126 129 } 127 130 return null; -
src/main/java/it/finki/charitable/entities/DonationPost.java
r881a233 rb8dc761 61 61 private List<FundsCollected> fundsCollected = new ArrayList<>(); 62 62 63 private Float totalFundsCollected = 0f; 64 private LocalDate createdAt; 65 private Integer riskFactor = 101; 66 63 67 @Transient 64 68 public List<String> getImagesPath() { … … 222 226 this.fundsCollected = fundsCollected; 223 227 } 228 229 public Float getTotalFundsCollected() { 230 return totalFundsCollected; 231 } 232 233 public void setTotalFundsCollected(Float totalFundsCollected) { 234 this.totalFundsCollected = totalFundsCollected; 235 } 236 237 public LocalDate getCreatedAt() { 238 return createdAt; 239 } 240 241 public void setCreatedAt(LocalDate createdAt) { 242 this.createdAt = createdAt; 243 } 244 245 public Integer getRiskFactor() { 246 return riskFactor; 247 } 248 249 public void setRiskFactor(Integer riskFactor) { 250 this.riskFactor = riskFactor; 251 } 224 252 } -
src/main/java/it/finki/charitable/security/SecurityConfig.java
r881a233 rb8dc761 3 3 import it.finki.charitable.entities.UserRole; 4 4 import it.finki.charitable.services.UserService; 5 import org.springframework.beans.factory.annotation.Autowired; 5 6 import org.springframework.context.annotation.Configuration; 6 7 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 7 8 import org.springframework.security.config.annotation.web.builders.HttpSecurity; 8 9 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 9 import org.springframework.security.core.Authentication;10 10 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 11 11 import org.springframework.security.web.DefaultRedirectStrategy; … … 14 14 import org.springframework.security.web.util.matcher.AntPathRequestMatcher; 15 15 16 import javax.servlet.ServletException;17 import javax.servlet.http.HttpServletRequest;18 import javax.servlet.http.HttpServletResponse;19 import java.io.IOException;20 21 16 @Configuration 22 17 public class SecurityConfig extends WebSecurityConfigurerAdapter { 18 19 @Autowired 20 private UserO2AuthService userO2AuthService; 21 @Autowired 22 private O2AuthSuccessHandler o2AuthSuccessHandler; 23 23 24 24 private final UserService userService; … … 41 41 "/album/**", 42 42 "/post", 43 "/post-photos/**" 43 "/post-photos/**", 44 "/oauth2/authorization/google" 44 45 }; 45 46 … … 58 59 .successHandler(authenticationSuccessHandler) 59 60 .and() 61 .oauth2Login() 62 .loginPage("/login") 63 .userInfoEndpoint() 64 .userService(userO2AuthService) 65 .and() 66 .successHandler(o2AuthSuccessHandler) 67 .and() 60 68 .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 61 69 .logoutSuccessUrl("/").deleteCookies("remember-me") … … 73 81 }; 74 82 83 84 75 85 @Override 76 86 protected void configure(AuthenticationManagerBuilder auth) throws Exception { -
src/main/java/it/finki/charitable/services/EmailService.java
r881a233 rb8dc761 1 1 package it.finki.charitable.services; 2 2 3 import it.finki.charitable.entities.EmailMessage; 3 4 import org.springframework.mail.SimpleMailMessage; 4 5 import org.springframework.mail.javamail.JavaMailSender; 6 import org.springframework.scheduling.annotation.Async; 5 7 import org.springframework.stereotype.Component; 6 8 … … 9 11 10 12 private final JavaMailSender javaMailSender; 13 private final EmailMessageService emailMessageService; 11 14 12 public EmailService(JavaMailSender javaMailSender ) {15 public EmailService(JavaMailSender javaMailSender, EmailMessageService emailMessageService) { 13 16 this.javaMailSender = javaMailSender; 17 this.emailMessageService = emailMessageService; 14 18 } 15 19 … … 20 24 21 25 String text = "Verify your account on the following link\n" + 22 "http://localhost: 8080/validate?token=" + token;26 "http://localhost:9091/validate?token=" + token; 23 27 message.setText(text); 24 javaMailSender.send(message);28 sendMail(message); 25 29 } 26 30 … … 30 34 message.setSubject(subject); 31 35 32 String text = "Your post has been approved\n" + "http://localhost: 8080/post?postid=" + postId;36 String text = "Your post has been approved\n" + "http://localhost:9091/post?postid=" + postId; 33 37 message.setText(text); 34 javaMailSender.send(message);38 sendMail(message); 35 39 } 36 40 … … 43 47 "Moderator:\n" + description; 44 48 message.setText(text); 45 javaMailSender.send(message);49 sendMail(message); 46 50 } 47 51 … … 54 58 "Moderator:\n" + description; 55 59 message.setText(text); 56 javaMailSender.send(message); 60 sendMail(message); 61 } 62 63 @Async 64 public void sendMail(SimpleMailMessage message) { 65 try { 66 javaMailSender.send(message); 67 68 } catch (Exception e) { 69 emailMessageService.save(new EmailMessage(message.getTo()[0], message.getSubject(), message.getText())); 70 } 57 71 } 58 72 } -
src/main/java/it/finki/charitable/util/AutomaticEvents.java
r881a233 rb8dc761 2 2 3 3 import it.finki.charitable.entities.AppUser; 4 import it.finki.charitable.entities.DonationPost; 5 import it.finki.charitable.entities.EmailMessage; 4 6 import it.finki.charitable.security.ConfirmationToken; 5 import it.finki.charitable.services. ConfirmationTokenService;6 import it.finki.charitable.services.UserService;7 import it.finki.charitable.services.*; 8 import org.springframework.mail.SimpleMailMessage; 7 9 import org.springframework.scheduling.annotation.Scheduled; 8 10 import org.springframework.stereotype.Component; 9 11 12 import java.time.Duration; 13 import java.time.LocalDate; 10 14 import java.util.List; 15 import java.util.stream.Collectors; 11 16 12 17 @Component … … 15 20 private final UserService userService; 16 21 private final ConfirmationTokenService confirmationTokenService; 22 private final DonationPostService donationPostService; 23 private final EmailMessageService emailMessageService; 24 private final EmailService emailService; 17 25 18 public AutomaticEvents(UserService userService, ConfirmationTokenService confirmationTokenService ) {26 public AutomaticEvents(UserService userService, ConfirmationTokenService confirmationTokenService, DonationPostService donationPostService, EmailMessageService emailMessageService, EmailService emailService) { 19 27 this.userService = userService; 20 28 this.confirmationTokenService = confirmationTokenService; 29 this.donationPostService = donationPostService; 30 this.emailMessageService = emailMessageService; 31 this.emailService = emailService; 21 32 } 22 33 … … 32 43 } 33 44 } 45 46 @Scheduled(cron = "0 0 0 * * *") 47 public void setRisk() { 48 System.out.println("cron"); 49 List<DonationPost> donationPosts = donationPostService.findAll(); 50 donationPosts = donationPosts.stream().filter(post -> { 51 long daysToEnd = Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays(); 52 long totalDays = Duration.between(post.getCreatedAt().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays(); 53 System.out.println(daysToEnd + " " + totalDays); 54 55 if(totalDays < 10) 56 return true; 57 58 return (daysToEnd * 1f/totalDays) * 100 < 75; 59 }).collect(Collectors.toList()); 60 61 donationPosts.forEach(post -> { 62 float dailyAverage = post.getTotalFundsCollected() / (Duration.between(post.getCreatedAt().atTime(0, 0, 0), LocalDate.now().atTime(0, 0, 0)).toDays() + 1); 63 float neededAverage = (post.getFundsNeeded() - post.getTotalFundsCollected()) / (Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays()+1); 64 65 System.out.println(dailyAverage + " " + neededAverage); 66 int risk = (int) (dailyAverage / neededAverage * 100); 67 68 if(risk > 100) { 69 risk = 100; 70 } 71 72 if(Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays() == 0) { 73 risk = 0; 74 } 75 76 if(post.getFundsNeeded() <= post.getTotalFundsCollected()) { 77 risk = 101; 78 } 79 80 post.setRiskFactor(risk); 81 donationPostService.save(post); 82 }); 83 } 84 85 @Scheduled(cron = "0 0 * * * *") 86 public void sendMessages() { 87 List<EmailMessage> messages = emailMessageService.findAll(); 88 for(EmailMessage message: messages) { 89 SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); 90 simpleMailMessage.setTo(message.getSendTo()); 91 simpleMailMessage.setSubject(message.getSubject()); 92 simpleMailMessage.setText(message.getText()); 93 emailService.sendMail(simpleMailMessage); 94 emailMessageService.delete(message); 95 } 96 } 34 97 } -
src/main/resources/application.properties
r881a233 rb8dc761 19 19 20 20 server.port=9091 21 22 spring.security.oauth2.client.registration.google.client-id=365912873832-mqicpsq1fbbi8e01ghslpulgblkngk3l.apps.googleusercontent.com 23 spring.security.oauth2.client.registration.google.client-secret=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 24 spring.security.oauth2.client.registration.google.scope=profile,email 25 26 spring.security.oauth2.client.registration.facebook.client-id=429047632308553 27 spring.security.oauth2.client.registration.facebook.client-secret=c13280b6c116663c5bdd85c5b7a986e1 28 spring.security.oauth2.client.registration.facebook.scope=public_profile,email -
src/main/resources/templates/album.html
r881a233 rb8dc761 1 1 <!DOCTYPE html> 2 <html lang="en" xmlns :th="http://www.w3.org/1999/xhtml">2 <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"> 3 3 <head> 4 4 <meta charset="UTF-8"/> … … 7 7 <!-- Bootstrap core CSS --> 8 8 <link href="css/bootstrap.min.css" rel="stylesheet"/> 9 <link rel="stylesheet" href="css/risk.css"/> 9 10 </head> 10 11 <body> … … 26 27 <option value="dateDue">Date due</option> 27 28 <option value="fundsNeeded">Funds needed</option> 29 <option value="riskFactor">Chance to collect funds</option> 28 30 </select> 29 31 </div> … … 47 49 <div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3"> 48 50 <div class="col" th:each="post : ${postList}"> 49 <div class="card shadow-sm" >51 <div class="card shadow-sm" th:classappend="${post.riskFactor < 25 ? 'red' : (post.riskFactor < 50 ? 'orange' : (post.riskFactor < 75 ? 'yellow' : (post.riskFactor < 100 ? 'light-yellow' : (post.riskFactor == 100 ? 'green' : '') )))}"> 50 52 <img class="card-img" style="object-fit: contain" width="100%" height="225" 51 53 th:src="${post.imagesPath[0]}"> … … 53 55 <h4 th:text="${post.title}" style="height: 60px"></h4> 54 56 <p class="card-text text-truncate" style="height: 45px" th:text="${post.description}"></p> 57 <span th:if="${post.riskFactor < 101}" class="" style="color: black; margin-left: 62%;">Chance: <small th:text="${post.riskFactor}"></small></span> 58 <span th:unless="${post.riskFactor < 101}" style="color: black; margin-left: 62%;">Chance: unavailable</span> 55 59 <div class="d-flex justify-content-between align-items-center"> 56 60 <div class="btn-group"> 57 <a class="btn btn-sm btn-outline-secondary" th:href="@{/post(postid=${post.id})}">Open</a>61 <a class="btn btn-sm btn-outline-secondary" style="color: black" th:href="@{/post(postid=${post.id})}">Open</a> 58 62 </div> 59 <span class=" text-muted">Date due: <small th:text="${post.dateDue}"></small></span>63 <span class="" style="color: black">Date due: <small th:text="${post.dateDue}"></small></span> 60 64 </div> 61 65 </div> -
src/main/resources/templates/login.html
r881a233 rb8dc761 27 27 <button class="w-100 btn btn-lg btn-primary" type="submit">Sign in</button> 28 28 </form> 29 <a th:href="@{/oauth2/authorization/google}" class="btn btn-primary">Continue with google</a> 30 <a th:href="@{/oauth2/authorization/facebook}" class="btn btn-primary">Continue with facebook</a> 29 31 <p th:if="${param.error != null}" class="text-danger">Incorrect username or password</p> 30 32 <p th:if="${successValidation}" class="text-success">Account validated</p> -
src/main/resources/templates/post.html
r881a233 rb8dc761 50 50 <p th:text="${post.description}"></p> 51 51 <h5>Funds needed:</h5> 52 <p><span th:text="${ total}"></span>/<span th:text="${post.fundsNeeded}"></span> - <span th:text="${post.currency}"></span></p>52 <p><span th:text="${post.totalFundsCollected}"></span>/<span th:text="${post.fundsNeeded}"></span> - <span th:text="${post.currency}"></span></p> 53 53 <h5>Date due:</h5> 54 54 <p th:text="${post.dateDue}"></p>
Note:
See TracChangeset
for help on using the changeset viewer.