feat: Refactor user context handling and service methods

- Updated middleware to fetch user and tenant models by ID and set them in context.
- Refactored common service methods to accept userID as a parameter instead of extracting from context.
- Modified content service methods to include userID as a parameter for better clarity and performance.
- Adjusted coupon, creator, notification, order, tenant, user, and wallet services to utilize userID directly.
- Enhanced context key constants for improved readability and maintainability.
This commit is contained in:
2025-12-30 22:49:26 +08:00
parent 619f7a69a7
commit 54de243fa1
19 changed files with 278 additions and 252 deletions

View File

@@ -4,8 +4,10 @@ import (
"quyun/v2/app/http/v1/dto"
"quyun/v2/app/requests"
"quyun/v2/app/services"
"quyun/v2/pkg/consts"
"github.com/gofiber/fiber/v3"
"github.com/spf13/cast"
)
// @provider
@@ -45,7 +47,8 @@ func (c *Content) List(
// @Success 200 {object} dto.ContentDetail
// @Bind id path
func (c *Content) Get(ctx fiber.Ctx, id string) (*dto.ContentDetail, error) {
return services.Content.Get(ctx, id)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Content.Get(ctx.Context(), uid, id)
}
// Get comments for a content
@@ -62,7 +65,8 @@ func (c *Content) Get(ctx fiber.Ctx, id string) (*dto.ContentDetail, error) {
// @Bind id path
// @Bind page query
func (c *Content) ListComments(ctx fiber.Ctx, id string, page int) (*requests.Pager, error) {
return services.Content.ListComments(ctx, id, page)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Content.ListComments(ctx.Context(), uid, id, page)
}
// Post a comment
@@ -79,7 +83,8 @@ func (c *Content) ListComments(ctx fiber.Ctx, id string, page int) (*requests.Pa
// @Bind id path
// @Bind form body
func (c *Content) CreateComment(ctx fiber.Ctx, id string, form *dto.CommentCreateForm) error {
return services.Content.CreateComment(ctx, id, form)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Content.CreateComment(ctx.Context(), uid, id, form)
}
// Like a comment
@@ -94,7 +99,8 @@ func (c *Content) CreateComment(ctx fiber.Ctx, id string, form *dto.CommentCreat
// @Success 200 {string} string "Liked"
// @Bind id path
func (c *Content) LikeComment(ctx fiber.Ctx, id string) error {
return services.Content.LikeComment(ctx, id)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Content.LikeComment(ctx.Context(), uid, id)
}
// List curated topics