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

@@ -17,7 +17,7 @@ import (
// @provider
type tenant struct{}
func (s *tenant) GetPublicProfile(ctx context.Context, id string) (*dto.TenantProfile, error) {
func (s *tenant) GetPublicProfile(ctx context.Context, userID int64, id string) (*dto.TenantProfile, error) {
tid := cast.ToInt64(id)
t, err := models.TenantQuery.WithContext(ctx).Where(models.TenantQuery.ID.Eq(tid)).First()
if err != nil {
@@ -29,12 +29,14 @@ func (s *tenant) GetPublicProfile(ctx context.Context, id string) (*dto.TenantPr
// Stats
followers, _ := models.TenantUserQuery.WithContext(ctx).Where(models.TenantUserQuery.TenantID.Eq(tid)).Count()
contents, _ := models.ContentQuery.WithContext(ctx).Where(models.ContentQuery.TenantID.Eq(tid), models.ContentQuery.Status.Eq(consts.ContentStatusPublished)).Count()
contents, _ := models.ContentQuery.WithContext(ctx).
Where(models.ContentQuery.TenantID.Eq(tid), models.ContentQuery.Status.Eq(consts.ContentStatusPublished)).
Count()
// Following status
isFollowing := false
if userID := ctx.Value(consts.CtxKeyUser); userID != nil {
uid := cast.ToInt64(userID)
if userID > 0 {
uid := userID
isFollowing, _ = models.TenantUserQuery.WithContext(ctx).
Where(models.TenantUserQuery.TenantID.Eq(tid), models.TenantUserQuery.UserID.Eq(uid)).
Exists()
@@ -52,12 +54,11 @@ func (s *tenant) GetPublicProfile(ctx context.Context, id string) (*dto.TenantPr
}, nil
}
func (s *tenant) Follow(ctx context.Context, id string) error {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
func (s *tenant) Follow(ctx context.Context, userID int64, id string) error {
if userID == 0 {
return errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
uid := userID
tid := cast.ToInt64(id)
// Check if tenant exists
@@ -83,12 +84,11 @@ func (s *tenant) Follow(ctx context.Context, id string) error {
return nil
}
func (s *tenant) Unfollow(ctx context.Context, id string) error {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
func (s *tenant) Unfollow(ctx context.Context, userID int64, id string) error {
if userID == 0 {
return errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
uid := userID
tid := cast.ToInt64(id)
_, err := models.TenantUserQuery.WithContext(ctx).
@@ -100,12 +100,11 @@ func (s *tenant) Unfollow(ctx context.Context, id string) error {
return nil
}
func (s *tenant) ListFollowed(ctx context.Context) ([]dto.TenantProfile, error) {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
func (s *tenant) ListFollowed(ctx context.Context, userID int64) ([]dto.TenantProfile, error) {
if userID == 0 {
return nil, errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
uid := userID
tbl, q := models.TenantUserQuery.QueryContext(ctx)
list, err := q.Where(tbl.UserID.Eq(uid)).Find()
@@ -122,8 +121,12 @@ func (s *tenant) ListFollowed(ctx context.Context) ([]dto.TenantProfile, error)
}
// Stats
followers, _ := models.TenantUserQuery.WithContext(ctx).Where(models.TenantUserQuery.TenantID.Eq(tu.TenantID)).Count()
contents, _ := models.ContentQuery.WithContext(ctx).Where(models.ContentQuery.TenantID.Eq(tu.TenantID), models.ContentQuery.Status.Eq(consts.ContentStatusPublished)).Count()
followers, _ := models.TenantUserQuery.WithContext(ctx).
Where(models.TenantUserQuery.TenantID.Eq(tu.TenantID)).
Count()
contents, _ := models.ContentQuery.WithContext(ctx).
Where(models.ContentQuery.TenantID.Eq(tu.TenantID), models.ContentQuery.Status.Eq(consts.ContentStatusPublished)).
Count()
data = append(data, dto.TenantProfile{
ID: cast.ToString(t.ID),
@@ -139,3 +142,16 @@ func (s *tenant) ListFollowed(ctx context.Context) ([]dto.TenantProfile, error)
return data, nil
}
// GetModelByID 获取指定 ID 的model
func (s *tenant) GetModelByID(ctx context.Context, id int64) (*models.Tenant, error) {
tbl, query := models.TenantQuery.QueryContext(ctx)
u, err := query.Where(tbl.ID.Eq(id)).First()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errorx.ErrRecordNotFound
}
return nil, errorx.ErrDatabaseError.WithCause(err)
}
return u, nil
}