feat: add post buy count

This commit is contained in:
yanghao05
2025-04-18 22:10:28 +08:00
parent 2cb7960302
commit 192bd07b9e
13 changed files with 429 additions and 11 deletions

View File

@@ -14,6 +14,7 @@ import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
"github.com/pkg/errors"
"github.com/samber/lo"
)
type ListQuery struct {
@@ -25,6 +26,11 @@ type posts struct {
wepay *wepay.Client
}
type PostItem struct {
model.Posts
BoughtCount int64 `json:"bought_count"`
}
// List posts
// @Router /api/posts [get]
// @Bind pagination query
@@ -32,11 +38,34 @@ type posts struct {
// @Bind user local
func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery, user *model.Users) (*requests.Pager, error) {
cond := models.Posts.BuildConditionWithKey(query.Keyword)
return models.Posts.List(ctx.Context(), pagination, cond, func(item model.Posts) model.Posts {
pager, err := models.Posts.List(ctx.Context(), pagination, cond, func(item model.Posts) model.Posts {
item.Assets = fields.ToJson([]fields.MediaAsset{})
item.Content = ""
return item
})
postIds := lo.Map(pager.Items.([]model.Posts), func(item model.Posts, _ int) int64 {
return item.ID
})
if len(postIds) > 0 {
postCntMap, err := models.Posts.BoughtStatistics(ctx.Context(), postIds)
if err != nil {
return pager, err
}
items := lo.Map(pager.Items.([]model.Posts), func(item model.Posts, _ int) PostItem {
cnt := int64(0)
if v, ok := postCntMap[item.ID]; ok {
cnt = v
}
return PostItem{Posts: item, BoughtCount: cnt}
})
pager.Items = items
}
return pager, err
}
// Show