feat: 更新用户上下文处理,服务方法显式接受用户参数,简化上下文调用

This commit is contained in:
2025-12-30 23:01:35 +08:00
parent 54de243fa1
commit e6a8e3f321
7 changed files with 211 additions and 156 deletions

View File

@@ -5,10 +5,9 @@ import (
"quyun/v2/app/http/v1/dto" "quyun/v2/app/http/v1/dto"
"quyun/v2/app/services" "quyun/v2/app/services"
"quyun/v2/pkg/consts" "quyun/v2/database/models"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/spf13/cast"
) )
// @provider // @provider
@@ -25,13 +24,18 @@ type Common struct{}
// @Param file formData file true "File" // @Param file formData file true "File"
// @Param type formData string false "Type enum(image, video, audio)" // @Param type formData string false "Type enum(image, video, audio)"
// @Success 200 {object} dto.UploadResult // @Success 200 {object} dto.UploadResult
// @Bind user local key(__ctx_user)
// @Bind file file // @Bind file file
// @Bind typeArg body key(type) // @Bind typeArg body key(type)
func (c *Common) Upload(ctx fiber.Ctx, file *multipart.FileHeader, typeArg *string) (*dto.UploadResult, error) { func (c *Common) Upload(
ctx fiber.Ctx,
user *models.User,
file *multipart.FileHeader,
typeArg *string,
) (*dto.UploadResult, error) {
val := "" val := ""
if typeArg != nil { if typeArg != nil {
val = *typeArg val = *typeArg
} }
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) return services.Common.Upload(ctx.Context(), user.ID, file, val)
return services.Common.Upload(ctx.Context(), uid, file, val)
} }

View File

@@ -3,10 +3,9 @@ package v1
import ( import (
"quyun/v2/app/http/v1/dto" "quyun/v2/app/http/v1/dto"
"quyun/v2/app/services" "quyun/v2/app/services"
"quyun/v2/pkg/consts" "quyun/v2/database/models"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/spf13/cast"
) )
// @provider // @provider
@@ -22,10 +21,10 @@ type Creator struct{}
// @Produce json // @Produce json
// @Param form body dto.ApplyForm true "Apply form" // @Param form body dto.ApplyForm true "Apply form"
// @Success 200 {string} string "Application submitted" // @Success 200 {string} string "Application submitted"
// @Bind user local key(__ctx_user)
// @Bind form body // @Bind form body
func (c *Creator) Apply(ctx fiber.Ctx, form *dto.ApplyForm) error { func (c *Creator) Apply(ctx fiber.Ctx, user *models.User, form *dto.ApplyForm) error {
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) return services.Creator.Apply(ctx.Context(), user.ID, form)
return services.Creator.Apply(ctx.Context(), uid, form)
} }
// Get creator dashboard stats // Get creator dashboard stats
@@ -37,9 +36,9 @@ func (c *Creator) Apply(ctx fiber.Ctx, form *dto.ApplyForm) error {
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Success 200 {object} dto.DashboardStats // @Success 200 {object} dto.DashboardStats
func (c *Creator) Dashboard(ctx fiber.Ctx) (*dto.DashboardStats, error) { // @Bind user local key(__ctx_user)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) func (c *Creator) Dashboard(ctx fiber.Ctx, user *models.User) (*dto.DashboardStats, error) {
return services.Creator.Dashboard(ctx.Context(), uid) return services.Creator.Dashboard(ctx.Context(), user.ID)
} }
// List creator contents // List creator contents
@@ -54,10 +53,14 @@ func (c *Creator) Dashboard(ctx fiber.Ctx) (*dto.DashboardStats, error) {
// @Param genre query string false "Genre" // @Param genre query string false "Genre"
// @Param keyword query string false "Keyword" // @Param keyword query string false "Keyword"
// @Success 200 {array} dto.ContentItem // @Success 200 {array} dto.ContentItem
// @Bind user local key(__ctx_user)
// @Bind filter query // @Bind filter query
func (c *Creator) ListContents(ctx fiber.Ctx, filter *dto.CreatorContentListFilter) ([]dto.ContentItem, error) { func (c *Creator) ListContents(
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) ctx fiber.Ctx,
return services.Creator.ListContents(ctx.Context(), uid, filter) user *models.User,
filter *dto.CreatorContentListFilter,
) ([]dto.ContentItem, error) {
return services.Creator.ListContents(ctx.Context(), user.ID, filter)
} }
// Create/Publish content // Create/Publish content
@@ -70,10 +73,10 @@ func (c *Creator) ListContents(ctx fiber.Ctx, filter *dto.CreatorContentListFilt
// @Produce json // @Produce json
// @Param form body dto.ContentCreateForm true "Content form" // @Param form body dto.ContentCreateForm true "Content form"
// @Success 200 {string} string "Created" // @Success 200 {string} string "Created"
// @Bind user local key(__ctx_user)
// @Bind form body // @Bind form body
func (c *Creator) CreateContent(ctx fiber.Ctx, form *dto.ContentCreateForm) error { func (c *Creator) CreateContent(ctx fiber.Ctx, user *models.User, form *dto.ContentCreateForm) error {
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) return services.Creator.CreateContent(ctx.Context(), user.ID, form)
return services.Creator.CreateContent(ctx.Context(), uid, form)
} }
// Update content // Update content
@@ -87,11 +90,11 @@ func (c *Creator) CreateContent(ctx fiber.Ctx, form *dto.ContentCreateForm) erro
// @Param id path string true "Content ID" // @Param id path string true "Content ID"
// @Param form body dto.ContentUpdateForm true "Update form" // @Param form body dto.ContentUpdateForm true "Update form"
// @Success 200 {string} string "Updated" // @Success 200 {string} string "Updated"
// @Bind user local key(__ctx_user)
// @Bind id path // @Bind id path
// @Bind form body // @Bind form body
func (c *Creator) UpdateContent(ctx fiber.Ctx, id string, form *dto.ContentUpdateForm) error { func (c *Creator) UpdateContent(ctx fiber.Ctx, user *models.User, id string, form *dto.ContentUpdateForm) error {
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) return services.Creator.UpdateContent(ctx.Context(), user.ID, id, form)
return services.Creator.UpdateContent(ctx.Context(), uid, id, form)
} }
// Delete content // Delete content
@@ -104,10 +107,10 @@ func (c *Creator) UpdateContent(ctx fiber.Ctx, id string, form *dto.ContentUpdat
// @Produce json // @Produce json
// @Param id path string true "Content ID" // @Param id path string true "Content ID"
// @Success 200 {string} string "Deleted" // @Success 200 {string} string "Deleted"
// @Bind user local key(__ctx_user)
// @Bind id path // @Bind id path
func (c *Creator) DeleteContent(ctx fiber.Ctx, id string) error { func (c *Creator) DeleteContent(ctx fiber.Ctx, user *models.User, id string) error {
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) return services.Creator.DeleteContent(ctx.Context(), user.ID, id)
return services.Creator.DeleteContent(ctx.Context(), uid, id)
} }
// List sales orders // List sales orders
@@ -121,10 +124,14 @@ func (c *Creator) DeleteContent(ctx fiber.Ctx, id string) error {
// @Param status query string false "Status" // @Param status query string false "Status"
// @Param keyword query string false "Keyword" // @Param keyword query string false "Keyword"
// @Success 200 {array} dto.Order // @Success 200 {array} dto.Order
// @Bind user local key(__ctx_user)
// @Bind filter query // @Bind filter query
func (c *Creator) ListOrders(ctx fiber.Ctx, filter *dto.CreatorOrderListFilter) ([]dto.Order, error) { func (c *Creator) ListOrders(
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) ctx fiber.Ctx,
return services.Creator.ListOrders(ctx.Context(), uid, filter) user *models.User,
filter *dto.CreatorOrderListFilter,
) ([]dto.Order, error) {
return services.Creator.ListOrders(ctx.Context(), user.ID, filter)
} }
// Process refund // Process refund
@@ -138,11 +145,11 @@ func (c *Creator) ListOrders(ctx fiber.Ctx, filter *dto.CreatorOrderListFilter)
// @Param id path string true "Order ID" // @Param id path string true "Order ID"
// @Param form body dto.RefundForm true "Refund form" // @Param form body dto.RefundForm true "Refund form"
// @Success 200 {string} string "Processed" // @Success 200 {string} string "Processed"
// @Bind user local key(__ctx_user)
// @Bind id path // @Bind id path
// @Bind form body // @Bind form body
func (c *Creator) Refund(ctx fiber.Ctx, id string, form *dto.RefundForm) error { func (c *Creator) Refund(ctx fiber.Ctx, user *models.User, id string, form *dto.RefundForm) error {
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) return services.Creator.ProcessRefund(ctx.Context(), user.ID, id, form)
return services.Creator.ProcessRefund(ctx.Context(), uid, id, form)
} }
// Get channel settings // Get channel settings
@@ -154,9 +161,9 @@ func (c *Creator) Refund(ctx fiber.Ctx, id string, form *dto.RefundForm) error {
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Success 200 {object} dto.Settings // @Success 200 {object} dto.Settings
func (c *Creator) GetSettings(ctx fiber.Ctx) (*dto.Settings, error) { // @Bind user local key(__ctx_user)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) func (c *Creator) GetSettings(ctx fiber.Ctx, user *models.User) (*dto.Settings, error) {
return services.Creator.GetSettings(ctx.Context(), uid) return services.Creator.GetSettings(ctx.Context(), user.ID)
} }
// Update channel settings // Update channel settings
@@ -169,10 +176,10 @@ func (c *Creator) GetSettings(ctx fiber.Ctx) (*dto.Settings, error) {
// @Produce json // @Produce json
// @Param form body dto.Settings true "Settings form" // @Param form body dto.Settings true "Settings form"
// @Success 200 {string} string "Updated" // @Success 200 {string} string "Updated"
// @Bind user local key(__ctx_user)
// @Bind form body // @Bind form body
func (c *Creator) UpdateSettings(ctx fiber.Ctx, form *dto.Settings) error { func (c *Creator) UpdateSettings(ctx fiber.Ctx, user *models.User, form *dto.Settings) error {
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) return services.Creator.UpdateSettings(ctx.Context(), user.ID, form)
return services.Creator.UpdateSettings(ctx.Context(), uid, form)
} }
// List payout accounts // List payout accounts
@@ -184,9 +191,9 @@ func (c *Creator) UpdateSettings(ctx fiber.Ctx, form *dto.Settings) error {
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Success 200 {array} dto.PayoutAccount // @Success 200 {array} dto.PayoutAccount
func (c *Creator) ListPayoutAccounts(ctx fiber.Ctx) ([]dto.PayoutAccount, error) { // @Bind user local key(__ctx_user)
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) func (c *Creator) ListPayoutAccounts(ctx fiber.Ctx, user *models.User) ([]dto.PayoutAccount, error) {
return services.Creator.ListPayoutAccounts(ctx.Context(), uid) return services.Creator.ListPayoutAccounts(ctx.Context(), user.ID)
} }
// Add payout account // Add payout account
@@ -199,10 +206,10 @@ func (c *Creator) ListPayoutAccounts(ctx fiber.Ctx) ([]dto.PayoutAccount, error)
// @Produce json // @Produce json
// @Param form body dto.PayoutAccount true "Account form" // @Param form body dto.PayoutAccount true "Account form"
// @Success 200 {string} string "Added" // @Success 200 {string} string "Added"
// @Bind user local key(__ctx_user)
// @Bind form body // @Bind form body
func (c *Creator) AddPayoutAccount(ctx fiber.Ctx, form *dto.PayoutAccount) error { func (c *Creator) AddPayoutAccount(ctx fiber.Ctx, user *models.User, form *dto.PayoutAccount) error {
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) return services.Creator.AddPayoutAccount(ctx.Context(), user.ID, form)
return services.Creator.AddPayoutAccount(ctx.Context(), uid, form)
} }
// Remove payout account // Remove payout account
@@ -215,10 +222,10 @@ func (c *Creator) AddPayoutAccount(ctx fiber.Ctx, form *dto.PayoutAccount) error
// @Produce json // @Produce json
// @Param id query string true "Account ID" // @Param id query string true "Account ID"
// @Success 200 {string} string "Removed" // @Success 200 {string} string "Removed"
// @Bind user local key(__ctx_user)
// @Bind id query // @Bind id query
func (c *Creator) RemovePayoutAccount(ctx fiber.Ctx, id string) error { func (c *Creator) RemovePayoutAccount(ctx fiber.Ctx, user *models.User, id string) error {
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) return services.Creator.RemovePayoutAccount(ctx.Context(), user.ID, id)
return services.Creator.RemovePayoutAccount(ctx.Context(), uid, id)
} }
// Request withdrawal // Request withdrawal
@@ -231,8 +238,8 @@ func (c *Creator) RemovePayoutAccount(ctx fiber.Ctx, id string) error {
// @Produce json // @Produce json
// @Param form body dto.WithdrawForm true "Withdraw form" // @Param form body dto.WithdrawForm true "Withdraw form"
// @Success 200 {string} string "Withdrawal requested" // @Success 200 {string} string "Withdrawal requested"
// @Bind user local key(__ctx_user)
// @Bind form body // @Bind form body
func (c *Creator) Withdraw(ctx fiber.Ctx, form *dto.WithdrawForm) error { func (c *Creator) Withdraw(ctx fiber.Ctx, user *models.User, form *dto.WithdrawForm) error {
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) return services.Creator.Withdraw(ctx.Context(), user.ID, form)
return services.Creator.Withdraw(ctx.Context(), uid, form)
} }

View File

@@ -51,8 +51,9 @@ func (r *Routes) Name() string {
func (r *Routes) Register(router fiber.Router) { func (r *Routes) Register(router fiber.Router) {
// Register routes for controller: Common // Register routes for controller: Common
r.log.Debugf("Registering route: Post /v1/upload -> common.Upload") r.log.Debugf("Registering route: Post /v1/upload -> common.Upload")
router.Post("/v1/upload"[len(r.Path()):], DataFunc2( router.Post("/v1/upload"[len(r.Path()):], DataFunc3(
r.common.Upload, r.common.Upload,
Local[*models.User]("__ctx_user"),
File[multipart.FileHeader]("file"), File[multipart.FileHeader]("file"),
Body[string]("type"), Body[string]("type"),
)) ))
@@ -90,72 +91,86 @@ func (r *Routes) Register(router fiber.Router) {
)) ))
// Register routes for controller: Creator // Register routes for controller: Creator
r.log.Debugf("Registering route: Delete /v1/creator/contents/:id -> creator.DeleteContent") r.log.Debugf("Registering route: Delete /v1/creator/contents/:id -> creator.DeleteContent")
router.Delete("/v1/creator/contents/:id"[len(r.Path()):], Func1( router.Delete("/v1/creator/contents/:id"[len(r.Path()):], Func2(
r.creator.DeleteContent, r.creator.DeleteContent,
Local[*models.User]("__ctx_user"),
PathParam[string]("id"), PathParam[string]("id"),
)) ))
r.log.Debugf("Registering route: Delete /v1/creator/payout-accounts -> creator.RemovePayoutAccount") r.log.Debugf("Registering route: Delete /v1/creator/payout-accounts -> creator.RemovePayoutAccount")
router.Delete("/v1/creator/payout-accounts"[len(r.Path()):], Func1( router.Delete("/v1/creator/payout-accounts"[len(r.Path()):], Func2(
r.creator.RemovePayoutAccount, r.creator.RemovePayoutAccount,
Local[*models.User]("__ctx_user"),
QueryParam[string]("id"), QueryParam[string]("id"),
)) ))
r.log.Debugf("Registering route: Get /v1/creator/contents -> creator.ListContents") r.log.Debugf("Registering route: Get /v1/creator/contents -> creator.ListContents")
router.Get("/v1/creator/contents"[len(r.Path()):], DataFunc1( router.Get("/v1/creator/contents"[len(r.Path()):], DataFunc2(
r.creator.ListContents, r.creator.ListContents,
Local[*models.User]("__ctx_user"),
Query[dto.CreatorContentListFilter]("filter"), Query[dto.CreatorContentListFilter]("filter"),
)) ))
r.log.Debugf("Registering route: Get /v1/creator/dashboard -> creator.Dashboard") r.log.Debugf("Registering route: Get /v1/creator/dashboard -> creator.Dashboard")
router.Get("/v1/creator/dashboard"[len(r.Path()):], DataFunc0( router.Get("/v1/creator/dashboard"[len(r.Path()):], DataFunc1(
r.creator.Dashboard, r.creator.Dashboard,
Local[*models.User]("__ctx_user"),
)) ))
r.log.Debugf("Registering route: Get /v1/creator/orders -> creator.ListOrders") r.log.Debugf("Registering route: Get /v1/creator/orders -> creator.ListOrders")
router.Get("/v1/creator/orders"[len(r.Path()):], DataFunc1( router.Get("/v1/creator/orders"[len(r.Path()):], DataFunc2(
r.creator.ListOrders, r.creator.ListOrders,
Local[*models.User]("__ctx_user"),
Query[dto.CreatorOrderListFilter]("filter"), Query[dto.CreatorOrderListFilter]("filter"),
)) ))
r.log.Debugf("Registering route: Get /v1/creator/payout-accounts -> creator.ListPayoutAccounts") r.log.Debugf("Registering route: Get /v1/creator/payout-accounts -> creator.ListPayoutAccounts")
router.Get("/v1/creator/payout-accounts"[len(r.Path()):], DataFunc0( router.Get("/v1/creator/payout-accounts"[len(r.Path()):], DataFunc1(
r.creator.ListPayoutAccounts, r.creator.ListPayoutAccounts,
Local[*models.User]("__ctx_user"),
)) ))
r.log.Debugf("Registering route: Get /v1/creator/settings -> creator.GetSettings") r.log.Debugf("Registering route: Get /v1/creator/settings -> creator.GetSettings")
router.Get("/v1/creator/settings"[len(r.Path()):], DataFunc0( router.Get("/v1/creator/settings"[len(r.Path()):], DataFunc1(
r.creator.GetSettings, r.creator.GetSettings,
Local[*models.User]("__ctx_user"),
)) ))
r.log.Debugf("Registering route: Post /v1/creator/apply -> creator.Apply") r.log.Debugf("Registering route: Post /v1/creator/apply -> creator.Apply")
router.Post("/v1/creator/apply"[len(r.Path()):], Func1( router.Post("/v1/creator/apply"[len(r.Path()):], Func2(
r.creator.Apply, r.creator.Apply,
Local[*models.User]("__ctx_user"),
Body[dto.ApplyForm]("form"), Body[dto.ApplyForm]("form"),
)) ))
r.log.Debugf("Registering route: Post /v1/creator/contents -> creator.CreateContent") r.log.Debugf("Registering route: Post /v1/creator/contents -> creator.CreateContent")
router.Post("/v1/creator/contents"[len(r.Path()):], Func1( router.Post("/v1/creator/contents"[len(r.Path()):], Func2(
r.creator.CreateContent, r.creator.CreateContent,
Local[*models.User]("__ctx_user"),
Body[dto.ContentCreateForm]("form"), Body[dto.ContentCreateForm]("form"),
)) ))
r.log.Debugf("Registering route: Post /v1/creator/orders/:id/refund -> creator.Refund") r.log.Debugf("Registering route: Post /v1/creator/orders/:id/refund -> creator.Refund")
router.Post("/v1/creator/orders/:id/refund"[len(r.Path()):], Func2( router.Post("/v1/creator/orders/:id/refund"[len(r.Path()):], Func3(
r.creator.Refund, r.creator.Refund,
Local[*models.User]("__ctx_user"),
PathParam[string]("id"), PathParam[string]("id"),
Body[dto.RefundForm]("form"), Body[dto.RefundForm]("form"),
)) ))
r.log.Debugf("Registering route: Post /v1/creator/payout-accounts -> creator.AddPayoutAccount") r.log.Debugf("Registering route: Post /v1/creator/payout-accounts -> creator.AddPayoutAccount")
router.Post("/v1/creator/payout-accounts"[len(r.Path()):], Func1( router.Post("/v1/creator/payout-accounts"[len(r.Path()):], Func2(
r.creator.AddPayoutAccount, r.creator.AddPayoutAccount,
Local[*models.User]("__ctx_user"),
Body[dto.PayoutAccount]("form"), Body[dto.PayoutAccount]("form"),
)) ))
r.log.Debugf("Registering route: Post /v1/creator/withdraw -> creator.Withdraw") r.log.Debugf("Registering route: Post /v1/creator/withdraw -> creator.Withdraw")
router.Post("/v1/creator/withdraw"[len(r.Path()):], Func1( router.Post("/v1/creator/withdraw"[len(r.Path()):], Func2(
r.creator.Withdraw, r.creator.Withdraw,
Local[*models.User]("__ctx_user"),
Body[dto.WithdrawForm]("form"), Body[dto.WithdrawForm]("form"),
)) ))
r.log.Debugf("Registering route: Put /v1/creator/contents/:id -> creator.UpdateContent") r.log.Debugf("Registering route: Put /v1/creator/contents/:id -> creator.UpdateContent")
router.Put("/v1/creator/contents/:id"[len(r.Path()):], Func2( router.Put("/v1/creator/contents/:id"[len(r.Path()):], Func3(
r.creator.UpdateContent, r.creator.UpdateContent,
Local[*models.User]("__ctx_user"),
PathParam[string]("id"), PathParam[string]("id"),
Body[dto.ContentUpdateForm]("form"), Body[dto.ContentUpdateForm]("form"),
)) ))
r.log.Debugf("Registering route: Put /v1/creator/settings -> creator.UpdateSettings") r.log.Debugf("Registering route: Put /v1/creator/settings -> creator.UpdateSettings")
router.Put("/v1/creator/settings"[len(r.Path()):], Func1( router.Put("/v1/creator/settings"[len(r.Path()):], Func2(
r.creator.UpdateSettings, r.creator.UpdateSettings,
Local[*models.User]("__ctx_user"),
Body[dto.Settings]("form"), Body[dto.Settings]("form"),
)) ))
// Register routes for controller: Storage // Register routes for controller: Storage
@@ -175,18 +190,21 @@ func (r *Routes) Register(router fiber.Router) {
)) ))
// Register routes for controller: Tenant // Register routes for controller: Tenant
r.log.Debugf("Registering route: Delete /v1/tenants/:id/follow -> tenant.Unfollow") r.log.Debugf("Registering route: Delete /v1/tenants/:id/follow -> tenant.Unfollow")
router.Delete("/v1/tenants/:id/follow"[len(r.Path()):], Func1( router.Delete("/v1/tenants/:id/follow"[len(r.Path()):], Func2(
r.tenant.Unfollow, r.tenant.Unfollow,
Local[*models.User]("__ctx_user"),
PathParam[string]("id"), PathParam[string]("id"),
)) ))
r.log.Debugf("Registering route: Get /v1/tenants/:id -> tenant.Get") r.log.Debugf("Registering route: Get /v1/tenants/:id -> tenant.Get")
router.Get("/v1/tenants/:id"[len(r.Path()):], DataFunc1( router.Get("/v1/tenants/:id"[len(r.Path()):], DataFunc2(
r.tenant.Get, r.tenant.Get,
Local[*models.User]("__ctx_user"),
PathParam[string]("id"), PathParam[string]("id"),
)) ))
r.log.Debugf("Registering route: Post /v1/tenants/:id/follow -> tenant.Follow") r.log.Debugf("Registering route: Post /v1/tenants/:id/follow -> tenant.Follow")
router.Post("/v1/tenants/:id/follow"[len(r.Path()):], Func1( router.Post("/v1/tenants/:id/follow"[len(r.Path()):], Func2(
r.tenant.Follow, r.tenant.Follow,
Local[*models.User]("__ctx_user"),
PathParam[string]("id"), PathParam[string]("id"),
)) ))
// Register routes for controller: Transaction // Register routes for controller: Transaction
@@ -196,13 +214,15 @@ func (r *Routes) Register(router fiber.Router) {
PathParam[string]("id"), PathParam[string]("id"),
)) ))
r.log.Debugf("Registering route: Post /v1/orders -> transaction.Create") r.log.Debugf("Registering route: Post /v1/orders -> transaction.Create")
router.Post("/v1/orders"[len(r.Path()):], DataFunc1( router.Post("/v1/orders"[len(r.Path()):], DataFunc2(
r.transaction.Create, r.transaction.Create,
Local[*models.User]("__ctx_user"),
Body[dto.OrderCreateForm]("form"), Body[dto.OrderCreateForm]("form"),
)) ))
r.log.Debugf("Registering route: Post /v1/orders/:id/pay -> transaction.Pay") r.log.Debugf("Registering route: Post /v1/orders/:id/pay -> transaction.Pay")
router.Post("/v1/orders/:id/pay"[len(r.Path()):], DataFunc2( router.Post("/v1/orders/:id/pay"[len(r.Path()):], DataFunc3(
r.transaction.Pay, r.transaction.Pay,
Local[*models.User]("__ctx_user"),
PathParam[string]("id"), PathParam[string]("id"),
Body[dto.OrderPayForm]("form"), Body[dto.OrderPayForm]("form"),
)) ))
@@ -213,13 +233,15 @@ func (r *Routes) Register(router fiber.Router) {
)) ))
// Register routes for controller: User // Register routes for controller: User
r.log.Debugf("Registering route: Delete /v1/me/favorites/:contentId -> user.RemoveFavorite") r.log.Debugf("Registering route: Delete /v1/me/favorites/:contentId -> user.RemoveFavorite")
router.Delete("/v1/me/favorites/:contentId"[len(r.Path()):], Func1( router.Delete("/v1/me/favorites/:contentId"[len(r.Path()):], Func2(
r.user.RemoveFavorite, r.user.RemoveFavorite,
Local[*models.User]("__ctx_user"),
PathParam[string]("contentId"), PathParam[string]("contentId"),
)) ))
r.log.Debugf("Registering route: Delete /v1/me/likes/:contentId -> user.RemoveLike") r.log.Debugf("Registering route: Delete /v1/me/likes/:contentId -> user.RemoveLike")
router.Delete("/v1/me/likes/:contentId"[len(r.Path()):], Func1( router.Delete("/v1/me/likes/:contentId"[len(r.Path()):], Func2(
r.user.RemoveLike, r.user.RemoveLike,
Local[*models.User]("__ctx_user"),
PathParam[string]("contentId"), PathParam[string]("contentId"),
)) ))
r.log.Debugf("Registering route: Get /v1/me -> user.Me") r.log.Debugf("Registering route: Get /v1/me -> user.Me")
@@ -228,69 +250,83 @@ func (r *Routes) Register(router fiber.Router) {
Local[*models.User]("__ctx_user"), Local[*models.User]("__ctx_user"),
)) ))
r.log.Debugf("Registering route: Get /v1/me/coupons -> user.MyCoupons") r.log.Debugf("Registering route: Get /v1/me/coupons -> user.MyCoupons")
router.Get("/v1/me/coupons"[len(r.Path()):], DataFunc1( router.Get("/v1/me/coupons"[len(r.Path()):], DataFunc2(
r.user.MyCoupons, r.user.MyCoupons,
Local[*models.User]("__ctx_user"),
QueryParam[string]("status"), QueryParam[string]("status"),
)) ))
r.log.Debugf("Registering route: Get /v1/me/favorites -> user.Favorites") r.log.Debugf("Registering route: Get /v1/me/favorites -> user.Favorites")
router.Get("/v1/me/favorites"[len(r.Path()):], DataFunc0( router.Get("/v1/me/favorites"[len(r.Path()):], DataFunc1(
r.user.Favorites, r.user.Favorites,
Local[*models.User]("__ctx_user"),
)) ))
r.log.Debugf("Registering route: Get /v1/me/following -> user.Following") r.log.Debugf("Registering route: Get /v1/me/following -> user.Following")
router.Get("/v1/me/following"[len(r.Path()):], DataFunc0( router.Get("/v1/me/following"[len(r.Path()):], DataFunc1(
r.user.Following, r.user.Following,
Local[*models.User]("__ctx_user"),
)) ))
r.log.Debugf("Registering route: Get /v1/me/library -> user.Library") r.log.Debugf("Registering route: Get /v1/me/library -> user.Library")
router.Get("/v1/me/library"[len(r.Path()):], DataFunc0( router.Get("/v1/me/library"[len(r.Path()):], DataFunc1(
r.user.Library, r.user.Library,
Local[*models.User]("__ctx_user"),
)) ))
r.log.Debugf("Registering route: Get /v1/me/likes -> user.Likes") r.log.Debugf("Registering route: Get /v1/me/likes -> user.Likes")
router.Get("/v1/me/likes"[len(r.Path()):], DataFunc0( router.Get("/v1/me/likes"[len(r.Path()):], DataFunc1(
r.user.Likes, r.user.Likes,
Local[*models.User]("__ctx_user"),
)) ))
r.log.Debugf("Registering route: Get /v1/me/notifications -> user.Notifications") r.log.Debugf("Registering route: Get /v1/me/notifications -> user.Notifications")
router.Get("/v1/me/notifications"[len(r.Path()):], DataFunc2( router.Get("/v1/me/notifications"[len(r.Path()):], DataFunc3(
r.user.Notifications, r.user.Notifications,
Local[*models.User]("__ctx_user"),
QueryParam[string]("type"), QueryParam[string]("type"),
QueryParam[int]("page"), QueryParam[int]("page"),
)) ))
r.log.Debugf("Registering route: Get /v1/me/orders -> user.ListOrders") r.log.Debugf("Registering route: Get /v1/me/orders -> user.ListOrders")
router.Get("/v1/me/orders"[len(r.Path()):], DataFunc1( router.Get("/v1/me/orders"[len(r.Path()):], DataFunc2(
r.user.ListOrders, r.user.ListOrders,
Local[*models.User]("__ctx_user"),
QueryParam[string]("status"), QueryParam[string]("status"),
)) ))
r.log.Debugf("Registering route: Get /v1/me/orders/:id -> user.GetOrder") r.log.Debugf("Registering route: Get /v1/me/orders/:id -> user.GetOrder")
router.Get("/v1/me/orders/:id"[len(r.Path()):], DataFunc1( router.Get("/v1/me/orders/:id"[len(r.Path()):], DataFunc2(
r.user.GetOrder, r.user.GetOrder,
Local[*models.User]("__ctx_user"),
PathParam[string]("id"), PathParam[string]("id"),
)) ))
r.log.Debugf("Registering route: Get /v1/me/wallet -> user.Wallet") r.log.Debugf("Registering route: Get /v1/me/wallet -> user.Wallet")
router.Get("/v1/me/wallet"[len(r.Path()):], DataFunc0( router.Get("/v1/me/wallet"[len(r.Path()):], DataFunc1(
r.user.Wallet, r.user.Wallet,
Local[*models.User]("__ctx_user"),
)) ))
r.log.Debugf("Registering route: Post /v1/me/favorites -> user.AddFavorite") r.log.Debugf("Registering route: Post /v1/me/favorites -> user.AddFavorite")
router.Post("/v1/me/favorites"[len(r.Path()):], Func1( router.Post("/v1/me/favorites"[len(r.Path()):], Func2(
r.user.AddFavorite, r.user.AddFavorite,
Local[*models.User]("__ctx_user"),
QueryParam[string]("contentId"), QueryParam[string]("contentId"),
)) ))
r.log.Debugf("Registering route: Post /v1/me/likes -> user.AddLike") r.log.Debugf("Registering route: Post /v1/me/likes -> user.AddLike")
router.Post("/v1/me/likes"[len(r.Path()):], Func1( router.Post("/v1/me/likes"[len(r.Path()):], Func2(
r.user.AddLike, r.user.AddLike,
Local[*models.User]("__ctx_user"),
QueryParam[string]("contentId"), QueryParam[string]("contentId"),
)) ))
r.log.Debugf("Registering route: Post /v1/me/realname -> user.RealName") r.log.Debugf("Registering route: Post /v1/me/realname -> user.RealName")
router.Post("/v1/me/realname"[len(r.Path()):], Func1( router.Post("/v1/me/realname"[len(r.Path()):], Func2(
r.user.RealName, r.user.RealName,
Local[*models.User]("__ctx_user"),
Body[dto.RealNameForm]("form"), Body[dto.RealNameForm]("form"),
)) ))
r.log.Debugf("Registering route: Post /v1/me/wallet/recharge -> user.Recharge") r.log.Debugf("Registering route: Post /v1/me/wallet/recharge -> user.Recharge")
router.Post("/v1/me/wallet/recharge"[len(r.Path()):], DataFunc1( router.Post("/v1/me/wallet/recharge"[len(r.Path()):], DataFunc2(
r.user.Recharge, r.user.Recharge,
Local[*models.User]("__ctx_user"),
Body[dto.RechargeForm]("form"), Body[dto.RechargeForm]("form"),
)) ))
r.log.Debugf("Registering route: Put /v1/me -> user.Update") r.log.Debugf("Registering route: Put /v1/me -> user.Update")
router.Put("/v1/me"[len(r.Path()):], Func1( router.Put("/v1/me"[len(r.Path()):], Func2(
r.user.Update, r.user.Update,
Local[*models.User]("__ctx_user"),
Body[dto.UserUpdate]("form"), Body[dto.UserUpdate]("form"),
)) ))

View File

@@ -3,10 +3,9 @@ package v1
import ( import (
"quyun/v2/app/http/v1/dto" "quyun/v2/app/http/v1/dto"
"quyun/v2/app/services" "quyun/v2/app/services"
"quyun/v2/pkg/consts" "quyun/v2/database/models"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/spf13/cast"
) )
// @provider // @provider
@@ -22,9 +21,13 @@ type Tenant struct{}
// @Produce json // @Produce json
// @Param id path string true "Tenant ID" // @Param id path string true "Tenant ID"
// @Success 200 {object} dto.TenantProfile // @Success 200 {object} dto.TenantProfile
// @Bind user local key(__ctx_user)
// @Bind id path // @Bind id path
func (t *Tenant) Get(ctx fiber.Ctx, id string) (*dto.TenantProfile, error) { func (t *Tenant) Get(ctx fiber.Ctx, user *models.User, id string) (*dto.TenantProfile, error) {
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) uid := int64(0)
if user != nil {
uid = user.ID
}
return services.Tenant.GetPublicProfile(ctx.Context(), uid, id) return services.Tenant.GetPublicProfile(ctx.Context(), uid, id)
} }
@@ -38,10 +41,10 @@ func (t *Tenant) Get(ctx fiber.Ctx, id string) (*dto.TenantProfile, error) {
// @Produce json // @Produce json
// @Param id path string true "Tenant ID" // @Param id path string true "Tenant ID"
// @Success 200 {string} string "Followed" // @Success 200 {string} string "Followed"
// @Bind user local key(__ctx_user)
// @Bind id path // @Bind id path
func (t *Tenant) Follow(ctx fiber.Ctx, id string) error { func (t *Tenant) Follow(ctx fiber.Ctx, user *models.User, id string) error {
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) return services.Tenant.Follow(ctx.Context(), user.ID, id)
return services.Tenant.Follow(ctx.Context(), uid, id)
} }
// Unfollow a tenant // Unfollow a tenant
@@ -54,8 +57,8 @@ func (t *Tenant) Follow(ctx fiber.Ctx, id string) error {
// @Produce json // @Produce json
// @Param id path string true "Tenant ID" // @Param id path string true "Tenant ID"
// @Success 200 {string} string "Unfollowed" // @Success 200 {string} string "Unfollowed"
// @Bind user local key(__ctx_user)
// @Bind id path // @Bind id path
func (t *Tenant) Unfollow(ctx fiber.Ctx, id string) error { func (t *Tenant) Unfollow(ctx fiber.Ctx, user *models.User, id string) error {
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) return services.Tenant.Unfollow(ctx.Context(), user.ID, id)
return services.Tenant.Unfollow(ctx.Context(), uid, id)
} }

View File

@@ -3,10 +3,9 @@ package v1
import ( import (
"quyun/v2/app/http/v1/dto" "quyun/v2/app/http/v1/dto"
"quyun/v2/app/services" "quyun/v2/app/services"
"quyun/v2/pkg/consts" "quyun/v2/database/models"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/spf13/cast"
) )
// @provider // @provider
@@ -22,10 +21,14 @@ type Transaction struct{}
// @Produce json // @Produce json
// @Param form body dto.OrderCreateForm true "Create form" // @Param form body dto.OrderCreateForm true "Create form"
// @Success 200 {object} dto.OrderCreateResponse // @Success 200 {object} dto.OrderCreateResponse
// @Bind user local key(__ctx_user)
// @Bind form body // @Bind form body
func (t *Transaction) Create(ctx fiber.Ctx, form *dto.OrderCreateForm) (*dto.OrderCreateResponse, error) { func (t *Transaction) Create(
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) ctx fiber.Ctx,
return services.Order.Create(ctx.Context(), uid, form) user *models.User,
form *dto.OrderCreateForm,
) (*dto.OrderCreateResponse, error) {
return services.Order.Create(ctx.Context(), user.ID, form)
} }
// Pay for order // Pay for order
@@ -39,11 +42,16 @@ func (t *Transaction) Create(ctx fiber.Ctx, form *dto.OrderCreateForm) (*dto.Ord
// @Param id path string true "Order ID" // @Param id path string true "Order ID"
// @Param form body dto.OrderPayForm true "Pay form" // @Param form body dto.OrderPayForm true "Pay form"
// @Success 200 {object} dto.OrderPayResponse // @Success 200 {object} dto.OrderPayResponse
// @Bind user local key(__ctx_user)
// @Bind id path // @Bind id path
// @Bind form body // @Bind form body
func (t *Transaction) Pay(ctx fiber.Ctx, id string, form *dto.OrderPayForm) (*dto.OrderPayResponse, error) { func (t *Transaction) Pay(
uid := cast.ToInt64(ctx.Locals(consts.CtxKeyUser)) ctx fiber.Ctx,
return services.Order.Pay(ctx.Context(), uid, id, form) user *models.User,
id string,
form *dto.OrderPayForm,
) (*dto.OrderPayResponse, error) {
return services.Order.Pay(ctx.Context(), user.ID, id, form)
} }
// Check order payment status // Check order payment status

View File

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

View File

@@ -3,7 +3,6 @@ package middlewares
import ( import (
"quyun/v2/app/errorx" "quyun/v2/app/errorx"
"quyun/v2/app/services" "quyun/v2/app/services"
"quyun/v2/pkg/consts"
"quyun/v2/providers/jwt" "quyun/v2/providers/jwt"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
@@ -43,13 +42,13 @@ func (m *Middlewares) Auth(ctx fiber.Ctx) error {
} }
// Set Context // Set Context
ctx.Locals(consts.CtxKeyUser, user) ctx.Locals("__ctx_user", user)
if claims.TenantID > 0 { if claims.TenantID > 0 {
tenant, err := services.Tenant.GetModelByID(ctx, claims.TenantID) tenant, err := services.Tenant.GetModelByID(ctx, claims.TenantID)
if err != nil { if err != nil {
return errorx.ErrUnauthorized.WithCause(err).WithMsg("TenantNotFound") return errorx.ErrUnauthorized.WithCause(err).WithMsg("TenantNotFound")
} }
ctx.Locals(consts.CtxKeyTenant, tenant) ctx.Locals("__ctx_tenant", tenant)
} }
return ctx.Next() return ctx.Next()