Changeset 8f1b7e9 in Git for src/main


Ignore:
Timestamp:
02/10/22 13:28:00 (3 years ago)
Author:
Test <matonikolov77@…>
Branches:
main
Children:
980eeda
Parents:
6f91f99
Message:

Changed DiscussionRepository and DiscussionServiceImpl for fetching likes

Location:
src/main
Files:
11 edited

Legend:

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

    r6f91f99 r8f1b7e9  
    11package com.wediscussmovies.project.model;
    22
    3 import com.wediscussmovies.project.model.relation.DiscussionLikes;
     3import lombok.AllArgsConstructor;
    44import lombok.Data;
     5import org.springframework.beans.factory.annotation.Value;
    56
    67import javax.persistence.*;
    7 import java.sql.Date;
    8 import java.util.List;
     8import java.time.LocalDate;
    99import java.util.Objects;
    1010
     
    2828    @Basic
    2929    @Column(name = "date")
    30     private Date date;
     30    private LocalDate date;
    3131
    3232
     
    4747
    4848    @Transient
    49     private Long likes;
     49    private long likes;
    5050
    51     public Discussion(Character type, String text, String title, Date date, User user) {
     51    public Discussion(Character type, String text, String title, LocalDate date, User user) {
    5252        this.type = type;
    5353        this.text = text;
     
    5656        this.user = user;
    5757    }
     58
     59
    5860
    5961    public Discussion() {
  • src/main/java/com/wediscussmovies/project/model/Movie.java

    r6f91f99 r8f1b7e9  
    99
    1010import javax.persistence.*;
     11import java.io.Serializable;
    1112import java.sql.Date;
    1213import java.util.Collection;
  • src/main/java/com/wediscussmovies/project/model/Person.java

    r6f91f99 r8f1b7e9  
    77import javax.persistence.*;
    88import javax.swing.text.DateFormatter;
     9import java.io.Serializable;
    910import java.sql.Date;
    1011import java.time.LocalDate;
  • src/main/java/com/wediscussmovies/project/model/User.java

    r6f91f99 r8f1b7e9  
    1010
    1111import javax.persistence.*;
     12import java.io.Serializable;
    1213import java.util.ArrayList;
    1314import java.util.Collection;
  • src/main/java/com/wediscussmovies/project/querymodels/DiscussionLikesQM.java

    r6f91f99 r8f1b7e9  
    11package com.wediscussmovies.project.querymodels;
    22
     3import com.wediscussmovies.project.model.Movie;
     4import com.wediscussmovies.project.model.Person;
     5import com.wediscussmovies.project.model.User;
     6import lombok.AllArgsConstructor;
    37import lombok.Data;
     8import lombok.NoArgsConstructor;
    49
     10import java.time.LocalDate;
     11import java.util.Date;
    512import java.util.Objects;
    613
    714@Data
     15@AllArgsConstructor
     16@NoArgsConstructor
    817public class DiscussionLikesQM {
     18
    919    private Integer discussionId;
    1020    private Long likes;
    11 
    12     public DiscussionLikesQM(Integer discussionId, Long likes) {
    13         this.discussionId = discussionId;
    14         this.likes = likes;
    15     }
    16 
    17     public DiscussionLikesQM() {
    18     }
    1921
    2022    @Override
  • src/main/java/com/wediscussmovies/project/repository/DiscussionRepository.java

    r6f91f99 r8f1b7e9  
    1919     List<Discussion> findAllByPerson (Person person);
    2020
    21      @Query(value = "select new com.wediscussmovies.project.querymodels.DiscussionLikesQM(d.discussionId, count(dl.id.userId)) from Discussion d" +
     21     @Query(value = "select new com.wediscussmovies.project.querymodels.DiscussionLikesQM(d.discussionId, count (dl.user) ) from Discussion d" +
    2222             " left join DiscussionLikes dl on dl.id.discussionId = d.discussionId" +
    23              " where :discussionId = d.discussionId" +
    24              " group by d.discussionId"+
    25              " order by count(dl.id.userId) desc")
     23             " where :id = d.discussionId" +
     24             " group by d.discussionId")
    2625     @Transactional
    27      DiscussionLikesQM findDiscussionWithLikes(@Param("discussionId") Integer discussionId);
     26     DiscussionLikesQM findDiscussionWithLikes(@Param("id") Integer discussionId);
    2827
    29      @Query(value = "select new com.wediscussmovies.project.querymodels.DiscussionLikesQM(d.discussionId, count(dl.id.userId)) from Discussion d" +
     28     @Query(value = "select new com.wediscussmovies.project.querymodels.DiscussionLikesQM (d.discussionId, count(dl.user)) from Discussion d" +
    3029             " left join DiscussionLikes dl on dl.id.discussionId = d.discussionId" +
    3130             " group by d.discussionId"+
    32              " order by count(dl.id.userId) desc")
     31               " order by d.discussionId asc")
    3332     @Transactional
    34      List<DiscussionLikesQM> findAllDiscussionsWithLikes();
     33     List<DiscussionLikesQM
     34             > findAllDiscussionsWithLikes();
    3535}
  • src/main/java/com/wediscussmovies/project/service/DiscussionService.java

    r6f91f99 r8f1b7e9  
    2222    DiscussionLikesQM findLikesForDiscussionWithId(Integer discussionId);
    2323
    24     void findLikesForAllDiscussions(List<Discussion> discussions);
     24    List<DiscussionLikesQM> findLikesForAllDiscussions();
    2525
    2626}
  • src/main/java/com/wediscussmovies/project/service/impl/DiscussionServiceImpl.java

    r6f91f99 r8f1b7e9  
    1616import org.springframework.stereotype.Service;
    1717
    18 import java.sql.Date;
    1918import java.time.LocalDate;
    2019import java.util.ArrayList;
    2120import java.util.List;
     21import java.util.concurrent.atomic.AtomicInteger;
    2222
    2323@Service
     
    4545    @Override
    4646    public List<Discussion> listAll() {
    47         return this.discussionRepository.findAll();
     47        List<Discussion> discussions = this.discussionRepository.findAll();
     48        List<DiscussionLikesQM> discussionLikes  = this.discussionRepository.findAllDiscussionsWithLikes();
     49        for (int i = 0; i < discussionLikes.size(); i++){
     50            discussions.get(i).setLikes(discussionLikes.get(i).getLikes());
     51        }
     52        return discussions;
    4853    }
    4954
    5055    @Override
    5156    public void save(Character type,Integer id,String title, String text,User user) {
    52         Date date = Date.valueOf(LocalDate.now());
     57        LocalDate date = LocalDate.now();
    5358        Discussion discussion;
    5459
     
    123128
    124129    @Override
    125     public void findLikesForAllDiscussions(List<Discussion> discussions) {
    126 //        this.discussionRepository.findAllDiscussionsWithLikes()
    127 //                .forEach(el ->{
    128 //                        if (discussions.)
    129 //                });
     130    public List<DiscussionLikesQM> findLikesForAllDiscussions() {
     131           // return this.discussionRepository.findAllDiscussionsWithLikes();
     132        return this.discussionRepository.findAllDiscussionsWithLikes();
    130133    }
    131134
     
    145148    @Override
    146149    public Discussion findById(Integer id) {
    147         return discussionRepository.findById(id).orElseThrow(() -> new DiscussionNotExistException(id));
     150        Discussion disc =  discussionRepository.findById(id).orElseThrow(() -> new DiscussionNotExistException(id));
     151        disc.setLikes(this.discussionRepository.findDiscussionWithLikes(id).getLikes());
     152        return disc;
    148153    }
    149154
  • src/main/java/com/wediscussmovies/project/web/controller/DiscussionController.java

    r6f91f99 r8f1b7e9  
    4545
    4646         */
    47         List<Discussion> discussions = discussionService.listAllByTitle(titleSearch);
     47        //List<Discussion> discussions = discussionService.listAllByTitle(titleSearch);
     48        List<Discussion> discussions = discussionService.listAll();
    4849        model.addAttribute("discussions", discussions);
    4950        model.addAttribute("contentTemplate", "discussionsList");
    5051        model.addAttribute("user",LoggedUser.getLoggedUser());
    5152        this.addModelPropertiesForUser(model);
    52         this.addModelPropertiesLikes(model,null, discussions);
     53       // this.addModelPropertiesLikes(model,null, discussions);
    5354        return "template";
    5455    }
     
    6364        try {
    6465            Discussion disc = discussionService.findById(id);
    65             disc.setLikes(discussionService.findLikesForDiscussionWithId(disc.getDiscussionId()).getLikes());
    6666            model.addAttribute("disc", disc);
    6767            model.addAttribute("contentTemplate", "discussion");
     
    185185        // ispagja kompliciran kod vaka, podobro da dodademe i svojstvo u diskusijata i koga ke se dodade lajk
    186186        // soodvetno da se zgoleme i obratno
    187         if(discussion==null){
    188             List<DiscussionLikesQM> discussionLikes = new ArrayList<>();
    189             for(Discussion d: discussions){
    190                 discussionLikes.add(discussionService.findLikesForDiscussionWithId(d.getDiscussionId()));
    191             }
    192             model.addAttribute("likes", discussionLikes);
    193         }
    194         else{
    195             model.addAttribute("likes",discussionService.findLikesForDiscussionWithId(discussion.getDiscussionId()).getLikes());
    196         }
     187//        if(discussion==null){
     188//            List<DiscussionLikesQM> discussionLikes = new ArrayList<>();
     189//            for(Discussion d: discussions){
     190//                discussionLikes.add(discussionService.findLikesForDiscussionWithId(d.getDiscussionId()));
     191//            }
     192//            model.addAttribute("likes", discussionLikes);
     193//        }
     194//        else{
     195//            model.addAttribute("likes",discussionService.findLikesForDiscussionWithId(discussion.getDiscussionId()).getLikes());
     196//        }
    197197    }
    198198
  • src/main/resources/templates/discussion.html

    r6f91f99 r8f1b7e9  
    1515            </span>
    1616            <span>Број на допаѓања:</span>
    17             <span th:text="${likes}" id="likes_count"></span>
     17            <span th:text="${disc.getLikes()}" id="likes_count"></span>
    1818        </h4>
    1919        <br>
  • src/main/resources/templates/discussionsList.html

    r6f91f99 r8f1b7e9  
    3131                                <a th:text="${disc.getTitle()}" th:href="@{'/discussions/{id}' (id=${disc.getDiscussionId()})}"></a>
    3232                            </td>
    33                             <td th:text="${disc.getLikes()}" class="likes_count">
     33                            <td th:text="${disc.getLikes() != null ? disc.getLikes() : 0}" class="likes_count">
    3434                            </td>
    3535                            <td th:text="${disc.getDate()}"></td>
Note: See TracChangeset for help on using the changeset viewer.