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

@@ -81,7 +81,7 @@ func (s *content) List(ctx context.Context, filter *content_dto.ContentListFilte
}, nil
}
func (s *content) Get(ctx context.Context, id string) (*content_dto.ContentDetail, error) {
func (s *content) Get(ctx context.Context, userID int64, id string) (*content_dto.ContentDetail, error) {
cid := cast.ToInt64(id)
// Increment Views
@@ -110,8 +110,8 @@ func (s *content) Get(ctx context.Context, id string) (*content_dto.ContentDetai
isFavorited := false
hasAccess := false
if userID := ctx.Value(consts.CtxKeyUser); userID != nil {
uid := cast.ToInt64(userID)
if userID > 0 {
uid := userID
// Interaction
isLiked, _ = models.UserContentActionQuery.WithContext(ctx).
Where(models.UserContentActionQuery.UserID.Eq(uid),
@@ -167,7 +167,7 @@ func (s *content) Get(ctx context.Context, id string) (*content_dto.ContentDetai
return detail, nil
}
func (s *content) ListComments(ctx context.Context, id string, page int) (*requests.Pager, error) {
func (s *content) ListComments(ctx context.Context, userID int64, id string, page int) (*requests.Pager, error) {
cid := cast.ToInt64(id)
tbl, q := models.CommentQuery.QueryContext(ctx)
@@ -187,8 +187,8 @@ func (s *content) ListComments(ctx context.Context, id string, page int) (*reque
// User likes
likedMap := make(map[int64]bool)
if userID := ctx.Value(consts.CtxKeyUser); userID != nil {
uid := cast.ToInt64(userID)
if userID > 0 {
uid := userID
ids := make([]int64, len(list))
for i, v := range list {
ids[i] = v.ID
@@ -225,12 +225,11 @@ func (s *content) ListComments(ctx context.Context, id string, page int) (*reque
}, nil
}
func (s *content) CreateComment(ctx context.Context, id string, form *content_dto.CommentCreateForm) error {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
func (s *content) CreateComment(ctx context.Context, userID int64, id string, form *content_dto.CommentCreateForm) error {
if userID == 0 {
return errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
uid := userID
cid := cast.ToInt64(id)
c, err := models.ContentQuery.WithContext(ctx).Where(models.ContentQuery.ID.Eq(cid)).First()
@@ -252,12 +251,11 @@ func (s *content) CreateComment(ctx context.Context, id string, form *content_dt
return nil
}
func (s *content) LikeComment(ctx context.Context, id string) error {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
func (s *content) LikeComment(ctx context.Context, userID int64, id string) error {
if userID == 0 {
return errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
uid := userID
cmid := cast.ToInt64(id)
// Fetch comment for author
@@ -292,12 +290,11 @@ func (s *content) LikeComment(ctx context.Context, id string) error {
return nil
}
func (s *content) GetLibrary(ctx context.Context) ([]user_dto.ContentItem, error) {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
func (s *content) GetLibrary(ctx context.Context, userID int64) ([]user_dto.ContentItem, error) {
if userID == 0 {
return nil, errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
uid := userID
tbl, q := models.ContentAccessQuery.QueryContext(ctx)
accessList, err := q.Where(tbl.UserID.Eq(uid), tbl.Status.Eq(consts.ContentAccessStatusActive)).Find()
@@ -334,28 +331,28 @@ func (s *content) GetLibrary(ctx context.Context) ([]user_dto.ContentItem, error
return data, nil
}
func (s *content) GetFavorites(ctx context.Context) ([]user_dto.ContentItem, error) {
return s.getInteractList(ctx, "favorite")
func (s *content) GetFavorites(ctx context.Context, userID int64) ([]user_dto.ContentItem, error) {
return s.getInteractList(ctx, userID, "favorite")
}
func (s *content) AddFavorite(ctx context.Context, contentId string) error {
return s.addInteract(ctx, contentId, "favorite")
func (s *content) AddFavorite(ctx context.Context, userID int64, contentId string) error {
return s.addInteract(ctx, userID, contentId, "favorite")
}
func (s *content) RemoveFavorite(ctx context.Context, contentId string) error {
return s.removeInteract(ctx, contentId, "favorite")
func (s *content) RemoveFavorite(ctx context.Context, userID int64, contentId string) error {
return s.removeInteract(ctx, userID, contentId, "favorite")
}
func (s *content) GetLikes(ctx context.Context) ([]user_dto.ContentItem, error) {
return s.getInteractList(ctx, "like")
func (s *content) GetLikes(ctx context.Context, userID int64) ([]user_dto.ContentItem, error) {
return s.getInteractList(ctx, userID, "like")
}
func (s *content) AddLike(ctx context.Context, contentId string) error {
return s.addInteract(ctx, contentId, "like")
func (s *content) AddLike(ctx context.Context, userID int64, contentId string) error {
return s.addInteract(ctx, userID, contentId, "like")
}
func (s *content) RemoveLike(ctx context.Context, contentId string) error {
return s.removeInteract(ctx, contentId, "like")
func (s *content) RemoveLike(ctx context.Context, userID int64, contentId string) error {
return s.removeInteract(ctx, userID, contentId, "like")
}
func (s *content) ListTopics(ctx context.Context) ([]content_dto.Topic, error) {
@@ -480,12 +477,11 @@ func (s *content) toMediaURLs(assets []*models.ContentAsset) []content_dto.Media
return urls
}
func (s *content) addInteract(ctx context.Context, contentId, typ string) error {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
func (s *content) addInteract(ctx context.Context, userID int64, contentId, typ string) error {
if userID == 0 {
return errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
uid := userID
cid := cast.ToInt64(contentId)
// Fetch content for author
@@ -529,12 +525,11 @@ func (s *content) addInteract(ctx context.Context, contentId, typ string) error
return nil
}
func (s *content) removeInteract(ctx context.Context, contentId, typ string) error {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
func (s *content) removeInteract(ctx context.Context, userID int64, contentId, typ string) error {
if userID == 0 {
return errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
uid := userID
cid := cast.ToInt64(contentId)
return models.Q.Transaction(func(tx *models.Query) error {
@@ -556,12 +551,11 @@ func (s *content) removeInteract(ctx context.Context, contentId, typ string) err
})
}
func (s *content) getInteractList(ctx context.Context, typ string) ([]user_dto.ContentItem, error) {
userID := ctx.Value(consts.CtxKeyUser)
if userID == nil {
func (s *content) getInteractList(ctx context.Context, userID int64, typ string) ([]user_dto.ContentItem, error) {
if userID == 0 {
return nil, errorx.ErrUnauthorized
}
uid := cast.ToInt64(userID)
uid := userID
tbl, q := models.UserContentActionQuery.QueryContext(ctx)
actions, err := q.Where(tbl.UserID.Eq(uid), tbl.Type.Eq(typ)).Find()