feat: add post buy count
This commit is contained in:
@@ -23,7 +23,32 @@ type posts struct{}
|
||||
// @Bind query query
|
||||
func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
|
||||
cond := models.Posts.BuildConditionWithKey(query.Keyword)
|
||||
return models.Posts.List(ctx.Context(), pagination, cond)
|
||||
pager, err := models.Posts.List(ctx.Context(), pagination, cond)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
type PostForm struct {
|
||||
@@ -142,7 +167,8 @@ func (ctl *posts) Delete(ctx fiber.Ctx, id int64) error {
|
||||
|
||||
type PostItem struct {
|
||||
*model.Posts
|
||||
Medias []*model.Medias `json:"medias"`
|
||||
Medias []*model.Medias `json:"medias"`
|
||||
BoughtCount int64 `json:"bought_count"`
|
||||
}
|
||||
|
||||
// Show posts by id
|
||||
@@ -165,3 +191,22 @@ func (ctl *posts) Show(ctx fiber.Ctx, id int64) (*PostItem, error) {
|
||||
Medias: medias,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SendTo
|
||||
// @Router /v1/admin/posts/:id/send-to/:userId [post]
|
||||
// @Bind id path
|
||||
// @Bind userId path
|
||||
func (ctl *posts) SendTo(ctx fiber.Ctx, id, userId int64) error {
|
||||
if _, err := models.Posts.GetByID(ctx.Context(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := models.Users.GetByID(ctx.Context(), userId); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := models.Posts.SendTo(ctx.Context(), id, userId); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user