Add tag filtering to ListPosts and enhance ListTags with post count
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 53s
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 53s
This commit is contained in:
@@ -39,23 +39,71 @@ func (l *ListPostsLogic) ListPosts(req *types.ListPostsReq) (resp *types.ListPos
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
// Build query with optional tag filter
|
||||
var countQuery string
|
||||
var postsQuery string
|
||||
var args []interface{}
|
||||
|
||||
if len(req.TagIds) > 0 {
|
||||
// Filter by tags - posts that have ALL specified tags
|
||||
countQuery = `
|
||||
SELECT COUNT(DISTINCT p.id)
|
||||
FROM posts p
|
||||
INNER JOIN post_hashtags ph ON p.id = ph.post_id
|
||||
WHERE ph.hashtag_id = ANY($1)
|
||||
GROUP BY p.id
|
||||
HAVING COUNT(DISTINCT ph.hashtag_id) = $2
|
||||
`
|
||||
|
||||
postsQuery = `
|
||||
SELECT DISTINCT p.id, p.title, p.created_at, p.updated_at, p.cover_id
|
||||
FROM posts p
|
||||
INNER JOIN post_hashtags ph ON p.id = ph.post_id
|
||||
WHERE ph.hashtag_id = ANY($1)
|
||||
GROUP BY p.id, p.title, p.created_at, p.updated_at, p.cover_id
|
||||
HAVING COUNT(DISTINCT ph.hashtag_id) = $2
|
||||
ORDER BY p.created_at DESC
|
||||
LIMIT $3 OFFSET $4
|
||||
`
|
||||
|
||||
args = []interface{}{req.TagIds, len(req.TagIds), pageSize, offset}
|
||||
} else {
|
||||
// No tag filter - get all posts
|
||||
countQuery = `SELECT COUNT(*) FROM posts`
|
||||
postsQuery = `
|
||||
SELECT p.id, p.title, p.created_at, p.updated_at, p.cover_id
|
||||
FROM posts p
|
||||
ORDER BY p.created_at DESC
|
||||
LIMIT $1 OFFSET $2
|
||||
`
|
||||
args = []interface{}{pageSize, offset}
|
||||
}
|
||||
|
||||
// Get total count
|
||||
var totalCount int
|
||||
countQuery := `SELECT COUNT(*) FROM posts`
|
||||
err = l.svcCtx.DB.QueryRowContext(l.ctx, countQuery).Scan(&totalCount)
|
||||
if err != nil {
|
||||
l.Errorf("Failed to get total count: %v", err)
|
||||
return nil, err
|
||||
if len(req.TagIds) > 0 {
|
||||
// Count posts matching tag filter
|
||||
rows, err := l.svcCtx.DB.QueryContext(l.ctx, countQuery, req.TagIds, len(req.TagIds))
|
||||
if err != nil {
|
||||
l.Errorf("Failed to get total count: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
totalCount = 0
|
||||
for rows.Next() {
|
||||
totalCount++
|
||||
}
|
||||
} else {
|
||||
err = l.svcCtx.DB.QueryRowContext(l.ctx, countQuery).Scan(&totalCount)
|
||||
if err != nil {
|
||||
l.Errorf("Failed to get total count: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Get posts with pagination
|
||||
postsQuery := `
|
||||
SELECT p.id, p.title, p.created_at, p.updated_at, p.cover_id
|
||||
FROM posts p
|
||||
ORDER BY p.created_at DESC
|
||||
LIMIT $1 OFFSET $2
|
||||
`
|
||||
rows, err := l.svcCtx.DB.QueryContext(l.ctx, postsQuery, pageSize, offset)
|
||||
rows, err := l.svcCtx.DB.QueryContext(l.ctx, postsQuery, args...)
|
||||
if err != nil {
|
||||
l.Errorf("Failed to get posts: %v", err)
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user