Skip to content

Commit

Permalink
add get list top/last post
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonPazniak committed Jan 6, 2024
1 parent 1cd6e0b commit 8eb7f90
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/moviePocket/controller/post/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,14 @@ public ResponseEntity<Boolean> getAllLikePostsByIdMovie(@RequestParam("idPost")
// public ResponseEntity<List<ParsPost>> getLeastLikedPosts() {
// return likePostService.getLeastLikedPosts();
// }

@GetMapping("/get/last")
public ResponseEntity<List<ParsPost>> getLast() {
return postService.getTop10LatestPosts();
}

@GetMapping("/get/top")
public ResponseEntity<List<ParsPost>> getTop() {
return postService.getTop10LikedPosts();
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/moviePocket/repository/post/PostRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@ public interface PostRepository extends JpaRepository<Post, Long> {
@Query("Select m from Post m where m.title like :title%")
List<Post> findAllByTitle(String title);

@Query("SELECT p FROM Post p ORDER BY p.created DESC")
List<Post> findTop10LatestPosts();

@Query("SELECT ll.post, COUNT(ll) as likeCount " +
"FROM LikePost ll " +
"WHERE ll.lickOrDis = true " +
"GROUP BY ll.post " +
"HAVING COUNT(ll) > 0 " +
"ORDER BY likeCount DESC")
List<Post> findTop10LikedPosts();

}
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,21 @@ public ResponseEntity<Boolean> authorshipCheck(Long idPost, String username) {
}
return ResponseEntity.ok(false);
}

public ResponseEntity<List<ParsPost>> getTop10LatestPosts() {
List<Post> posts = postRepository.findTop10LatestPosts();
if (!posts.isEmpty())
return ResponseEntity.ok(parsPost(posts));
else
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

public ResponseEntity<List<ParsPost>> getTop10LikedPosts() {
List<Post> posts = postRepository.findTop10LikedPosts();
if (!posts.isEmpty())
return ResponseEntity.ok(parsPost(posts));
else
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public interface PostService {

ResponseEntity<Boolean> authorshipCheck(Long idPost, String username);

ResponseEntity<List<ParsPost>> getTop10LatestPosts();

ResponseEntity<List<ParsPost>> getTop10LikedPosts();


}

0 comments on commit 8eb7f90

Please sign in to comment.