🐛 Fix get single won't get react info
This commit is contained in:
parent
bbdc8e6aa6
commit
4d618a91fe
@ -53,14 +53,16 @@ func getPost(c *fiber.Ctx) error {
|
|||||||
take := c.QueryInt("take", 0)
|
take := c.QueryInt("take", 0)
|
||||||
offset := c.QueryInt("offset", 0)
|
offset := c.QueryInt("offset", 0)
|
||||||
|
|
||||||
var post models.Post
|
tx := database.C.Where(&models.Post{
|
||||||
if err := services.PreloadRelatedPost(database.C.Where(&models.Post{
|
|
||||||
BaseModel: models.BaseModel{ID: uint(id)},
|
BaseModel: models.BaseModel{ID: uint(id)},
|
||||||
})).First(&post).Error; err != nil {
|
})
|
||||||
|
|
||||||
|
post, err := services.GetPost(tx)
|
||||||
|
if err != nil {
|
||||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
tx := database.C.
|
tx = database.C.
|
||||||
Where(&models.Post{ReplyID: &post.ID}).
|
Where(&models.Post{ReplyID: &post.ID}).
|
||||||
Where("published_at <= ? OR published_at IS NULL", time.Now()).
|
Where("published_at <= ? OR published_at IS NULL", time.Now()).
|
||||||
Order("created_at desc")
|
Order("created_at desc")
|
||||||
|
@ -30,6 +30,38 @@ func PreloadRelatedPost(tx *gorm.DB) *gorm.DB {
|
|||||||
Preload("ReplyTo.Tags")
|
Preload("ReplyTo.Tags")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetPost(tx *gorm.DB) (*models.Post, error) {
|
||||||
|
var post *models.Post
|
||||||
|
if err := PreloadRelatedPost(tx).
|
||||||
|
First(&post).Error; err != nil {
|
||||||
|
return post, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var reactInfo struct {
|
||||||
|
PostID uint
|
||||||
|
LikeCount int64
|
||||||
|
DislikeCount int64
|
||||||
|
}
|
||||||
|
|
||||||
|
prefix := viper.GetString("database.prefix")
|
||||||
|
database.C.Raw(fmt.Sprintf(`SELECT t.id as post_id,
|
||||||
|
COALESCE(l.like_count, 0) AS like_count,
|
||||||
|
COALESCE(d.dislike_count, 0) AS dislike_count
|
||||||
|
FROM %sposts t
|
||||||
|
LEFT JOIN (SELECT post_id, COUNT(*) AS like_count
|
||||||
|
FROM %spost_likes
|
||||||
|
GROUP BY post_id) l ON t.id = l.post_id
|
||||||
|
LEFT JOIN (SELECT post_id, COUNT(*) AS dislike_count
|
||||||
|
FROM %spost_dislikes
|
||||||
|
GROUP BY post_id) d ON t.id = d.post_id
|
||||||
|
WHERE t.id = ?`, prefix, prefix, prefix), post.ID).Scan(&reactInfo)
|
||||||
|
|
||||||
|
post.LikeCount = reactInfo.LikeCount
|
||||||
|
post.DislikeCount = reactInfo.DislikeCount
|
||||||
|
|
||||||
|
return post, nil
|
||||||
|
}
|
||||||
|
|
||||||
func ListPost(tx *gorm.DB, take int, offset int) ([]*models.Post, error) {
|
func ListPost(tx *gorm.DB, take int, offset int) ([]*models.Post, error) {
|
||||||
var posts []*models.Post
|
var posts []*models.Post
|
||||||
if err := PreloadRelatedPost(tx).
|
if err := PreloadRelatedPost(tx).
|
||||||
|
Loading…
Reference in New Issue
Block a user