- Timestamp:
- 02/10/22 13:28:00 (3 years ago)
- Branches:
- main
- Children:
- 980eeda
- Parents:
- 6f91f99
- Location:
- src/main
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/wediscussmovies/project/model/Discussion.java
r6f91f99 r8f1b7e9 1 1 package com.wediscussmovies.project.model; 2 2 3 import com.wediscussmovies.project.model.relation.DiscussionLikes;3 import lombok.AllArgsConstructor; 4 4 import lombok.Data; 5 import org.springframework.beans.factory.annotation.Value; 5 6 6 7 import javax.persistence.*; 7 import java.sql.Date; 8 import java.util.List; 8 import java.time.LocalDate; 9 9 import java.util.Objects; 10 10 … … 28 28 @Basic 29 29 @Column(name = "date") 30 private Date date;30 private LocalDate date; 31 31 32 32 … … 47 47 48 48 @Transient 49 private Long likes;49 private long likes; 50 50 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) { 52 52 this.type = type; 53 53 this.text = text; … … 56 56 this.user = user; 57 57 } 58 59 58 60 59 61 public Discussion() { -
src/main/java/com/wediscussmovies/project/model/Movie.java
r6f91f99 r8f1b7e9 9 9 10 10 import javax.persistence.*; 11 import java.io.Serializable; 11 12 import java.sql.Date; 12 13 import java.util.Collection; -
src/main/java/com/wediscussmovies/project/model/Person.java
r6f91f99 r8f1b7e9 7 7 import javax.persistence.*; 8 8 import javax.swing.text.DateFormatter; 9 import java.io.Serializable; 9 10 import java.sql.Date; 10 11 import java.time.LocalDate; -
src/main/java/com/wediscussmovies/project/model/User.java
r6f91f99 r8f1b7e9 10 10 11 11 import javax.persistence.*; 12 import java.io.Serializable; 12 13 import java.util.ArrayList; 13 14 import java.util.Collection; -
src/main/java/com/wediscussmovies/project/querymodels/DiscussionLikesQM.java
r6f91f99 r8f1b7e9 1 1 package com.wediscussmovies.project.querymodels; 2 2 3 import com.wediscussmovies.project.model.Movie; 4 import com.wediscussmovies.project.model.Person; 5 import com.wediscussmovies.project.model.User; 6 import lombok.AllArgsConstructor; 3 7 import lombok.Data; 8 import lombok.NoArgsConstructor; 4 9 10 import java.time.LocalDate; 11 import java.util.Date; 5 12 import java.util.Objects; 6 13 7 14 @Data 15 @AllArgsConstructor 16 @NoArgsConstructor 8 17 public class DiscussionLikesQM { 18 9 19 private Integer discussionId; 10 20 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 }19 21 20 22 @Override -
src/main/java/com/wediscussmovies/project/repository/DiscussionRepository.java
r6f91f99 r8f1b7e9 19 19 List<Discussion> findAllByPerson (Person person); 20 20 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" + 22 22 " 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") 26 25 @Transactional 27 DiscussionLikesQM findDiscussionWithLikes(@Param(" discussionId") Integer discussionId);26 DiscussionLikesQM findDiscussionWithLikes(@Param("id") Integer discussionId); 28 27 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" + 30 29 " left join DiscussionLikes dl on dl.id.discussionId = d.discussionId" + 31 30 " group by d.discussionId"+ 32 " order by count(dl.id.userId) desc")31 " order by d.discussionId asc") 33 32 @Transactional 34 List<DiscussionLikesQM> findAllDiscussionsWithLikes(); 33 List<DiscussionLikesQM 34 > findAllDiscussionsWithLikes(); 35 35 } -
src/main/java/com/wediscussmovies/project/service/DiscussionService.java
r6f91f99 r8f1b7e9 22 22 DiscussionLikesQM findLikesForDiscussionWithId(Integer discussionId); 23 23 24 void findLikesForAllDiscussions(List<Discussion> discussions);24 List<DiscussionLikesQM> findLikesForAllDiscussions(); 25 25 26 26 } -
src/main/java/com/wediscussmovies/project/service/impl/DiscussionServiceImpl.java
r6f91f99 r8f1b7e9 16 16 import org.springframework.stereotype.Service; 17 17 18 import java.sql.Date;19 18 import java.time.LocalDate; 20 19 import java.util.ArrayList; 21 20 import java.util.List; 21 import java.util.concurrent.atomic.AtomicInteger; 22 22 23 23 @Service … … 45 45 @Override 46 46 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; 48 53 } 49 54 50 55 @Override 51 56 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(); 53 58 Discussion discussion; 54 59 … … 123 128 124 129 @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(); 130 133 } 131 134 … … 145 148 @Override 146 149 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; 148 153 } 149 154 -
src/main/java/com/wediscussmovies/project/web/controller/DiscussionController.java
r6f91f99 r8f1b7e9 45 45 46 46 */ 47 List<Discussion> discussions = discussionService.listAllByTitle(titleSearch); 47 //List<Discussion> discussions = discussionService.listAllByTitle(titleSearch); 48 List<Discussion> discussions = discussionService.listAll(); 48 49 model.addAttribute("discussions", discussions); 49 50 model.addAttribute("contentTemplate", "discussionsList"); 50 51 model.addAttribute("user",LoggedUser.getLoggedUser()); 51 52 this.addModelPropertiesForUser(model); 52 this.addModelPropertiesLikes(model,null, discussions);53 // this.addModelPropertiesLikes(model,null, discussions); 53 54 return "template"; 54 55 } … … 63 64 try { 64 65 Discussion disc = discussionService.findById(id); 65 disc.setLikes(discussionService.findLikesForDiscussionWithId(disc.getDiscussionId()).getLikes());66 66 model.addAttribute("disc", disc); 67 67 model.addAttribute("contentTemplate", "discussion"); … … 185 185 // ispagja kompliciran kod vaka, podobro da dodademe i svojstvo u diskusijata i koga ke se dodade lajk 186 186 // 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 // } 197 197 } 198 198 -
src/main/resources/templates/discussion.html
r6f91f99 r8f1b7e9 15 15 </span> 16 16 <span>Број на допаѓања:</span> 17 <span th:text="${ likes}" id="likes_count"></span>17 <span th:text="${disc.getLikes()}" id="likes_count"></span> 18 18 </h4> 19 19 <br> -
src/main/resources/templates/discussionsList.html
r6f91f99 r8f1b7e9 31 31 <a th:text="${disc.getTitle()}" th:href="@{'/discussions/{id}' (id=${disc.getDiscussionId()})}"></a> 32 32 </td> 33 <td th:text="${disc.getLikes() }"class="likes_count">33 <td th:text="${disc.getLikes() != null ? disc.getLikes() : 0}" class="likes_count"> 34 34 </td> 35 35 <td th:text="${disc.getDate()}"></td>
Note:
See TracChangeset
for help on using the changeset viewer.