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:
@@ -3,8 +3,10 @@ package v1
|
||||
import (
|
||||
"quyun/v2/app/http/v1/dto"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// @provider
|
||||
@@ -22,7 +24,8 @@ type Creator struct{}
|
||||
// @Success 200 {string} string "Application submitted"
|
||||
// @Bind form body
|
||||
func (c *Creator) Apply(ctx fiber.Ctx, form *dto.ApplyForm) error {
|
||||
return services.Creator.Apply(ctx, form)
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Creator.Apply(ctx.Context(), uid, form)
|
||||
}
|
||||
|
||||
// Get creator dashboard stats
|
||||
@@ -35,7 +38,8 @@ func (c *Creator) Apply(ctx fiber.Ctx, form *dto.ApplyForm) error {
|
||||
// @Produce json
|
||||
// @Success 200 {object} dto.DashboardStats
|
||||
func (c *Creator) Dashboard(ctx fiber.Ctx) (*dto.DashboardStats, error) {
|
||||
return services.Creator.Dashboard(ctx)
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Creator.Dashboard(ctx.Context(), uid)
|
||||
}
|
||||
|
||||
// List creator contents
|
||||
@@ -52,7 +56,8 @@ func (c *Creator) Dashboard(ctx fiber.Ctx) (*dto.DashboardStats, error) {
|
||||
// @Success 200 {array} dto.ContentItem
|
||||
// @Bind filter query
|
||||
func (c *Creator) ListContents(ctx fiber.Ctx, filter *dto.CreatorContentListFilter) ([]dto.ContentItem, error) {
|
||||
return services.Creator.ListContents(ctx, filter)
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Creator.ListContents(ctx.Context(), uid, filter)
|
||||
}
|
||||
|
||||
// Create/Publish content
|
||||
@@ -67,7 +72,8 @@ func (c *Creator) ListContents(ctx fiber.Ctx, filter *dto.CreatorContentListFilt
|
||||
// @Success 200 {string} string "Created"
|
||||
// @Bind form body
|
||||
func (c *Creator) CreateContent(ctx fiber.Ctx, form *dto.ContentCreateForm) error {
|
||||
return services.Creator.CreateContent(ctx, form)
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Creator.CreateContent(ctx.Context(), uid, form)
|
||||
}
|
||||
|
||||
// Update content
|
||||
@@ -84,7 +90,8 @@ func (c *Creator) CreateContent(ctx fiber.Ctx, form *dto.ContentCreateForm) erro
|
||||
// @Bind id path
|
||||
// @Bind form body
|
||||
func (c *Creator) UpdateContent(ctx fiber.Ctx, id string, form *dto.ContentUpdateForm) error {
|
||||
return services.Creator.UpdateContent(ctx, id, form)
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Creator.UpdateContent(ctx.Context(), uid, id, form)
|
||||
}
|
||||
|
||||
// Delete content
|
||||
@@ -99,7 +106,8 @@ func (c *Creator) UpdateContent(ctx fiber.Ctx, id string, form *dto.ContentUpdat
|
||||
// @Success 200 {string} string "Deleted"
|
||||
// @Bind id path
|
||||
func (c *Creator) DeleteContent(ctx fiber.Ctx, id string) error {
|
||||
return services.Creator.DeleteContent(ctx, id)
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Creator.DeleteContent(ctx.Context(), uid, id)
|
||||
}
|
||||
|
||||
// List sales orders
|
||||
@@ -115,7 +123,8 @@ func (c *Creator) DeleteContent(ctx fiber.Ctx, id string) error {
|
||||
// @Success 200 {array} dto.Order
|
||||
// @Bind filter query
|
||||
func (c *Creator) ListOrders(ctx fiber.Ctx, filter *dto.CreatorOrderListFilter) ([]dto.Order, error) {
|
||||
return services.Creator.ListOrders(ctx, filter)
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Creator.ListOrders(ctx.Context(), uid, filter)
|
||||
}
|
||||
|
||||
// Process refund
|
||||
@@ -132,7 +141,8 @@ func (c *Creator) ListOrders(ctx fiber.Ctx, filter *dto.CreatorOrderListFilter)
|
||||
// @Bind id path
|
||||
// @Bind form body
|
||||
func (c *Creator) Refund(ctx fiber.Ctx, id string, form *dto.RefundForm) error {
|
||||
return services.Creator.ProcessRefund(ctx, id, form)
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Creator.ProcessRefund(ctx.Context(), uid, id, form)
|
||||
}
|
||||
|
||||
// Get channel settings
|
||||
@@ -145,7 +155,8 @@ func (c *Creator) Refund(ctx fiber.Ctx, id string, form *dto.RefundForm) error {
|
||||
// @Produce json
|
||||
// @Success 200 {object} dto.Settings
|
||||
func (c *Creator) GetSettings(ctx fiber.Ctx) (*dto.Settings, error) {
|
||||
return services.Creator.GetSettings(ctx)
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Creator.GetSettings(ctx.Context(), uid)
|
||||
}
|
||||
|
||||
// Update channel settings
|
||||
@@ -160,7 +171,8 @@ func (c *Creator) GetSettings(ctx fiber.Ctx) (*dto.Settings, error) {
|
||||
// @Success 200 {string} string "Updated"
|
||||
// @Bind form body
|
||||
func (c *Creator) UpdateSettings(ctx fiber.Ctx, form *dto.Settings) error {
|
||||
return services.Creator.UpdateSettings(ctx, form)
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Creator.UpdateSettings(ctx.Context(), uid, form)
|
||||
}
|
||||
|
||||
// List payout accounts
|
||||
@@ -173,7 +185,8 @@ func (c *Creator) UpdateSettings(ctx fiber.Ctx, form *dto.Settings) error {
|
||||
// @Produce json
|
||||
// @Success 200 {array} dto.PayoutAccount
|
||||
func (c *Creator) ListPayoutAccounts(ctx fiber.Ctx) ([]dto.PayoutAccount, error) {
|
||||
return services.Creator.ListPayoutAccounts(ctx)
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Creator.ListPayoutAccounts(ctx.Context(), uid)
|
||||
}
|
||||
|
||||
// Add payout account
|
||||
@@ -188,7 +201,8 @@ func (c *Creator) ListPayoutAccounts(ctx fiber.Ctx) ([]dto.PayoutAccount, error)
|
||||
// @Success 200 {string} string "Added"
|
||||
// @Bind form body
|
||||
func (c *Creator) AddPayoutAccount(ctx fiber.Ctx, form *dto.PayoutAccount) error {
|
||||
return services.Creator.AddPayoutAccount(ctx, form)
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Creator.AddPayoutAccount(ctx.Context(), uid, form)
|
||||
}
|
||||
|
||||
// Remove payout account
|
||||
@@ -203,7 +217,8 @@ func (c *Creator) AddPayoutAccount(ctx fiber.Ctx, form *dto.PayoutAccount) error
|
||||
// @Success 200 {string} string "Removed"
|
||||
// @Bind id query
|
||||
func (c *Creator) RemovePayoutAccount(ctx fiber.Ctx, id string) error {
|
||||
return services.Creator.RemovePayoutAccount(ctx, id)
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Creator.RemovePayoutAccount(ctx.Context(), uid, id)
|
||||
}
|
||||
|
||||
// Request withdrawal
|
||||
@@ -218,5 +233,6 @@ func (c *Creator) RemovePayoutAccount(ctx fiber.Ctx, id string) error {
|
||||
// @Success 200 {string} string "Withdrawal requested"
|
||||
// @Bind form body
|
||||
func (c *Creator) Withdraw(ctx fiber.Ctx, form *dto.WithdrawForm) error {
|
||||
return services.Creator.Withdraw(ctx, form)
|
||||
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser))
|
||||
return services.Creator.Withdraw(ctx.Context(), uid, form)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user