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:
@@ -20,12 +20,11 @@ import (
|
||||
// @provider
|
||||
type creator struct{}
|
||||
|
||||
func (s *creator) Apply(ctx context.Context, form *creator_dto.ApplyForm) error {
|
||||
userID := ctx.Value(consts.CtxKeyUser)
|
||||
if userID == nil {
|
||||
func (s *creator) Apply(ctx context.Context, userID int64, form *creator_dto.ApplyForm) error {
|
||||
if userID == 0 {
|
||||
return errorx.ErrUnauthorized
|
||||
}
|
||||
uid := cast.ToInt64(userID)
|
||||
uid := userID
|
||||
|
||||
tbl, q := models.TenantQuery.QueryContext(ctx)
|
||||
// Check if already has a tenant
|
||||
@@ -62,8 +61,8 @@ func (s *creator) Apply(ctx context.Context, form *creator_dto.ApplyForm) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *creator) Dashboard(ctx context.Context) (*creator_dto.DashboardStats, error) {
|
||||
tid, err := s.getTenantID(ctx)
|
||||
func (s *creator) Dashboard(ctx context.Context, userID int64) (*creator_dto.DashboardStats, error) {
|
||||
tid, err := s.getTenantID(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -95,8 +94,8 @@ func (s *creator) Dashboard(ctx context.Context) (*creator_dto.DashboardStats, e
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (s *creator) ListContents(ctx context.Context, filter *creator_dto.CreatorContentListFilter) ([]creator_dto.ContentItem, error) {
|
||||
tid, err := s.getTenantID(ctx)
|
||||
func (s *creator) ListContents(ctx context.Context, userID int64, filter *creator_dto.CreatorContentListFilter) ([]creator_dto.ContentItem, error) {
|
||||
tid, err := s.getTenantID(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -133,12 +132,12 @@ func (s *creator) ListContents(ctx context.Context, filter *creator_dto.CreatorC
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (s *creator) CreateContent(ctx context.Context, form *creator_dto.ContentCreateForm) error {
|
||||
tid, err := s.getTenantID(ctx)
|
||||
func (s *creator) CreateContent(ctx context.Context, userID int64, form *creator_dto.ContentCreateForm) error {
|
||||
tid, err := s.getTenantID(ctx, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
uid := cast.ToInt64(ctx.Value(consts.CtxKeyUser))
|
||||
uid := userID
|
||||
|
||||
return models.Q.Transaction(func(tx *models.Query) error {
|
||||
// 1. Create Content
|
||||
@@ -187,13 +186,13 @@ func (s *creator) CreateContent(ctx context.Context, form *creator_dto.ContentCr
|
||||
})
|
||||
}
|
||||
|
||||
func (s *creator) UpdateContent(ctx context.Context, id string, form *creator_dto.ContentUpdateForm) error {
|
||||
tid, err := s.getTenantID(ctx)
|
||||
func (s *creator) UpdateContent(ctx context.Context, userID int64, id string, form *creator_dto.ContentUpdateForm) error {
|
||||
tid, err := s.getTenantID(ctx, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cid := cast.ToInt64(id)
|
||||
uid := cast.ToInt64(ctx.Value(consts.CtxKeyUser))
|
||||
uid := userID
|
||||
|
||||
return models.Q.Transaction(func(tx *models.Query) error {
|
||||
// 1. Check Ownership
|
||||
@@ -257,9 +256,9 @@ func (s *creator) UpdateContent(ctx context.Context, id string, form *creator_dt
|
||||
})
|
||||
}
|
||||
|
||||
func (s *creator) DeleteContent(ctx context.Context, id string) error {
|
||||
func (s *creator) DeleteContent(ctx context.Context, userID int64, id string) error {
|
||||
cid := cast.ToInt64(id)
|
||||
tid, err := s.getTenantID(ctx)
|
||||
tid, err := s.getTenantID(ctx, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -271,8 +270,8 @@ func (s *creator) DeleteContent(ctx context.Context, id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *creator) ListOrders(ctx context.Context, filter *creator_dto.CreatorOrderListFilter) ([]creator_dto.Order, error) {
|
||||
tid, err := s.getTenantID(ctx)
|
||||
func (s *creator) ListOrders(ctx context.Context, userID int64, filter *creator_dto.CreatorOrderListFilter) ([]creator_dto.Order, error) {
|
||||
tid, err := s.getTenantID(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -302,13 +301,13 @@ func (s *creator) ListOrders(ctx context.Context, filter *creator_dto.CreatorOrd
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (s *creator) ProcessRefund(ctx context.Context, id string, form *creator_dto.RefundForm) error {
|
||||
tid, err := s.getTenantID(ctx)
|
||||
func (s *creator) ProcessRefund(ctx context.Context, userID int64, id string, form *creator_dto.RefundForm) error {
|
||||
tid, err := s.getTenantID(ctx, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
oid := cast.ToInt64(id)
|
||||
uid := cast.ToInt64(ctx.Value(consts.CtxKeyUser)) // Creator ID
|
||||
uid := userID // Creator ID
|
||||
|
||||
// Fetch Order
|
||||
o, err := models.OrderQuery.WithContext(ctx).Where(models.OrderQuery.ID.Eq(oid), models.OrderQuery.TenantID.Eq(tid)).First()
|
||||
@@ -402,8 +401,8 @@ func (s *creator) ProcessRefund(ctx context.Context, id string, form *creator_dt
|
||||
return errorx.ErrBadRequest.WithMsg("无效的操作")
|
||||
}
|
||||
|
||||
func (s *creator) GetSettings(ctx context.Context) (*creator_dto.Settings, error) {
|
||||
tid, err := s.getTenantID(ctx)
|
||||
func (s *creator) GetSettings(ctx context.Context, userID int64) (*creator_dto.Settings, error) {
|
||||
tid, err := s.getTenantID(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -418,12 +417,12 @@ func (s *creator) GetSettings(ctx context.Context) (*creator_dto.Settings, error
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *creator) UpdateSettings(ctx context.Context, form *creator_dto.Settings) error {
|
||||
func (s *creator) UpdateSettings(ctx context.Context, userID int64, form *creator_dto.Settings) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *creator) ListPayoutAccounts(ctx context.Context) ([]creator_dto.PayoutAccount, error) {
|
||||
tid, err := s.getTenantID(ctx)
|
||||
func (s *creator) ListPayoutAccounts(ctx context.Context, userID int64) ([]creator_dto.PayoutAccount, error) {
|
||||
tid, err := s.getTenantID(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -446,12 +445,12 @@ func (s *creator) ListPayoutAccounts(ctx context.Context) ([]creator_dto.PayoutA
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (s *creator) AddPayoutAccount(ctx context.Context, form *creator_dto.PayoutAccount) error {
|
||||
tid, err := s.getTenantID(ctx)
|
||||
func (s *creator) AddPayoutAccount(ctx context.Context, userID int64, form *creator_dto.PayoutAccount) error {
|
||||
tid, err := s.getTenantID(ctx, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
uid := cast.ToInt64(ctx.Value(consts.CtxKeyUser))
|
||||
uid := userID
|
||||
|
||||
pa := &models.PayoutAccount{
|
||||
TenantID: tid,
|
||||
@@ -467,8 +466,8 @@ func (s *creator) AddPayoutAccount(ctx context.Context, form *creator_dto.Payout
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *creator) RemovePayoutAccount(ctx context.Context, id string) error {
|
||||
tid, err := s.getTenantID(ctx)
|
||||
func (s *creator) RemovePayoutAccount(ctx context.Context, userID int64, id string) error {
|
||||
tid, err := s.getTenantID(ctx, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -483,12 +482,12 @@ func (s *creator) RemovePayoutAccount(ctx context.Context, id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *creator) Withdraw(ctx context.Context, form *creator_dto.WithdrawForm) error {
|
||||
tid, err := s.getTenantID(ctx)
|
||||
func (s *creator) Withdraw(ctx context.Context, userID int64, form *creator_dto.WithdrawForm) error {
|
||||
tid, err := s.getTenantID(ctx, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
uid := cast.ToInt64(ctx.Value(consts.CtxKeyUser))
|
||||
uid := userID
|
||||
|
||||
amount := int64(form.Amount * 100)
|
||||
if amount <= 0 {
|
||||
@@ -552,12 +551,11 @@ func (s *creator) Withdraw(ctx context.Context, form *creator_dto.WithdrawForm)
|
||||
|
||||
// Helpers
|
||||
|
||||
func (s *creator) getTenantID(ctx context.Context) (int64, error) {
|
||||
userID := ctx.Value(consts.CtxKeyUser)
|
||||
if userID == nil {
|
||||
func (s *creator) getTenantID(ctx context.Context, userID int64) (int64, error) {
|
||||
if userID == 0 {
|
||||
return 0, errorx.ErrUnauthorized
|
||||
}
|
||||
uid := cast.ToInt64(userID)
|
||||
uid := userID
|
||||
|
||||
// Simple check: User owns tenant
|
||||
t, err := models.TenantQuery.WithContext(ctx).Where(models.TenantQuery.UserID.Eq(uid)).First()
|
||||
|
||||
Reference in New Issue
Block a user