feat: 更新路由绑定,使用模型作为参数以简化控制器逻辑
Some checks failed
build quyun / Build (push) Failing after 10s

This commit is contained in:
2025-12-19 23:53:15 +08:00
parent 7b18a6a0e6
commit df6a8de61d
7 changed files with 96 additions and 113 deletions

View File

@@ -37,13 +37,8 @@ func (ctl *medias) List(ctx fiber.Ctx, pagination *requests.Pagination, query *L
// @Param id path int64 true "媒体 ID"
// @Success 302 {string} string "跳转"
// @Router /admin/medias/:id [get]
// @Bind id path
func (ctl *medias) Show(ctx fiber.Ctx, id int64) error {
media, err := services.Medias.FindByID(ctx, id)
if err != nil {
return ctx.SendString("Media not found")
}
// @Bind media path key(id) model(id)
func (ctl *medias) Show(ctx fiber.Ctx, media *models.Media) error {
url, err := ctl.oss.GetSignedUrl(ctx, media.Path)
if err != nil {
return err
@@ -60,13 +55,8 @@ func (ctl *medias) Show(ctx fiber.Ctx, id int64) error {
// @Param id path int64 true "媒体 ID"
// @Success 204 {object} any "成功"
// @Router /admin/medias/:id [delete]
// @Bind id path
func (ctl *medias) Delete(ctx fiber.Ctx, id int64) error {
media, err := services.Medias.FindByID(ctx, id)
if err != nil {
return ctx.SendString("Media not found")
}
// @Bind media path key(id) model(id)
func (ctl *medias) Delete(ctx fiber.Ctx, media *models.Media) error {
if err := ctl.oss.Delete(ctx, media.Path); err != nil {
return err
}

View File

@@ -24,7 +24,7 @@ type orders struct {
wepay *wepay.Client
}
// List users
// List
//
// @Summary 订单列表
// @Tags Admin Orders
@@ -60,13 +60,8 @@ func (ctl *orders) List(
// @Param id path int64 true "订单 ID"
// @Success 200 {object} any "成功"
// @Router /admin/orders/:id/refund [post]
// @Bind id path
func (ctl *orders) Refund(ctx fiber.Ctx, id int64) error {
order, err := services.Orders.FindByID(ctx, id)
if err != nil {
return err
}
// @Bind order path key(id) model(id)
func (ctl *orders) Refund(ctx fiber.Ctx, order *models.Order) error {
user, err := services.Users.FindByID(ctx, order.UserID)
if err != nil {
return err

View File

@@ -127,13 +127,9 @@ func (ctl *posts) Create(ctx fiber.Ctx, form *PostForm) error {
// @Param form body PostForm true "请求体"
// @Success 200 {object} any "成功"
// @Router /admin/posts/:id [put]
// @Bind id path
// @Bind post path key(id) model(id)
// @Bind form body
func (ctl *posts) Update(ctx fiber.Ctx, id int64, form *PostForm) error {
post, err := services.Posts.FindByID(ctx, id)
if err != nil {
return err
}
func (ctl *posts) Update(ctx fiber.Ctx, post *models.Post, form *PostForm) error {
post.Title = form.Title
post.HeadImages = types.NewJSONType(form.HeadImages)
post.Price = form.Price
@@ -172,16 +168,8 @@ func (ctl *posts) Update(ctx fiber.Ctx, id int64, form *PostForm) error {
// @Param id path int64 true "作品 ID"
// @Success 204 {object} any "成功"
// @Router /admin/posts/:id [delete]
// @Bind id path
func (ctl *posts) Delete(ctx fiber.Ctx, id int64) error {
post, err := services.Posts.FindByID(ctx, id)
if err != nil {
return err
}
if post == nil {
return fiber.ErrNotFound
}
// @Bind post path key(id) model(id)
func (ctl *posts) Delete(ctx fiber.Ctx, post *models.Post) error {
if _, err := post.ForceDelete(ctx); err != nil {
return err
}
@@ -202,13 +190,8 @@ type PostItem struct {
// @Param id path int64 true "作品 ID"
// @Success 200 {object} PostItem "成功"
// @Router /admin/posts/:id [get]
// @Bind id path
func (ctl *posts) Show(ctx fiber.Ctx, id int64) (*PostItem, error) {
post, err := services.Posts.FindByID(ctx, id)
if err != nil {
return nil, err
}
// @Bind post path key(id) model(id)
func (ctl *posts) Show(ctx fiber.Ctx, post *models.Post) (*PostItem, error) {
medias, err := services.Medias.GetByIds(ctx, lo.Map(post.Assets.Data(), func(asset fields.MediaAsset, _ int) int64 {
return asset.Media
}))
@@ -230,19 +213,9 @@ func (ctl *posts) Show(ctx fiber.Ctx, id int64) (*PostItem, error) {
// @Param userId path int64 true "用户 ID"
// @Success 200 {object} any "成功"
// @Router /admin/posts/:id/send-to/:userId [post]
// @Bind id path
// @Bind userId path
func (ctl *posts) SendTo(ctx fiber.Ctx, id, userId int64) error {
post, err := services.Posts.FindByID(ctx, id)
if err != nil {
return err
}
user, err := services.Users.FindByID(ctx, userId)
if err != nil {
return err
}
// @Bind post path key(id) model(id)
// @Bind user path key(userId) model(id)
func (ctl *posts) SendTo(ctx fiber.Ctx, post *models.Post, user *models.User) error {
if err := services.Posts.SendTo(ctx, post.ID, user.ID); err != nil {
return err
}

View File

@@ -5,8 +5,10 @@
package admin
import (
"go.ipao.vip/gen/field"
"quyun/v2/app/middlewares"
"quyun/v2/app/requests"
"quyun/v2/database/models"
"github.com/gofiber/fiber/v3"
log "github.com/sirupsen/logrus"
@@ -57,7 +59,10 @@ func (r *Routes) Register(router fiber.Router) {
r.log.Debugf("Registering route: Delete /admin/medias/:id -> medias.Delete")
router.Delete("/admin/medias/:id"[len(r.Path()):], Func1(
r.medias.Delete,
PathParam[int64]("id"),
func(ctx fiber.Ctx) (*models.Media, error) {
v := fiber.Params[int](ctx, "id")
return models.MediaQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
))
r.log.Debugf("Registering route: Get /admin/medias -> medias.List")
router.Get("/admin/medias"[len(r.Path()):], DataFunc2(
@@ -68,7 +73,10 @@ func (r *Routes) Register(router fiber.Router) {
r.log.Debugf("Registering route: Get /admin/medias/:id -> medias.Show")
router.Get("/admin/medias/:id"[len(r.Path()):], Func1(
r.medias.Show,
PathParam[int64]("id"),
func(ctx fiber.Ctx) (*models.Media, error) {
v := fiber.Params[int](ctx, "id")
return models.MediaQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
))
// Register routes for controller: orders
r.log.Debugf("Registering route: Get /admin/orders -> orders.List")
@@ -80,13 +88,19 @@ func (r *Routes) Register(router fiber.Router) {
r.log.Debugf("Registering route: Post /admin/orders/:id/refund -> orders.Refund")
router.Post("/admin/orders/:id/refund"[len(r.Path()):], Func1(
r.orders.Refund,
PathParam[int64]("id"),
func(ctx fiber.Ctx) (*models.Order, error) {
v := fiber.Params[int](ctx, "id")
return models.OrderQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
))
// Register routes for controller: posts
r.log.Debugf("Registering route: Delete /admin/posts/:id -> posts.Delete")
router.Delete("/admin/posts/:id"[len(r.Path()):], Func1(
r.posts.Delete,
PathParam[int64]("id"),
func(ctx fiber.Ctx) (*models.Post, error) {
v := fiber.Params[int](ctx, "id")
return models.PostQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
))
r.log.Debugf("Registering route: Get /admin/posts -> posts.List")
router.Get("/admin/posts"[len(r.Path()):], DataFunc2(
@@ -97,7 +111,10 @@ func (r *Routes) Register(router fiber.Router) {
r.log.Debugf("Registering route: Get /admin/posts/:id -> posts.Show")
router.Get("/admin/posts/:id"[len(r.Path()):], DataFunc1(
r.posts.Show,
PathParam[int64]("id"),
func(ctx fiber.Ctx) (*models.Post, error) {
v := fiber.Params[int](ctx, "id")
return models.PostQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
))
r.log.Debugf("Registering route: Post /admin/posts -> posts.Create")
router.Post("/admin/posts"[len(r.Path()):], Func1(
@@ -107,13 +124,22 @@ func (r *Routes) Register(router fiber.Router) {
r.log.Debugf("Registering route: Post /admin/posts/:id/send-to/:userId -> posts.SendTo")
router.Post("/admin/posts/:id/send-to/:userId"[len(r.Path()):], Func2(
r.posts.SendTo,
PathParam[int64]("id"),
PathParam[int64]("userId"),
func(ctx fiber.Ctx) (*models.Post, error) {
v := fiber.Params[int](ctx, "id")
return models.PostQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
func(ctx fiber.Ctx) (*models.User, error) {
v := fiber.Params[int](ctx, "userId")
return models.UserQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
))
r.log.Debugf("Registering route: Put /admin/posts/:id -> posts.Update")
router.Put("/admin/posts/:id"[len(r.Path()):], Func2(
r.posts.Update,
PathParam[int64]("id"),
func(ctx fiber.Ctx) (*models.Post, error) {
v := fiber.Params[int](ctx, "id")
return models.PostQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
Body[PostForm]("form"),
))
// Register routes for controller: statistics
@@ -144,18 +170,27 @@ func (r *Routes) Register(router fiber.Router) {
r.log.Debugf("Registering route: Get /admin/users/:id -> users.Show")
router.Get("/admin/users/:id"[len(r.Path()):], DataFunc1(
r.users.Show,
PathParam[int64]("id"),
func(ctx fiber.Ctx) (*models.User, error) {
v := fiber.Params[int](ctx, "id")
return models.UserQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
))
r.log.Debugf("Registering route: Get /admin/users/:id/articles -> users.Articles")
router.Get("/admin/users/:id/articles"[len(r.Path()):], DataFunc2(
r.users.Articles,
PathParam[int64]("id"),
func(ctx fiber.Ctx) (*models.User, error) {
v := fiber.Params[int](ctx, "id")
return models.UserQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
Query[requests.Pagination]("pagination"),
))
r.log.Debugf("Registering route: Post /admin/users/:id/balance -> users.Balance")
router.Post("/admin/users/:id/balance"[len(r.Path()):], Func2(
r.users.Balance,
PathParam[int64]("id"),
func(ctx fiber.Ctx) (*models.User, error) {
v := fiber.Params[int](ctx, "id")
return models.UserQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
Body[UserBalance]("balance"),
))

View File

@@ -43,9 +43,9 @@ func (ctl *users) List(ctx fiber.Ctx, pagination *requests.Pagination, query *Us
// @Param id path int64 true "用户 ID"
// @Success 200 {object} models.User "成功"
// @Router /admin/users/:id [get]
// @Bind id path
func (ctl *users) Show(ctx fiber.Ctx, id int64) (*models.User, error) {
return services.Users.FindByID(ctx, id)
// @Bind user path key(id) model(id)
func (ctl *users) Show(ctx fiber.Ctx, user *models.User) (*models.User, error) {
return user, nil
}
// Articles show user bought articles
@@ -57,10 +57,10 @@ func (ctl *users) Show(ctx fiber.Ctx, id int64) (*models.User, error) {
// @Param pagination query requests.Pagination false "分页参数"
// @Success 200 {object} requests.Pager{items=models.Post} "成功"
// @Router /admin/users/:id/articles [get]
// @Bind id path
// @Bind user path key(id) model(id)
// @Bind pagination query
func (ctl *users) Articles(ctx fiber.Ctx, id int64, pagination *requests.Pagination) (*requests.Pager, error) {
return services.Posts.Bought(ctx, id, pagination)
func (ctl *users) Articles(ctx fiber.Ctx, user *models.User, pagination *requests.Pagination) (*requests.Pager, error) {
return services.Posts.Bought(ctx, user.ID, pagination)
}
type UserBalance struct {
@@ -77,12 +77,8 @@ type UserBalance struct {
// @Param balance body UserBalance true "请求体"
// @Success 200 {object} any "成功"
// @Router /admin/users/:id/balance [post]
// @Bind id path
// @Bind user path key(id) model(id)
// @Bind balance body
func (ctl *users) Balance(ctx fiber.Ctx, id int64, balance *UserBalance) error {
user, err := services.Users.FindByID(ctx, id)
if err != nil {
return err
}
func (ctl *users) Balance(ctx fiber.Ctx, user *models.User, balance *UserBalance) error {
return services.Users.AddBalance(ctx, user.ID, balance.Balance)
}

View File

@@ -135,19 +135,13 @@ type PostItem struct {
// @Param id path int64 true "作品 ID"
// @Success 200 {object} PostItem "成功"
// @Router /posts/:id/show [get]
// @Bind id path
// @Bind post path key(id) model(id)
// @Bind user local
func (ctl *posts) Show(ctx fiber.Ctx, id int64, user *models.User) (*PostItem, error) {
log.Infof("Fetching post with ID: %d", id)
func (ctl *posts) Show(ctx fiber.Ctx, post *models.Post, user *models.User) (*PostItem, error) {
log.Infof("Fetching post with ID: %d", post.ID)
post, err := services.Posts.FindByID(
ctx,
id,
models.PostQuery.Status.Eq(fields.PostStatusPublished),
)
if err != nil {
log.WithError(err).Errorf("GetByID err: %v", err)
return nil, err
if post.Status != fields.PostStatusPublished {
return nil, fiber.ErrNotFound
}
bought, err := services.Users.HasBought(ctx, user.ID, post.ID)
@@ -196,26 +190,21 @@ type PlayUrl struct {
// @Param id path int64 true "作品 ID"
// @Success 200 {object} PlayUrl "成功"
// @Router /posts/:id/play [get]
// @Bind id path
// @Bind post path key(id) model(id)
// @Bind user local
func (ctl *posts) Play(ctx fiber.Ctx, id int64, user *models.User) (*PlayUrl, error) {
log := log.WithField("PlayPostID", strconv.FormatInt(id, 10))
func (ctl *posts) Play(ctx fiber.Ctx, post *models.Post, user *models.User) (*PlayUrl, error) {
log := log.WithField("PlayPostID", strconv.FormatInt(post.ID, 10))
// return &PlayUrl{
// Url: "https://github.com/mediaelement/mediaelement-files/raw/refs/heads/master/big_buck_bunny.mp4",
// }, nil
preview := false
bought, err := services.Users.HasBought(ctx, user.ID, id)
bought, err := services.Users.HasBought(ctx, user.ID, post.ID)
if !bought || err != nil {
preview = true
}
log.Infof("Fetching play URL for post ID: %d", id)
post, err := services.Posts.FindByID(ctx, id)
if err != nil {
log.WithError(err).Errorf("GetByID err: %v", err)
return nil, err
}
log.Infof("Fetching play URL for post ID: %d", post.ID)
go services.Posts.IncrViewCount(ctx, post.ID)
for _, asset := range post.Assets.Data() {
@@ -320,10 +309,10 @@ func (ctl *posts) Mine(
// @Param id path int64 true "作品 ID"
// @Success 200 {object} wechat.JSAPIPayParams "成功(余额支付返回 AppId=balance"
// @Router /posts/:id/buy [post]
// @Bind id path
// @Bind post path key(id) model(id)
// @Bind user local
func (ctl *posts) Buy(ctx fiber.Ctx, id int64, user *models.User) (*wechat.JSAPIPayParams, error) {
bought, err := services.Users.HasBought(ctx, user.ID, id)
func (ctl *posts) Buy(ctx fiber.Ctx, post *models.Post, user *models.User) (*wechat.JSAPIPayParams, error) {
bought, err := services.Users.HasBought(ctx, user.ID, post.ID)
if err != nil {
return nil, errors.New("查询购买失败")
}
@@ -331,11 +320,6 @@ func (ctl *posts) Buy(ctx fiber.Ctx, id int64, user *models.User) (*wechat.JSAPI
if bought {
return nil, errors.New("已经购买过了")
}
post, err := services.Posts.FindByID(ctx, id)
if err != nil {
return nil, errors.Wrapf(err, " failed to get post: %d", id)
}
// payPrice := post.PayPrice()
order, err := services.Orders.CreateFromUserPostID(ctx, user.ID, post.ID)

View File

@@ -5,6 +5,7 @@
package http
import (
"go.ipao.vip/gen/field"
"quyun/v2/app/middlewares"
"quyun/v2/app/requests"
"quyun/v2/database/models"
@@ -54,13 +55,19 @@ func (r *Routes) Register(router fiber.Router) {
r.log.Debugf("Registering route: Get /posts/:id/play -> posts.Play")
router.Get("/posts/:id/play"[len(r.Path()):], DataFunc2(
r.posts.Play,
PathParam[int64]("id"),
func(ctx fiber.Ctx) (*models.Post, error) {
v := fiber.Params[int](ctx, "id")
return models.PostQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
Local[*models.User]("user"),
))
r.log.Debugf("Registering route: Get /posts/:id/show -> posts.Show")
router.Get("/posts/:id/show"[len(r.Path()):], DataFunc2(
r.posts.Show,
PathParam[int64]("id"),
func(ctx fiber.Ctx) (*models.Post, error) {
v := fiber.Params[int](ctx, "id")
return models.PostQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
Local[*models.User]("user"),
))
r.log.Debugf("Registering route: Get /posts/mine -> posts.Mine")
@@ -73,7 +80,10 @@ func (r *Routes) Register(router fiber.Router) {
r.log.Debugf("Registering route: Post /posts/:id/buy -> posts.Buy")
router.Post("/posts/:id/buy"[len(r.Path()):], DataFunc2(
r.posts.Buy,
PathParam[int64]("id"),
func(ctx fiber.Ctx) (*models.Post, error) {
v := fiber.Params[int](ctx, "id")
return models.PostQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
Local[*models.User]("user"),
))
// Register routes for controller: users