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

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