feat: 更新用户上下文处理,服务方法显式接受用户参数,简化上下文调用
This commit is contained in:
@@ -6,10 +6,8 @@ import (
|
||||
"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
|
||||
@@ -40,10 +38,10 @@ func (u *User) Me(ctx fiber.Ctx, user *models.User) (*auth_dto.User, error) {
|
||||
// @Produce json
|
||||
// @Param form body dto.UserUpdate true "Update form"
|
||||
// @Success 200 {string} string "Updated"
|
||||
// @Bind user local key(__ctx_user)
|
||||
// @Bind form body
|
||||
func (u *User) Update(ctx fiber.Ctx, form *dto.UserUpdate) error {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.User.Update(ctx.Context(), uid, form)
|
||||
func (u *User) Update(ctx fiber.Ctx, user *models.User, form *dto.UserUpdate) error {
|
||||
return services.User.Update(ctx.Context(), user.ID, form)
|
||||
}
|
||||
|
||||
// Submit real-name authentication
|
||||
@@ -56,10 +54,10 @@ func (u *User) Update(ctx fiber.Ctx, form *dto.UserUpdate) error {
|
||||
// @Produce json
|
||||
// @Param form body dto.RealNameForm true "Realname form"
|
||||
// @Success 200 {string} string "Submitted"
|
||||
// @Bind user local key(__ctx_user)
|
||||
// @Bind form body
|
||||
func (u *User) RealName(ctx fiber.Ctx, form *dto.RealNameForm) error {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.User.RealName(ctx.Context(), uid, form)
|
||||
func (u *User) RealName(ctx fiber.Ctx, user *models.User, form *dto.RealNameForm) error {
|
||||
return services.User.RealName(ctx.Context(), user.ID, form)
|
||||
}
|
||||
|
||||
// Get wallet balance and transactions
|
||||
@@ -71,9 +69,9 @@ func (u *User) RealName(ctx fiber.Ctx, form *dto.RealNameForm) error {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} dto.WalletResponse
|
||||
func (u *User) Wallet(ctx fiber.Ctx) (*dto.WalletResponse, error) {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Wallet.GetWallet(ctx.Context(), uid)
|
||||
// @Bind user local key(__ctx_user)
|
||||
func (u *User) Wallet(ctx fiber.Ctx, user *models.User) (*dto.WalletResponse, error) {
|
||||
return services.Wallet.GetWallet(ctx.Context(), user.ID)
|
||||
}
|
||||
|
||||
// Recharge wallet
|
||||
@@ -86,10 +84,10 @@ func (u *User) Wallet(ctx fiber.Ctx) (*dto.WalletResponse, error) {
|
||||
// @Produce json
|
||||
// @Param form body dto.RechargeForm true "Recharge form"
|
||||
// @Success 200 {object} dto.RechargeResponse
|
||||
// @Bind user local key(__ctx_user)
|
||||
// @Bind form body
|
||||
func (u *User) Recharge(ctx fiber.Ctx, form *dto.RechargeForm) (*dto.RechargeResponse, error) {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Wallet.Recharge(ctx.Context(), uid, form)
|
||||
func (u *User) Recharge(ctx fiber.Ctx, user *models.User, form *dto.RechargeForm) (*dto.RechargeResponse, error) {
|
||||
return services.Wallet.Recharge(ctx.Context(), user.ID, form)
|
||||
}
|
||||
|
||||
// List user orders
|
||||
@@ -102,10 +100,10 @@ func (u *User) Recharge(ctx fiber.Ctx, form *dto.RechargeForm) (*dto.RechargeRes
|
||||
// @Produce json
|
||||
// @Param status query string false "Status enum(all, unpaid, completed, refund)"
|
||||
// @Success 200 {array} dto.Order
|
||||
// @Bind user local key(__ctx_user)
|
||||
// @Bind status query
|
||||
func (u *User) ListOrders(ctx fiber.Ctx, status string) ([]dto.Order, error) {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Order.ListUserOrders(ctx.Context(), uid, status)
|
||||
func (u *User) ListOrders(ctx fiber.Ctx, user *models.User, status string) ([]dto.Order, error) {
|
||||
return services.Order.ListUserOrders(ctx.Context(), user.ID, status)
|
||||
}
|
||||
|
||||
// Get user order detail
|
||||
@@ -118,10 +116,10 @@ func (u *User) ListOrders(ctx fiber.Ctx, status string) ([]dto.Order, error) {
|
||||
// @Produce json
|
||||
// @Param id path string true "Order ID"
|
||||
// @Success 200 {object} dto.Order
|
||||
// @Bind user local key(__ctx_user)
|
||||
// @Bind id path
|
||||
func (u *User) GetOrder(ctx fiber.Ctx, id string) (*dto.Order, error) {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Order.GetUserOrder(ctx.Context(), uid, id)
|
||||
func (u *User) GetOrder(ctx fiber.Ctx, user *models.User, id string) (*dto.Order, error) {
|
||||
return services.Order.GetUserOrder(ctx.Context(), user.ID, id)
|
||||
}
|
||||
|
||||
// Get purchased content
|
||||
@@ -133,9 +131,9 @@ func (u *User) GetOrder(ctx fiber.Ctx, id string) (*dto.Order, error) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {array} dto.ContentItem
|
||||
func (u *User) Library(ctx fiber.Ctx) ([]dto.ContentItem, error) {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Content.GetLibrary(ctx.Context(), uid)
|
||||
// @Bind user local key(__ctx_user)
|
||||
func (u *User) Library(ctx fiber.Ctx, user *models.User) ([]dto.ContentItem, error) {
|
||||
return services.Content.GetLibrary(ctx.Context(), user.ID)
|
||||
}
|
||||
|
||||
// Get favorites
|
||||
@@ -147,9 +145,9 @@ func (u *User) Library(ctx fiber.Ctx) ([]dto.ContentItem, error) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {array} dto.ContentItem
|
||||
func (u *User) Favorites(ctx fiber.Ctx) ([]dto.ContentItem, error) {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Content.GetFavorites(ctx.Context(), uid)
|
||||
// @Bind user local key(__ctx_user)
|
||||
func (u *User) Favorites(ctx fiber.Ctx, user *models.User) ([]dto.ContentItem, error) {
|
||||
return services.Content.GetFavorites(ctx.Context(), user.ID)
|
||||
}
|
||||
|
||||
// Add to favorites
|
||||
@@ -162,10 +160,10 @@ func (u *User) Favorites(ctx fiber.Ctx) ([]dto.ContentItem, error) {
|
||||
// @Produce json
|
||||
// @Param contentId query string true "Content ID"
|
||||
// @Success 200 {string} string "Added"
|
||||
// @Bind user local key(__ctx_user)
|
||||
// @Bind contentId query
|
||||
func (u *User) AddFavorite(ctx fiber.Ctx, contentId string) error {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Content.AddFavorite(ctx.Context(), uid, contentId)
|
||||
func (u *User) AddFavorite(ctx fiber.Ctx, user *models.User, contentId string) error {
|
||||
return services.Content.AddFavorite(ctx.Context(), user.ID, contentId)
|
||||
}
|
||||
|
||||
// Remove from favorites
|
||||
@@ -178,10 +176,10 @@ func (u *User) AddFavorite(ctx fiber.Ctx, contentId string) error {
|
||||
// @Produce json
|
||||
// @Param contentId path string true "Content ID"
|
||||
// @Success 200 {string} string "Removed"
|
||||
// @Bind user local key(__ctx_user)
|
||||
// @Bind contentId path
|
||||
func (u *User) RemoveFavorite(ctx fiber.Ctx, contentId string) error {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Content.RemoveFavorite(ctx.Context(), uid, contentId)
|
||||
func (u *User) RemoveFavorite(ctx fiber.Ctx, user *models.User, contentId string) error {
|
||||
return services.Content.RemoveFavorite(ctx.Context(), user.ID, contentId)
|
||||
}
|
||||
|
||||
// Get liked contents
|
||||
@@ -193,9 +191,9 @@ func (u *User) RemoveFavorite(ctx fiber.Ctx, contentId string) error {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {array} dto.ContentItem
|
||||
func (u *User) Likes(ctx fiber.Ctx) ([]dto.ContentItem, error) {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Content.GetLikes(ctx.Context(), uid)
|
||||
// @Bind user local key(__ctx_user)
|
||||
func (u *User) Likes(ctx fiber.Ctx, user *models.User) ([]dto.ContentItem, error) {
|
||||
return services.Content.GetLikes(ctx.Context(), user.ID)
|
||||
}
|
||||
|
||||
// Like content
|
||||
@@ -208,10 +206,10 @@ func (u *User) Likes(ctx fiber.Ctx) ([]dto.ContentItem, error) {
|
||||
// @Produce json
|
||||
// @Param contentId query string true "Content ID"
|
||||
// @Success 200 {string} string "Liked"
|
||||
// @Bind user local key(__ctx_user)
|
||||
// @Bind contentId query
|
||||
func (u *User) AddLike(ctx fiber.Ctx, contentId string) error {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Content.AddLike(ctx.Context(), uid, contentId)
|
||||
func (u *User) AddLike(ctx fiber.Ctx, user *models.User, contentId string) error {
|
||||
return services.Content.AddLike(ctx.Context(), user.ID, contentId)
|
||||
}
|
||||
|
||||
// Unlike content
|
||||
@@ -224,10 +222,10 @@ func (u *User) AddLike(ctx fiber.Ctx, contentId string) error {
|
||||
// @Produce json
|
||||
// @Param contentId path string true "Content ID"
|
||||
// @Success 200 {string} string "Unliked"
|
||||
// @Bind user local key(__ctx_user)
|
||||
// @Bind contentId path
|
||||
func (u *User) RemoveLike(ctx fiber.Ctx, contentId string) error {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Content.RemoveLike(ctx.Context(), uid, contentId)
|
||||
func (u *User) RemoveLike(ctx fiber.Ctx, user *models.User, contentId string) error {
|
||||
return services.Content.RemoveLike(ctx.Context(), user.ID, contentId)
|
||||
}
|
||||
|
||||
// Get following tenants
|
||||
@@ -239,9 +237,9 @@ func (u *User) RemoveLike(ctx fiber.Ctx, contentId string) error {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {array} dto.TenantProfile
|
||||
func (u *User) Following(ctx fiber.Ctx) ([]dto.TenantProfile, error) {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Tenant.ListFollowed(ctx.Context(), uid)
|
||||
// @Bind user local key(__ctx_user)
|
||||
func (u *User) Following(ctx fiber.Ctx, user *models.User) ([]dto.TenantProfile, error) {
|
||||
return services.Tenant.ListFollowed(ctx.Context(), user.ID)
|
||||
}
|
||||
|
||||
// Get notifications
|
||||
@@ -255,11 +253,11 @@ func (u *User) Following(ctx fiber.Ctx) ([]dto.TenantProfile, error) {
|
||||
// @Param type query string false "Type enum(all, system, order, audit, interaction)"
|
||||
// @Param page query int false "Page number"
|
||||
// @Success 200 {object} requests.Pager{items=[]dto.Notification}
|
||||
// @Bind user local key(__ctx_user)
|
||||
// @Bind typeArg query key(type)
|
||||
// @Bind page query
|
||||
func (u *User) Notifications(ctx fiber.Ctx, typeArg string, page int) (*requests.Pager, error) {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Notification.List(ctx.Context(), uid, page, typeArg)
|
||||
func (u *User) Notifications(ctx fiber.Ctx, user *models.User, typeArg string, page int) (*requests.Pager, error) {
|
||||
return services.Notification.List(ctx.Context(), user.ID, page, typeArg)
|
||||
}
|
||||
|
||||
// List my coupons
|
||||
@@ -272,8 +270,8 @@ func (u *User) Notifications(ctx fiber.Ctx, typeArg string, page int) (*requests
|
||||
// @Produce json
|
||||
// @Param status query string false "Status (unused, used, expired)"
|
||||
// @Success 200 {array} dto.UserCouponItem
|
||||
// @Bind user local key(__ctx_user)
|
||||
// @Bind status query
|
||||
func (u *User) MyCoupons(ctx fiber.Ctx, status string) ([]dto.UserCouponItem, error) {
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Coupon.ListUserCoupons(ctx.Context(), uid, status)
|
||||
func (u *User) MyCoupons(ctx fiber.Ctx, user *models.User, status string) ([]dto.UserCouponItem, error) {
|
||||
return services.Coupon.ListUserCoupons(ctx.Context(), user.ID, status)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user