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

@@ -38,3 +38,22 @@ func (ctl *medias) Show(ctx fiber.Ctx, id int64) error {
return ctx.Redirect().To(url)
}
// Delete
// @Router /v1/admin/medias/:id [delete]
// @Bind id path
func (ctl *medias) Delete(ctx fiber.Ctx, id int64) error {
media, err := models.Medias.GetByID(ctx.Context(), id)
if err != nil {
return ctx.SendString("Media not found")
}
if err := ctl.oss.Delete(ctx.Context(), media.Path); err != nil {
return err
}
if err := models.Medias.Delete(ctx.Context(), id); err != nil {
return err
}
return ctx.SendStatus(fiber.StatusNoContent)
}

View File

@@ -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
}

View File

@@ -50,6 +50,11 @@ func (r *Routes) Register(router fiber.Router) {
PathParam[int64]("id"),
))
router.Delete("/v1/admin/medias/:id", Func1(
r.medias.Delete,
PathParam[int64]("id"),
))
// 注册路由组: orders
router.Get("/v1/admin/orders", DataFunc2(
r.orders.List,
@@ -85,6 +90,12 @@ func (r *Routes) Register(router fiber.Router) {
PathParam[int64]("id"),
))
router.Post("/v1/admin/posts/:id/send-to/:userId", Func2(
r.posts.SendTo,
PathParam[int64]("id"),
PathParam[int64]("userId"),
))
// 注册路由组: uploads
router.Get("/v1/admin/uploads/pre-uploaded-check/:md5.:ext", DataFunc3(
r.uploads.PreUploadCheck,