Changeset 62b653f for springapp


Ignore:
Timestamp:
08/30/22 15:33:18 (2 years ago)
Author:
unknown <mlviktor23@…>
Branches:
main
Children:
cae16b5
Parents:
2fcbde4
Message:

implemented upvote/downvote func. in react

Location:
springapp/src/main
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • springapp/src/main/java/mk/profesori/springapp/Controller/PublicController.java

    r2fcbde4 r62b653f  
    2323@RestController
    2424@RequestMapping("/public")
    25 @CrossOrigin(origins = { "http://192.168.0.19:3000", "http://192.168.0.24:3000" })
     25@CrossOrigin(origins = { "http://192.168.0.17:3000", "http://192.168.0.24:3000" })
    2626public class PublicController {
    2727
  • springapp/src/main/java/mk/profesori/springapp/Controller/SecureController.java

    r2fcbde4 r62b653f  
    2525@RestController
    2626@RequestMapping("/secure")
    27 @CrossOrigin(origins = { "http://192.168.0.19:3000", "http://192.168.0.24:3000" })
     27@CrossOrigin(origins = { "http://192.168.0.17:3000", "http://192.168.0.24:3000" })
    2828public class SecureController {
    2929
     
    7272    }
    7373
     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
    7498}
  • springapp/src/main/java/mk/profesori/springapp/Model/CustomUserDetails.java

    r2fcbde4 r62b653f  
    1212import javax.persistence.EnumType;
    1313import javax.persistence.Enumerated;
     14import javax.persistence.FetchType;
    1415import javax.persistence.GeneratedValue;
    1516import javax.persistence.GenerationType;
    1617import javax.persistence.Id;
     18import javax.persistence.JoinTable;
     19import javax.persistence.JoinColumn;
     20import javax.persistence.ManyToMany;
    1721import javax.persistence.OneToMany;
    1822import javax.persistence.SequenceGenerator;
     
    5054    private Boolean locked = false;
    5155    private Boolean enabled = false;
    52     @OneToMany(mappedBy = "customUserDetails", cascade = CascadeType.ALL)
     56    @OneToMany(mappedBy = "customUserDetails", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    5357    private Set<ConfirmationToken> confirmationTokens = new HashSet<>();
    54     @OneToMany(mappedBy = "author", cascade = CascadeType.ALL)
     58    @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    5559    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;
    5767
    5868    public CustomUserDetails(String fullName, String username, String email, String password, UserRole userRole) {
     
    105115
    106116    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;
    112118    }
    113119
     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    }
    114139}
  • springapp/src/main/java/mk/profesori/springapp/Model/Opinion.java

    r2fcbde4 r62b653f  
    2323    public Opinion(String title, String content, CustomUserDetails author, LocalDateTime timePosted,
    2424            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);
    2727        this.targetProfessor = targetProfessor;
    2828    }
     
    3131    public Opinion(String title, String content, CustomUserDetails author, LocalDateTime timePosted,
    3232            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);
    3535        this.targetProfessor = targetProfessor;
    3636    }
  • springapp/src/main/java/mk/profesori/springapp/Model/Post.java

    r2fcbde4 r62b653f  
    44import java.util.ArrayList;
    55import java.util.List;
     6import java.util.Set;
    67
    78import javax.persistence.CascadeType;
     
    1617import javax.persistence.InheritanceType;
    1718import javax.persistence.JoinColumn;
     19import javax.persistence.ManyToMany;
    1820import javax.persistence.ManyToOne;
    1921import javax.persistence.OneToMany;
     
    5254    private LocalDateTime timeLastEdited;
    5355
    54     @Column(name = "upvote_count")
    55     private Integer upvoteCount;
    56 
    57     @Column(name = "downvote_count")
    58     private Integer downvoteCount;
    59 
    6056    @ManyToOne
    6157    @JoinColumn(name = "parent_post_id", nullable = true)
     
    6460    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    6561    private List<Post> children = new ArrayList<>();
     62
     63    @ManyToMany(mappedBy = "likedPosts")
     64    Set<CustomUserDetails> likes;
     65
     66    @ManyToMany(mappedBy = "dislikedPosts")
     67    Set<CustomUserDetails> dislikes;
    6668
    6769    // getters and setters
     
    114116    }
    115117
    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 
    132118    public Post getParent() {
    133119        return parent;
     
    146132    }
    147133
     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
    148150    // konstruktor so parent (koga e reply)
    149151    public Post(String title, String content, CustomUserDetails author, LocalDateTime timePosted,
    150152            LocalDateTime timeLastEdited,
    151             Integer upvoteCount, Integer downvoteCount, Post parent, List<Post> children) {
     153            Post parent, List<Post> children) {
    152154        this.title = title;
    153155        this.content = content;
     
    155157        this.timePosted = LocalDateTime.now();
    156158        this.timeLastEdited = LocalDateTime.now();
    157         this.upvoteCount = 0;
    158         this.downvoteCount = 0;
    159159        this.parent = parent;
    160160        this.children = new ArrayList<>();
     
    164164    public Post(String title, String content, CustomUserDetails author, LocalDateTime timePosted,
    165165            LocalDateTime timeLastEdited,
    166             Integer upvoteCount, Integer downvoteCount, List<Post> children) {
     166            List<Post> children) {
    167167        this.title = title;
    168168        this.content = content;
     
    170170        this.timePosted = LocalDateTime.now();
    171171        this.timeLastEdited = LocalDateTime.now();
    172         this.upvoteCount = 0;
    173         this.downvoteCount = 0;
    174172        this.parent = null;
    175173        this.children = new ArrayList<>();
  • springapp/src/main/java/mk/profesori/springapp/Security/SecurityConfiguration.java

    r2fcbde4 r62b653f  
    3737            @Override
    3838            public void addCorsMappings(CorsRegistry registry) {
    39                 registry.addMapping("/**").allowedOrigins("http://192.168.0.19:3000", "http://192.168.0.24:3000")
     39                registry.addMapping("/**").allowedOrigins("http://192.168.0.17:3000", "http://192.168.0.24:3000")
    4040                        .allowCredentials(true);
    4141            }
  • springapp/src/main/java/mk/profesori/springapp/Service/ConfirmationTokenService.java

    r2fcbde4 r62b653f  
    1313@AllArgsConstructor
    1414public class ConfirmationTokenService {
    15    
     15
    1616    private final ConfirmationTokenRepository confirmationTokenRepository;
    1717
  • springapp/src/main/java/mk/profesori/springapp/Service/CustomUserDetailsService.java

    r2fcbde4 r62b653f  
    2222
    2323    private final UserRepository userRepository;
     24
    2425    @Autowired
    25     public PasswordEncoder passwordEncoder()
    26     {
     26    public PasswordEncoder passwordEncoder() {
    2727        return new BCryptPasswordEncoder();
    2828    }
     29
    2930    private final static String USER_NOT_FOUND_MSG = "User with email %s not found";
    3031    private final ConfirmationTokenService confirmationTokenService;
     
    3334    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
    3435        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)));
    3737    }
    3838
     
    5151        String token = UUID.randomUUID().toString();
    5252        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
    5958        confirmationTokenService.saveConfirmationToken(confirmationToken);
    6059        return token;
  • springapp/src/main/java/mk/profesori/springapp/Service/MainService.java

    r2fcbde4 r62b653f  
    1313import mk.profesori.springapp.Repository.StudyProgrammeRepository;
    1414import mk.profesori.springapp.Repository.UniversityRepository;
     15import mk.profesori.springapp.Repository.UserRepository;
    1516import mk.profesori.springapp.Model.City;
    1617import mk.profesori.springapp.Model.CustomUserDetails;
     
    3637    @Autowired
    3738    private OpinionRepository opinionRepository;
     39    @Autowired
     40    private UserRepository userRepository;
    3841
    3942    public List<Professor> getAllProfessors() {
     
    138141
    139142        Opinion opinionToAdd = new Opinion(title, content, currentUser, null, null,
    140                 null, null, null, targetProfessor);
     143                null, targetProfessor);
    141144
    142145        opinionRepository.save(opinionToAdd);
     
    149152
    150153        Opinion opinionToAdd = new Opinion(null, content, currentUser, null, null,
    151                 null, null, targetOpinion, null, targetProfessor);
     154                targetOpinion, null, targetProfessor);
    152155        opinionRepository.save(opinionToAdd);
    153156
     
    155158        opinionRepository.save(targetOpinion);
    156159    }
     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    }
    157192}
  • springapp/src/main/java/mk/profesori/springapp/Service/RegistrationService.java

    r2fcbde4 r62b653f  
    4545                String tokenToResend = customUserDetailsService
    4646                        .createToken(userRepository.findByEmail(request.getEmail()).get());
    47                 String link = "http://192.168.0.19:8080/registration/confirm?token=" + tokenToResend;
     47                String link = "http://192.168.0.17:8080/registration/confirm?token=" + tokenToResend;
    4848                emailSender.send(request.getEmail(), emailSender.buildEmail(request.getUsername(), link));
    4949                return tokenToResend;
     
    6666                        UserRole.REGULAR));
    6767
    68         String link = "http://192.168.0.19:8080/registration/confirm?token=" + token;
     68        String link = "http://192.168.0.17:8080/registration/confirm?token=" + token;
    6969
    7070        emailSender.send(request.getEmail(), emailSender.buildEmail(request.getUsername(), link));
  • springapp/src/main/resources/application.properties

    r2fcbde4 r62b653f  
    77spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
    88spring.jpa.properties.hibernate.format_sql=true
    9 server.address=192.168.0.19
     9server.address=192.168.0.17
    1010spring.mail.host=192.168.0.24
    1111spring.mail.username=mailuser
Note: See TracChangeset for help on using the changeset viewer.