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

@@ -5,8 +5,11 @@ import (
auth_dto "quyun/v2/app/http/v1/dto"
"quyun/v2/app/requests"
"quyun/v2/app/services"
"quyun/v2/database/models"
"quyun/v2/pkg/consts"
"github.com/gofiber/fiber/v3"
"github.com/spf13/cast"
)
// @provider
@@ -21,8 +24,10 @@ type User struct{}
// @Accept json
// @Produce json
// @Success 200 {object} auth_dto.User
func (u *User) Me(ctx fiber.Ctx) (*auth_dto.User, error) {
return services.User.Me(ctx)
// @Bind user local key(__ctx_user)
func (u *User) Me(ctx fiber.Ctx, user *models.User) (*auth_dto.User, error) {
// uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.User.ToAuthUserDTO(user), nil
}
// Update user profile
@@ -37,7 +42,8 @@ func (u *User) Me(ctx fiber.Ctx) (*auth_dto.User, error) {
// @Success 200 {string} string "Updated"
// @Bind form body
func (u *User) Update(ctx fiber.Ctx, form *dto.UserUpdate) error {
return services.User.Update(ctx, form)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.User.Update(ctx.Context(), uid, form)
}
// Submit real-name authentication
@@ -52,7 +58,8 @@ func (u *User) Update(ctx fiber.Ctx, form *dto.UserUpdate) error {
// @Success 200 {string} string "Submitted"
// @Bind form body
func (u *User) RealName(ctx fiber.Ctx, form *dto.RealNameForm) error {
return services.User.RealName(ctx, form)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.User.RealName(ctx.Context(), uid, form)
}
// Get wallet balance and transactions
@@ -65,7 +72,8 @@ func (u *User) RealName(ctx fiber.Ctx, form *dto.RealNameForm) error {
// @Produce json
// @Success 200 {object} dto.WalletResponse
func (u *User) Wallet(ctx fiber.Ctx) (*dto.WalletResponse, error) {
return services.Wallet.GetWallet(ctx)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Wallet.GetWallet(ctx.Context(), uid)
}
// Recharge wallet
@@ -80,7 +88,8 @@ func (u *User) Wallet(ctx fiber.Ctx) (*dto.WalletResponse, error) {
// @Success 200 {object} dto.RechargeResponse
// @Bind form body
func (u *User) Recharge(ctx fiber.Ctx, form *dto.RechargeForm) (*dto.RechargeResponse, error) {
return services.Wallet.Recharge(ctx, form)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Wallet.Recharge(ctx.Context(), uid, form)
}
// List user orders
@@ -95,7 +104,8 @@ func (u *User) Recharge(ctx fiber.Ctx, form *dto.RechargeForm) (*dto.RechargeRes
// @Success 200 {array} dto.Order
// @Bind status query
func (u *User) ListOrders(ctx fiber.Ctx, status string) ([]dto.Order, error) {
return services.Order.ListUserOrders(ctx, status)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Order.ListUserOrders(ctx.Context(), uid, status)
}
// Get user order detail
@@ -110,7 +120,8 @@ func (u *User) ListOrders(ctx fiber.Ctx, status string) ([]dto.Order, error) {
// @Success 200 {object} dto.Order
// @Bind id path
func (u *User) GetOrder(ctx fiber.Ctx, id string) (*dto.Order, error) {
return services.Order.GetUserOrder(ctx, id)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Order.GetUserOrder(ctx.Context(), uid, id)
}
// Get purchased content
@@ -123,7 +134,8 @@ func (u *User) GetOrder(ctx fiber.Ctx, id string) (*dto.Order, error) {
// @Produce json
// @Success 200 {array} dto.ContentItem
func (u *User) Library(ctx fiber.Ctx) ([]dto.ContentItem, error) {
return services.Content.GetLibrary(ctx)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Content.GetLibrary(ctx.Context(), uid)
}
// Get favorites
@@ -136,7 +148,8 @@ func (u *User) Library(ctx fiber.Ctx) ([]dto.ContentItem, error) {
// @Produce json
// @Success 200 {array} dto.ContentItem
func (u *User) Favorites(ctx fiber.Ctx) ([]dto.ContentItem, error) {
return services.Content.GetFavorites(ctx)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Content.GetFavorites(ctx.Context(), uid)
}
// Add to favorites
@@ -151,7 +164,8 @@ func (u *User) Favorites(ctx fiber.Ctx) ([]dto.ContentItem, error) {
// @Success 200 {string} string "Added"
// @Bind contentId query
func (u *User) AddFavorite(ctx fiber.Ctx, contentId string) error {
return services.Content.AddFavorite(ctx, contentId)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Content.AddFavorite(ctx.Context(), uid, contentId)
}
// Remove from favorites
@@ -166,7 +180,8 @@ func (u *User) AddFavorite(ctx fiber.Ctx, contentId string) error {
// @Success 200 {string} string "Removed"
// @Bind contentId path
func (u *User) RemoveFavorite(ctx fiber.Ctx, contentId string) error {
return services.Content.RemoveFavorite(ctx, contentId)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Content.RemoveFavorite(ctx.Context(), uid, contentId)
}
// Get liked contents
@@ -179,7 +194,8 @@ func (u *User) RemoveFavorite(ctx fiber.Ctx, contentId string) error {
// @Produce json
// @Success 200 {array} dto.ContentItem
func (u *User) Likes(ctx fiber.Ctx) ([]dto.ContentItem, error) {
return services.Content.GetLikes(ctx)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Content.GetLikes(ctx.Context(), uid)
}
// Like content
@@ -194,7 +210,8 @@ func (u *User) Likes(ctx fiber.Ctx) ([]dto.ContentItem, error) {
// @Success 200 {string} string "Liked"
// @Bind contentId query
func (u *User) AddLike(ctx fiber.Ctx, contentId string) error {
return services.Content.AddLike(ctx, contentId)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Content.AddLike(ctx.Context(), uid, contentId)
}
// Unlike content
@@ -209,7 +226,8 @@ func (u *User) AddLike(ctx fiber.Ctx, contentId string) error {
// @Success 200 {string} string "Unliked"
// @Bind contentId path
func (u *User) RemoveLike(ctx fiber.Ctx, contentId string) error {
return services.Content.RemoveLike(ctx, contentId)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Content.RemoveLike(ctx.Context(), uid, contentId)
}
// Get following tenants
@@ -222,7 +240,8 @@ func (u *User) RemoveLike(ctx fiber.Ctx, contentId string) error {
// @Produce json
// @Success 200 {array} dto.TenantProfile
func (u *User) Following(ctx fiber.Ctx) ([]dto.TenantProfile, error) {
return services.Tenant.ListFollowed(ctx)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Tenant.ListFollowed(ctx.Context(), uid)
}
// Get notifications
@@ -239,7 +258,8 @@ func (u *User) Following(ctx fiber.Ctx) ([]dto.TenantProfile, error) {
// @Bind typeArg query key(type)
// @Bind page query
func (u *User) Notifications(ctx fiber.Ctx, typeArg string, page int) (*requests.Pager, error) {
return services.Notification.List(ctx, page, typeArg)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Notification.List(ctx.Context(), uid, page, typeArg)
}
// List my coupons
@@ -254,5 +274,6 @@ func (u *User) Notifications(ctx fiber.Ctx, typeArg string, page int) (*requests
// @Success 200 {array} dto.UserCouponItem
// @Bind status query
func (u *User) MyCoupons(ctx fiber.Ctx, status string) ([]dto.UserCouponItem, error) {
return services.Coupon.ListUserCoupons(ctx, status)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
return services.Coupon.ListUserCoupons(ctx.Context(), uid, status)
}