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

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