feat: add post buy count
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user