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

@@ -21,12 +21,11 @@ import (
// @provider
type order struct{}
func (s *order) ListUserOrders(ctx context.Context, status string) ([]user_dto.Order, error) {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
func (s *order) ListUserOrders(ctx context.Context, userID int64, status string) ([]user_dto.Order, error) {
if userID == 0 {
return nil, errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
uid := userID
tbl, q := models.OrderQuery.QueryContext(ctx)
q = q.Where(tbl.UserID.Eq(uid))
@@ -48,12 +47,11 @@ func (s *order) ListUserOrders(ctx context.Context, status string) ([]user_dto.O
return data, nil
}
func (s *order) GetUserOrder(ctx context.Context, id string) (*user_dto.Order, error) {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
func (s *order) GetUserOrder(ctx context.Context, userID int64, id string) (*user_dto.Order, error) {
if userID == 0 {
return nil, errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
uid := userID
oid := cast.ToInt64(id)
tbl, q := models.OrderQuery.QueryContext(ctx)
@@ -72,12 +70,11 @@ func (s *order) GetUserOrder(ctx context.Context, id string) (*user_dto.Order, e
return &dto, nil
}
func (s *order) Create(ctx context.Context, form *transaction_dto.OrderCreateForm) (*transaction_dto.OrderCreateResponse, error) {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
func (s *order) Create(ctx context.Context, userID int64, form *transaction_dto.OrderCreateForm) (*transaction_dto.OrderCreateResponse, error) {
if userID == 0 {
return nil, errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
uid := userID
cid := cast.ToInt64(form.ContentID)
// 1. Fetch Content & Price
@@ -169,12 +166,11 @@ func (s *order) Create(ctx context.Context, form *transaction_dto.OrderCreateFor
}, nil
}
func (s *order) Pay(ctx context.Context, id string, form *transaction_dto.OrderPayForm) (*transaction_dto.OrderPayResponse, error) {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
func (s *order) Pay(ctx context.Context, userID int64, id string, form *transaction_dto.OrderPayForm) (*transaction_dto.OrderPayResponse, error) {
if userID == 0 {
return nil, errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
uid := userID
oid := cast.ToInt64(id)
// Fetch Order