Changeset 6f91f99 in Git for src


Ignore:
Timestamp:
02/09/22 15:46:58 (3 years ago)
Author:
Test <matonikolov77@…>
Branches:
main
Children:
8f1b7e9
Parents:
967b414
Message:

Refactored reply repository and discussion repository

Location:
src/main
Files:
14 edited
2 moved

Legend:

Unmodified
Added
Removed
  • src/main/java/com/wediscussmovies/project/model/Discussion.java

    r967b414 r6f91f99  
    11package com.wediscussmovies.project.model;
    22
     3import com.wediscussmovies.project.model.relation.DiscussionLikes;
    34import lombok.Data;
    45
    56import javax.persistence.*;
    67import java.sql.Date;
     8import java.util.List;
    79import java.util.Objects;
    810
     
    2931
    3032
     33
     34
     35
    3136    @ManyToOne
    3237    @JoinColumn(name = "movie_id")
     
    4146    private Person person;
    4247
     48    @Transient
     49    private Long likes;
     50
    4351    public Discussion(Character type, String text, String title, Date date, User user) {
    4452        this.type = type;
     
    5260    }
    5361
    54     public String getText() {
    55         return text;
    56     }
    57 
    58     public void setText(String text) {
    59         this.text = text;
    60     }
    61 
    62     public String getTitle() {
    63         return title;
    64     }
    65 
    66     public void setTitle(String title) {
    67         this.title = title;
    68     }
    69 
    70     public Date getDate() {
    71         return date;
    72     }
    73 
    74     public void setDate(Date date) {
    75         this.date = date;
    76     }
    7762
    7863    @Override
  • src/main/java/com/wediscussmovies/project/querymodels/DiscussionLikesQM.java

    r967b414 r6f91f99  
    66
    77@Data
    8 public class DiscussionLikes {
     8public class DiscussionLikesQM {
    99    private Integer discussionId;
    1010    private Long likes;
    1111
    12     public DiscussionLikes(Integer discussionId, Long likes) {
     12    public DiscussionLikesQM(Integer discussionId, Long likes) {
    1313        this.discussionId = discussionId;
    1414        this.likes = likes;
    1515    }
    1616
    17     public DiscussionLikes() {
     17    public DiscussionLikesQM() {
    1818    }
    1919
     
    2222        if (this == o) return true;
    2323        if (o == null || getClass() != o.getClass()) return false;
    24         DiscussionLikes that = (DiscussionLikes) o;
     24        DiscussionLikesQM that = (DiscussionLikesQM) o;
    2525        return Objects.equals(discussionId, that.discussionId) && Objects.equals(likes, that.likes);
    2626    }
  • src/main/java/com/wediscussmovies/project/querymodels/GenreLikesQM.java

    r967b414 r6f91f99  
    33import lombok.Data;
    44
    5 import javax.persistence.Entity;
    6 import javax.persistence.Table;
    7 import java.util.Comparator;
    85import java.util.Objects;
    96
    107@Data
    11 public class GenreLikes {
     8public class GenreLikesQM {
    129    private Integer genreId;
    1310    private String name;
    1411    private Long likes;
    1512
    16     public static Comparator<GenreLikes> sorter = Comparator.comparing(GenreLikes::getLikes).thenComparing(GenreLikes::getName).reversed();
     13    //public static Comparator<GenreLikes> sorter = Comparator.comparing(GenreLikes::getLikes).thenComparing(GenreLikes::getName).reversed();
    1714
    18     public GenreLikes(Integer genreId, String name, Long likes) {
     15    public GenreLikesQM(Integer genreId, String name, Long likes) {
    1916        this.genreId = genreId;
    2017        this.name = name;
     
    2724        if (this == o) return true;
    2825        if (o == null || getClass() != o.getClass()) return false;
    29         GenreLikes that = (GenreLikes) o;
     26        GenreLikesQM that = (GenreLikesQM) o;
    3027        return Objects.equals(genreId, that.genreId);
    3128    }
  • src/main/java/com/wediscussmovies/project/repository/DiscussionLikesRepository.java

    r967b414 r6f91f99  
    77import com.wediscussmovies.project.model.relation.MovieLikes;
    88import com.wediscussmovies.project.model.relation.UserGenres;
     9import org.springframework.data.domain.Sort;
    910import org.springframework.data.jpa.repository.JpaRepository;
    1011import org.springframework.stereotype.Repository;
     
    1516public interface DiscussionLikesRepository extends JpaRepository<DiscussionLikes, DiscussionLikesPK> {
    1617    List<DiscussionLikes> findAllByUser(User user);
     18
    1719}
  • src/main/java/com/wediscussmovies/project/repository/DiscussionRepository.java

    r967b414 r6f91f99  
    44import com.wediscussmovies.project.model.Movie;
    55import com.wediscussmovies.project.model.Person;
    6 import com.wediscussmovies.project.querymodels.DiscussionLikes;
    7 import com.wediscussmovies.project.querymodels.GenreLikes;
     6import com.wediscussmovies.project.querymodels.DiscussionLikesQM;
    87import org.springframework.data.jpa.repository.JpaRepository;
    98import org.springframework.data.jpa.repository.Query;
     9import org.springframework.data.repository.query.Param;
    1010import org.springframework.stereotype.Repository;
    1111
     
    1919     List<Discussion> findAllByPerson (Person person);
    2020
    21      @Query(value = "select new com.wediscussmovies.project.querymodels.DiscussionLikes(d.discussionId, count(dl.id.userId)) from Discussion d" +
     21     @Query(value = "select new com.wediscussmovies.project.querymodels.DiscussionLikesQM(d.discussionId, count(dl.id.userId)) from Discussion d" +
    2222             " left join DiscussionLikes dl on dl.id.discussionId = d.discussionId" +
    23              " group by d.discussionId" +
     23             " where :discussionId = d.discussionId" +
     24             " group by d.discussionId"+
    2425             " order by count(dl.id.userId) desc")
    2526     @Transactional
    26      List<DiscussionLikes> findAllWithLikes();
     27     DiscussionLikesQM findDiscussionWithLikes(@Param("discussionId") Integer discussionId);
     28
     29     @Query(value = "select new com.wediscussmovies.project.querymodels.DiscussionLikesQM(d.discussionId, count(dl.id.userId)) from Discussion d" +
     30             " left join DiscussionLikes dl on dl.id.discussionId = d.discussionId" +
     31             " group by d.discussionId"+
     32             " order by count(dl.id.userId) desc")
     33     @Transactional
     34     List<DiscussionLikesQM> findAllDiscussionsWithLikes();
    2735}
  • src/main/java/com/wediscussmovies/project/repository/GenreLikesRepository.java

    r967b414 r6f91f99  
    22
    33import com.wediscussmovies.project.model.User;
    4 import com.wediscussmovies.project.model.primarykeys.GenreLikesPK;
    5 import com.wediscussmovies.project.model.primarykeys.MovieLikesPK;
    64import com.wediscussmovies.project.model.primarykeys.UserGenresPK;
    7 import com.wediscussmovies.project.model.relation.MovieLikes;
    85import com.wediscussmovies.project.model.relation.UserGenres;
    9 import com.wediscussmovies.project.querymodels.GenreLikes;
    106import org.springframework.data.jpa.repository.JpaRepository;
    117import org.springframework.stereotype.Repository;
  • src/main/java/com/wediscussmovies/project/repository/GenreRepository.java

    r967b414 r6f91f99  
    22
    33import com.wediscussmovies.project.model.Genre;
    4 import com.wediscussmovies.project.querymodels.GenreLikes;
     4import com.wediscussmovies.project.querymodels.GenreLikesQM;
    55import org.springframework.data.jpa.repository.JpaRepository;
    66import org.springframework.data.jpa.repository.Modifying;
     
    1010
    1111import javax.transaction.Transactional;
    12 import java.sql.Date;
    1312import java.util.List;
    1413
     
    1716     List<Genre> findAllByGenreType(String genre);
    1817
    19      @Query(value = "select new com.wediscussmovies.project.querymodels.GenreLikes(g.genreId, g.genreType, count(ug.id.userId)) from Genre g" +
     18     @Query(value = "select new com.wediscussmovies.project.querymodels.GenreLikesQM(g.genreId, g.genreType, count(ug.id.userId)) from Genre g" +
    2019             " left join UserGenres ug on ug.id.genreId = g.genreId" +
    2120             " group by g.genreId, g.genreType" +
    22              " order by count(ug.id.userId) desc")
     21             " order by count(ug.id.userId) desc, g.genreType asc")
    2322     @Transactional
    24      List<GenreLikes> findAllWithLikes();
     23     List<GenreLikesQM> findAllWithLikes();
    2524
    2625     @Modifying
  • src/main/java/com/wediscussmovies/project/repository/MovieRepository.java

    r967b414 r6f91f99  
    33import com.wediscussmovies.project.model.Movie;
    44import com.wediscussmovies.project.model.Person;
    5 import com.wediscussmovies.project.querymodels.GenreLikes;
    65import com.wediscussmovies.project.querymodels.MovieLikesQM;
    76import org.springframework.data.jpa.repository.JpaRepository;
  • src/main/java/com/wediscussmovies/project/service/DiscussionService.java

    r967b414 r6f91f99  
    33import com.wediscussmovies.project.model.Discussion;
    44import com.wediscussmovies.project.model.User;
    5 import com.wediscussmovies.project.querymodels.DiscussionLikes;
     5import com.wediscussmovies.project.querymodels.DiscussionLikesQM;
    66
    77import java.util.List;
     
    2020    List<Discussion> findLikedDiscussionsByUser(User user);
    2121
    22     DiscussionLikes findLikesForDiscussionWithId(int discussionId);
     22    DiscussionLikesQM findLikesForDiscussionWithId(Integer discussionId);
     23
     24    void findLikesForAllDiscussions(List<Discussion> discussions);
    2325
    2426}
  • src/main/java/com/wediscussmovies/project/service/GenreService.java

    r967b414 r6f91f99  
    22
    33import com.wediscussmovies.project.model.Genre;
    4 import com.wediscussmovies.project.querymodels.GenreLikes;
     4import com.wediscussmovies.project.querymodels.GenreLikesQM;
    55import org.springframework.stereotype.Service;
    66
     
    1313    List<Genre> findAllByType(String genre);
    1414    Genre save(String genreName);
    15     List<GenreLikes> findAllWithLikes();
     15    List<GenreLikesQM> findAllWithLikes();
    1616    void likeGenre(Integer genreId,Integer userId);
    1717    void unlikeGenre(Integer genreId,Integer userId);
  • src/main/java/com/wediscussmovies/project/service/impl/DiscussionServiceImpl.java

    r967b414 r6f91f99  
    77import com.wediscussmovies.project.model.primarykeys.DiscussionLikesPK;
    88import com.wediscussmovies.project.model.relation.DiscussionLikes;
     9import com.wediscussmovies.project.querymodels.DiscussionLikesQM;
    910import com.wediscussmovies.project.repository.*;
    1011import com.wediscussmovies.project.model.Discussion;
     
    115116
    116117    @Override
    117     public com.wediscussmovies.project.querymodels.DiscussionLikes findLikesForDiscussionWithId(int discussionId) {
    118         return discussionRepository.findAllWithLikes().stream().filter(d ->  d.getDiscussionId().equals(discussionId)).findFirst().get();
     118    public DiscussionLikesQM findLikesForDiscussionWithId(Integer discussionId) {
     119//        return discussionRepository.findAllWithLikes().stream().filter(d ->  d.getDiscussionId().equals(discussionId)).findFirst().get();
     120                return this.discussionRepository.findDiscussionWithLikes(discussionId);
     121
     122    }
     123
     124    @Override
     125    public void findLikesForAllDiscussions(List<Discussion> discussions) {
     126//        this.discussionRepository.findAllDiscussionsWithLikes()
     127//                .forEach(el ->{
     128//                        if (discussions.)
     129//                });
    119130    }
    120131
  • src/main/java/com/wediscussmovies/project/service/impl/GenreServiceImpl.java

    r967b414 r6f91f99  
    44import com.wediscussmovies.project.model.exception.GenreNotExistException;
    55import com.wediscussmovies.project.model.exception.UserNotExistException;
    6 import com.wediscussmovies.project.model.primarykeys.GenreLikesPK;
    76import com.wediscussmovies.project.model.primarykeys.UserGenresPK;
    8 import com.wediscussmovies.project.model.relation.MovieLikes;
    97import com.wediscussmovies.project.model.relation.UserGenres;
    10 import com.wediscussmovies.project.querymodels.GenreLikes;
     8import com.wediscussmovies.project.querymodels.GenreLikesQM;
    119import com.wediscussmovies.project.repository.GenreLikesRepository;
    1210import com.wediscussmovies.project.repository.GenreRepository;
     
    4846
    4947    @Override
    50     public List<GenreLikes> findAllWithLikes() {
    51         List<GenreLikes> genreLikesList = this.genreRepository.findAllWithLikes();
    52         genreLikesList.sort(GenreLikes.sorter);
    53         return genreLikesList;
     48    public List<GenreLikesQM> findAllWithLikes() {
     49        // List<GenreLikes> genreLikesList = this.genreRepository.findAllWithLikes();
     50       // genreLikesList.sort(GenreLikes.sorter);
     51        return this.genreRepository.findAllWithLikes();
    5452    }
    5553
  • src/main/java/com/wediscussmovies/project/web/controller/DiscussionController.java

    r967b414 r6f91f99  
    66import com.wediscussmovies.project.model.User;
    77import com.wediscussmovies.project.model.relation.DiscussionLikes;
     8import com.wediscussmovies.project.querymodels.DiscussionLikesQM;
    89import com.wediscussmovies.project.service.DiscussionService;
    910import com.wediscussmovies.project.service.MovieService;
     
    6263        try {
    6364            Discussion disc = discussionService.findById(id);
     65            disc.setLikes(discussionService.findLikesForDiscussionWithId(disc.getDiscussionId()).getLikes());
    6466            model.addAttribute("disc", disc);
    6567            model.addAttribute("contentTemplate", "discussion");
    66             model.addAttribute("user",LoggedUser.getLoggedUser());
    6768            model.addAttribute("replies",this.replyService.findAllByDiscussion(disc));
    68             addModelPropertiesLikes(model, disc, null);
    6969            addModelPropertiesForUser(model);
    7070
     
    183183    }
    184184    private void addModelPropertiesLikes(Model model, Discussion discussion, List<Discussion> discussions){
     185        // ispagja kompliciran kod vaka, podobro da dodademe i svojstvo u diskusijata i koga ke se dodade lajk
     186        // soodvetno da se zgoleme i obratno
    185187        if(discussion==null){
    186             List<com.wediscussmovies.project.querymodels.DiscussionLikes> discussionLikes = new ArrayList<>();
     188            List<DiscussionLikesQM> discussionLikes = new ArrayList<>();
    187189            for(Discussion d: discussions){
    188190                discussionLikes.add(discussionService.findLikesForDiscussionWithId(d.getDiscussionId()));
  • src/main/java/com/wediscussmovies/project/web/controller/GenreController.java

    r967b414 r6f91f99  
    55import com.wediscussmovies.project.model.User;
    66import com.wediscussmovies.project.model.relation.UserGenres;
    7 import com.wediscussmovies.project.querymodels.GenreLikes;
    87import com.wediscussmovies.project.repository.GenreLikesRepository;
    98import com.wediscussmovies.project.service.GenreService;
  • src/main/resources/static/js/sharedScript.js

    r967b414 r6f91f99  
    9191
    9292    $(".user-movies-list").on("click", function (){
    93         children = $(this).children()
    94         first = true
     93       let children = $(this).children()
     94        let first = true
    9595        for (let item of children){
    9696            if(first){
  • src/main/resources/templates/discussionsList.html

    r967b414 r6f91f99  
    3131                                <a th:text="${disc.getTitle()}" th:href="@{'/discussions/{id}' (id=${disc.getDiscussionId()})}"></a>
    3232                            </td>
    33                             <td th:each="like : ${likes}" th:if="${like.getDiscussionId().equals(disc.getDiscussionId())}" th:text="${like.getLikes()}" class="likes_count">
     33                            <td th:text="${disc.getLikes()}" class="likes_count">
    3434                            </td>
    3535                            <td th:text="${disc.getDate()}"></td>
     
    4141                            <td th:if="${!disc.getUser().equals(user)}"><a class="btn btn-success" th:href="@{'/replies/add/{discussionId}' (discussionId=${disc.getDiscussionId()})}">Реплицирај</a> </td>
    4242                            <td th:if="${!disc.getUser().equals(user)}">
    43                                 <a class='btn btn-success button-like-discussion-alt' th:if="${!likedDiscussions.contains(disc)}" th:discussion-id="${disc.getDiscussionId()}" th:user-id="${user.getUserId()}" >❤</a>
    44                                 <a class='btn btn-danger button-unlike-discussion-alt' th:if="${likedDiscussions.contains(disc)}" th:discussion-id="${disc.getDiscussionId()}" th:user-id="${user.getUserId()}" >💔</a>
     43                                <a class='btn btn-success button-like-discussion-alt' th:if="${!likedDiscussions.contains(disc)}" th:discussion-id="${disc?.getDiscussionId()}" th:user-id="${user.getUserId()}" >❤</a>
     44                                <a class='btn btn-danger button-unlike-discussion-alt' th:if="${likedDiscussions.contains(disc)}" th:discussion-id="${disc?.getDiscussionId()}" th:user-id="${user.getUserId()}" >💔</a>
    4545                            </td>
    4646                            </th:block>
Note: See TracChangeset for help on using the changeset viewer.