Changeset 62b653f for springapp/src/main/java/mk/profesori
- Timestamp:
- 08/30/22 15:33:18 (2 years ago)
- Branches:
- main
- Children:
- cae16b5
- Parents:
- 2fcbde4
- Location:
- springapp/src/main/java/mk/profesori/springapp
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
springapp/src/main/java/mk/profesori/springapp/Controller/PublicController.java
r2fcbde4 r62b653f 23 23 @RestController 24 24 @RequestMapping("/public") 25 @CrossOrigin(origins = { "http://192.168.0.1 9:3000", "http://192.168.0.24:3000" })25 @CrossOrigin(origins = { "http://192.168.0.17:3000", "http://192.168.0.24:3000" }) 26 26 public class PublicController { 27 27 -
springapp/src/main/java/mk/profesori/springapp/Controller/SecureController.java
r2fcbde4 r62b653f 25 25 @RestController 26 26 @RequestMapping("/secure") 27 @CrossOrigin(origins = { "http://192.168.0.1 9:3000", "http://192.168.0.24:3000" })27 @CrossOrigin(origins = { "http://192.168.0.17:3000", "http://192.168.0.24:3000" }) 28 28 public class SecureController { 29 29 … … 72 72 } 73 73 74 @RequestMapping(value = "/professor/{professorId}/upvoteOpinion/{postId}", method = RequestMethod.GET) 75 public void upvoteOpinion(@PathVariable Long professorId, 76 @PathVariable Long postId, @CurrentSecurityContext SecurityContext context) { 77 78 Authentication authentication = context.getAuthentication(); 79 80 if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails) { 81 CustomUserDetails currentUser = (CustomUserDetails) authentication.getPrincipal(); 82 mainService.upvoteOpinion(postId, currentUser); 83 } 84 } 85 86 @RequestMapping(value = "/professor/{professorId}/downvoteOpinion/{postId}", method = RequestMethod.GET) 87 public void downvoteOpinion(@PathVariable Long professorId, 88 @PathVariable Long postId, @CurrentSecurityContext SecurityContext context) { 89 90 Authentication authentication = context.getAuthentication(); 91 92 if (authentication != null && authentication.getPrincipal() instanceof CustomUserDetails) { 93 CustomUserDetails currentUser = (CustomUserDetails) authentication.getPrincipal(); 94 mainService.downvoteOpinion(postId, currentUser); 95 } 96 } 97 74 98 } -
springapp/src/main/java/mk/profesori/springapp/Model/CustomUserDetails.java
r2fcbde4 r62b653f 12 12 import javax.persistence.EnumType; 13 13 import javax.persistence.Enumerated; 14 import javax.persistence.FetchType; 14 15 import javax.persistence.GeneratedValue; 15 16 import javax.persistence.GenerationType; 16 17 import javax.persistence.Id; 18 import javax.persistence.JoinTable; 19 import javax.persistence.JoinColumn; 20 import javax.persistence.ManyToMany; 17 21 import javax.persistence.OneToMany; 18 22 import javax.persistence.SequenceGenerator; … … 50 54 private Boolean locked = false; 51 55 private Boolean enabled = false; 52 @OneToMany(mappedBy = "customUserDetails", cascade = CascadeType.ALL )56 @OneToMany(mappedBy = "customUserDetails", cascade = CascadeType.ALL, fetch = FetchType.EAGER) 53 57 private Set<ConfirmationToken> confirmationTokens = new HashSet<>(); 54 @OneToMany(mappedBy = "author", cascade = CascadeType.ALL )58 @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.EAGER) 55 59 private Set<Post> authoredPosts = new HashSet<>(); 56 private Integer karma; 60 private Integer karma = 0; 61 @ManyToMany(fetch = FetchType.EAGER) 62 @JoinTable(name = "post_like", joinColumns = @JoinColumn(name = "custom_user_details_id"), inverseJoinColumns = @JoinColumn(name = "post_id")) 63 Set<Post> likedPosts; 64 @ManyToMany(fetch = FetchType.EAGER) 65 @JoinTable(name = "post_dislike", joinColumns = @JoinColumn(name = "custom_user_details_id"), inverseJoinColumns = @JoinColumn(name = "post_id")) 66 Set<Post> dislikedPosts; 57 67 58 68 public CustomUserDetails(String fullName, String username, String email, String password, UserRole userRole) { … … 105 115 106 116 public Integer getKarma() { 107 Integer karma = 0; 108 for (Post post : this.authoredPosts) { 109 karma += post.getUpvoteCount() - post.getDownvoteCount(); 110 } 111 return karma; 117 return this.karma; 112 118 } 113 119 120 public void setKarma(Integer karma) { 121 this.karma = karma; 122 } 123 124 public Set<Post> getLikedPosts() { 125 return this.likedPosts; 126 } 127 128 public void setLikedPosts(Set<Post> likedPosts) { 129 this.likedPosts = likedPosts; 130 } 131 132 public Set<Post> getDislikedPosts() { 133 return this.dislikedPosts; 134 } 135 136 public void setDislikedPosts(Set<Post> dislikedPosts) { 137 this.likedPosts = dislikedPosts; 138 } 114 139 } -
springapp/src/main/java/mk/profesori/springapp/Model/Opinion.java
r2fcbde4 r62b653f 23 23 public Opinion(String title, String content, CustomUserDetails author, LocalDateTime timePosted, 24 24 LocalDateTime timeLastEdited, 25 Integer upvoteCount, Integer downvoteCount,Post parent, List<Post> children, Professor targetProfessor) {26 super(title, content, author, timePosted, timeLastEdited, upvoteCount, downvoteCount,parent, children);25 Post parent, List<Post> children, Professor targetProfessor) { 26 super(title, content, author, timePosted, timeLastEdited, parent, children); 27 27 this.targetProfessor = targetProfessor; 28 28 } … … 31 31 public Opinion(String title, String content, CustomUserDetails author, LocalDateTime timePosted, 32 32 LocalDateTime timeLastEdited, 33 Integer upvoteCount, Integer downvoteCount,List<Post> children, Professor targetProfessor) {34 super(title, content, author, timePosted, timeLastEdited, upvoteCount, downvoteCount,children);33 List<Post> children, Professor targetProfessor) { 34 super(title, content, author, timePosted, timeLastEdited, children); 35 35 this.targetProfessor = targetProfessor; 36 36 } -
springapp/src/main/java/mk/profesori/springapp/Model/Post.java
r2fcbde4 r62b653f 4 4 import java.util.ArrayList; 5 5 import java.util.List; 6 import java.util.Set; 6 7 7 8 import javax.persistence.CascadeType; … … 16 17 import javax.persistence.InheritanceType; 17 18 import javax.persistence.JoinColumn; 19 import javax.persistence.ManyToMany; 18 20 import javax.persistence.ManyToOne; 19 21 import javax.persistence.OneToMany; … … 52 54 private LocalDateTime timeLastEdited; 53 55 54 @Column(name = "upvote_count")55 private Integer upvoteCount;56 57 @Column(name = "downvote_count")58 private Integer downvoteCount;59 60 56 @ManyToOne 61 57 @JoinColumn(name = "parent_post_id", nullable = true) … … 64 60 @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) 65 61 private List<Post> children = new ArrayList<>(); 62 63 @ManyToMany(mappedBy = "likedPosts") 64 Set<CustomUserDetails> likes; 65 66 @ManyToMany(mappedBy = "dislikedPosts") 67 Set<CustomUserDetails> dislikes; 66 68 67 69 // getters and setters … … 114 116 } 115 117 116 public Integer getUpvoteCount() {117 return upvoteCount;118 }119 120 public void setUpvoteCount(Integer upvoteCount) {121 this.upvoteCount = upvoteCount;122 }123 124 public Integer getDownvoteCount() {125 return downvoteCount;126 }127 128 public void setDownvoteCount(Integer downvoteCount) {129 this.downvoteCount = downvoteCount;130 }131 132 118 public Post getParent() { 133 119 return parent; … … 146 132 } 147 133 134 public Set<CustomUserDetails> getLikes() { 135 return likes; 136 } 137 138 public void setLikes(Set<CustomUserDetails> likes) { 139 this.likes = likes; 140 } 141 142 public Set<CustomUserDetails> getDislikes() { 143 return dislikes; 144 } 145 146 public void setDislikes(Set<CustomUserDetails> dislikes) { 147 this.dislikes = dislikes; 148 } 149 148 150 // konstruktor so parent (koga e reply) 149 151 public Post(String title, String content, CustomUserDetails author, LocalDateTime timePosted, 150 152 LocalDateTime timeLastEdited, 151 Integer upvoteCount, Integer downvoteCount,Post parent, List<Post> children) {153 Post parent, List<Post> children) { 152 154 this.title = title; 153 155 this.content = content; … … 155 157 this.timePosted = LocalDateTime.now(); 156 158 this.timeLastEdited = LocalDateTime.now(); 157 this.upvoteCount = 0;158 this.downvoteCount = 0;159 159 this.parent = parent; 160 160 this.children = new ArrayList<>(); … … 164 164 public Post(String title, String content, CustomUserDetails author, LocalDateTime timePosted, 165 165 LocalDateTime timeLastEdited, 166 Integer upvoteCount, Integer downvoteCount,List<Post> children) {166 List<Post> children) { 167 167 this.title = title; 168 168 this.content = content; … … 170 170 this.timePosted = LocalDateTime.now(); 171 171 this.timeLastEdited = LocalDateTime.now(); 172 this.upvoteCount = 0;173 this.downvoteCount = 0;174 172 this.parent = null; 175 173 this.children = new ArrayList<>(); -
springapp/src/main/java/mk/profesori/springapp/Security/SecurityConfiguration.java
r2fcbde4 r62b653f 37 37 @Override 38 38 public void addCorsMappings(CorsRegistry registry) { 39 registry.addMapping("/**").allowedOrigins("http://192.168.0.1 9:3000", "http://192.168.0.24:3000")39 registry.addMapping("/**").allowedOrigins("http://192.168.0.17:3000", "http://192.168.0.24:3000") 40 40 .allowCredentials(true); 41 41 } -
springapp/src/main/java/mk/profesori/springapp/Service/ConfirmationTokenService.java
r2fcbde4 r62b653f 13 13 @AllArgsConstructor 14 14 public class ConfirmationTokenService { 15 15 16 16 private final ConfirmationTokenRepository confirmationTokenRepository; 17 17 -
springapp/src/main/java/mk/profesori/springapp/Service/CustomUserDetailsService.java
r2fcbde4 r62b653f 22 22 23 23 private final UserRepository userRepository; 24 24 25 @Autowired 25 public PasswordEncoder passwordEncoder() 26 { 26 public PasswordEncoder passwordEncoder() { 27 27 return new BCryptPasswordEncoder(); 28 28 } 29 29 30 private final static String USER_NOT_FOUND_MSG = "User with email %s not found"; 30 31 private final ConfirmationTokenService confirmationTokenService; … … 33 34 public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { 34 35 return userRepository.findByEmail(email).orElseThrow( 35 () -> new UsernameNotFoundException(String.format(USER_NOT_FOUND_MSG,email)) 36 ); 36 () -> new UsernameNotFoundException(String.format(USER_NOT_FOUND_MSG, email))); 37 37 } 38 38 … … 51 51 String token = UUID.randomUUID().toString(); 52 52 ConfirmationToken confirmationToken = new ConfirmationToken( 53 token, 54 LocalDateTime.now(), 55 LocalDateTime.now().plusMinutes(10), 56 customUserDetails 57 ); 58 53 token, 54 LocalDateTime.now(), 55 LocalDateTime.now().plusMinutes(10), 56 customUserDetails); 57 59 58 confirmationTokenService.saveConfirmationToken(confirmationToken); 60 59 return token; -
springapp/src/main/java/mk/profesori/springapp/Service/MainService.java
r2fcbde4 r62b653f 13 13 import mk.profesori.springapp.Repository.StudyProgrammeRepository; 14 14 import mk.profesori.springapp.Repository.UniversityRepository; 15 import mk.profesori.springapp.Repository.UserRepository; 15 16 import mk.profesori.springapp.Model.City; 16 17 import mk.profesori.springapp.Model.CustomUserDetails; … … 36 37 @Autowired 37 38 private OpinionRepository opinionRepository; 39 @Autowired 40 private UserRepository userRepository; 38 41 39 42 public List<Professor> getAllProfessors() { … … 138 141 139 142 Opinion opinionToAdd = new Opinion(title, content, currentUser, null, null, 140 null, null, null,targetProfessor);143 null, targetProfessor); 141 144 142 145 opinionRepository.save(opinionToAdd); … … 149 152 150 153 Opinion opinionToAdd = new Opinion(null, content, currentUser, null, null, 151 null, null,targetOpinion, null, targetProfessor);154 targetOpinion, null, targetProfessor); 152 155 opinionRepository.save(opinionToAdd); 153 156 … … 155 158 opinionRepository.save(targetOpinion); 156 159 } 160 161 public void upvoteOpinion(Long postId, CustomUserDetails currentUser) { 162 Opinion targetOpinion = opinionRepository.findByPostId(postId); 163 164 if (!targetOpinion.getLikes().contains(currentUser)) { 165 166 targetOpinion.getLikes().add(currentUser); 167 // opinionRepository.save(targetOpinion); 168 169 targetOpinion.getAuthor().setKarma(targetOpinion.getAuthor().getKarma() + 1); 170 userRepository.save(targetOpinion.getAuthor()); 171 172 currentUser.getLikedPosts().add(targetOpinion); 173 userRepository.save(currentUser); 174 } 175 } 176 177 public void downvoteOpinion(Long postId, CustomUserDetails currentUser) { 178 Opinion targetOpinion = opinionRepository.findByPostId(postId); 179 180 if (!targetOpinion.getDislikes().contains(currentUser)) { 181 182 targetOpinion.getDislikes().add(currentUser); 183 // opinionRepository.save(targetOpinion); 184 185 targetOpinion.getAuthor().setKarma(targetOpinion.getAuthor().getKarma() - 1); 186 userRepository.save(targetOpinion.getAuthor()); 187 188 currentUser.getDislikedPosts().add(targetOpinion); 189 userRepository.save(currentUser); 190 } 191 } 157 192 } -
springapp/src/main/java/mk/profesori/springapp/Service/RegistrationService.java
r2fcbde4 r62b653f 45 45 String tokenToResend = customUserDetailsService 46 46 .createToken(userRepository.findByEmail(request.getEmail()).get()); 47 String link = "http://192.168.0.1 9:8080/registration/confirm?token=" + tokenToResend;47 String link = "http://192.168.0.17:8080/registration/confirm?token=" + tokenToResend; 48 48 emailSender.send(request.getEmail(), emailSender.buildEmail(request.getUsername(), link)); 49 49 return tokenToResend; … … 66 66 UserRole.REGULAR)); 67 67 68 String link = "http://192.168.0.1 9:8080/registration/confirm?token=" + token;68 String link = "http://192.168.0.17:8080/registration/confirm?token=" + token; 69 69 70 70 emailSender.send(request.getEmail(), emailSender.buildEmail(request.getUsername(), link));
Note:
See TracChangeset
for help on using the changeset viewer.