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

@@ -20,15 +20,9 @@ type notification struct {
job *job.Job
}
func (s *notification) List(ctx context.Context, page int, typeArg string) (*requests.Pager, error) {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
return nil, errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
func (s *notification) List(ctx context.Context, userID int64, page int, typeArg string) (*requests.Pager, error) {
tbl, q := models.NotificationQuery.QueryContext(ctx)
q = q.Where(tbl.UserID.Eq(uid))
q = q.Where(tbl.UserID.Eq(userID))
if typeArg != "" && typeArg != "all" {
q = q.Where(tbl.Type.Eq(typeArg))
@@ -66,16 +60,11 @@ func (s *notification) List(ctx context.Context, page int, typeArg string) (*req
}, nil
}
func (s *notification) MarkRead(ctx context.Context, id string) error {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
return errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
func (s *notification) MarkRead(ctx context.Context, userID int64, id string) error {
nid := cast.ToInt64(id)
_, err := models.NotificationQuery.WithContext(ctx).
Where(models.NotificationQuery.ID.Eq(nid), models.NotificationQuery.UserID.Eq(uid)).
Where(models.NotificationQuery.ID.Eq(nid), models.NotificationQuery.UserID.Eq(userID)).
UpdateSimple(models.NotificationQuery.IsRead.Value(true))
if err != nil {
return errorx.ErrDatabaseError.WithCause(err)