✨ Featured posts
This commit is contained in:
40
pkg/internal/services/featured.go
Normal file
40
pkg/internal/services/featured.go
Normal file
@ -0,0 +1,40 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GetFeaturedPosts How to determine featured posts?
|
||||
// Get the most upvoted posts in the last 7 days
|
||||
// And then how to get the upvote count of each post in the last 7 days?
|
||||
// We will get the reactions that attitude equals to 1 and created within the last 7 days
|
||||
// By the way, the upvote count will subtract the downvote count
|
||||
// Notice, this function is a raw query, it is not recommended to return the result directly
|
||||
// Instead, you should get the id and query it again via the ListPost function
|
||||
func GetFeaturedPosts(count int) ([]models.Post, error) {
|
||||
deadline := time.Now().Add(-7 * 24 * time.Hour)
|
||||
|
||||
var posts []models.Post
|
||||
if err := database.C.Raw(`
|
||||
SELECT p.*
|
||||
FROM posts p
|
||||
JOIN (
|
||||
SELECT
|
||||
post_id,
|
||||
SUM(CASE WHEN attitude = 1 THEN 1 ELSE 0 END) -
|
||||
SUM(CASE WHEN attitude = 2 THEN 1 ELSE 0 END) AS social_points
|
||||
FROM reactions
|
||||
WHERE created_at >= ?
|
||||
GROUP BY post_id
|
||||
ORDER BY social_points DESC
|
||||
LIMIT ?
|
||||
) t ON p.id = t.post_id
|
||||
ORDER BY t.social_points DESC, p.published_at DESC
|
||||
`, deadline, count).Scan(&posts).Error; err != nil {
|
||||
return posts, err
|
||||
}
|
||||
|
||||
return posts, nil
|
||||
}
|
Reference in New Issue
Block a user