-
Notifications
You must be signed in to change notification settings - Fork 692
/
Copy pathPostRepository.java
37 lines (29 loc) · 1.37 KB
/
PostRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.raysmond.blog.repositories;
import com.raysmond.blog.models.Post;
import com.raysmond.blog.models.support.PostStatus;
import com.raysmond.blog.models.support.PostType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* @author Raysmond
*/
@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
Post findByPermalinkAndPostStatus(String permalink, PostStatus postStatus);
Page<Post> findAllByPostType(PostType postType, Pageable pageRequest);
Page<Post> findAllByPostTypeAndPostStatus(PostType postType, PostStatus postStatus, Pageable pageRequest);
@Query("SELECT p FROM Post p INNER JOIN p.tags t WHERE t.name = :tag")
Page<Post> findByTag(@Param("tag") String tag, Pageable pageable);
@Query("SELECT t.name, count(p) as tag_count from Post p " +
"INNER JOIN p.tags t " +
"WHERE p.postStatus = :status " +
"GROUP BY t.id " +
"ORDER BY tag_count DESC")
List<Object[]> countPostsByTags(@Param("status") PostStatus status);
}