Refactor super module: Move HTTP handlers to new super/v1 package

- Deleted old super.go file and moved all related HTTP handlers to new package structure under backend/app/http/super/v1.
- Updated service imports in super.go and super_test.go to reflect new DTO paths.
- Created new auth, contents, orders, tenants, and users handlers with corresponding routes and methods.
- Added new DTO definitions for authentication and content management in the super/v1/dto directory.
- Implemented new migration for content_access table and created model for it.
This commit is contained in:
2025-12-29 17:17:07 +08:00
parent bdf20fb0c6
commit 33e1921e17
18 changed files with 665 additions and 443 deletions

View File

@@ -1,224 +0,0 @@
package dto
import (
"quyun/v2/app/requests"
"quyun/v2/pkg/consts"
)
// Filters
type UserListFilter struct {
requests.Pagination
Username *string `query:"username"`
}
type TenantListFilter struct {
requests.Pagination
Name *string `query:"name"`
}
type SuperContentListFilter struct {
requests.Pagination
// Add filters if needed, currently list signature is just pagination in super.go service
}
type SuperOrderListFilter struct {
requests.Pagination
// Add filters if needed
}
// SuperUserLite 用于平台用户列表的轻量级用户信息
type SuperUserLite struct {
ID int64 `json:"id"`
Username string `json:"username"`
Roles []consts.Role `json:"roles"`
Status consts.UserStatus `json:"status"`
StatusDescription string `json:"status_description"`
VerifiedAt string `json:"verified_at"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type UserItem struct {
SuperUserLite
Balance int64 `json:"balance"`
BalanceFrozen int64 `json:"balance_frozen"`
OwnedTenantCount int64 `json:"owned_tenant_count"`
JoinedTenantCount int64 `json:"joined_tenant_count"`
}
type UserStatistics struct {
Status consts.UserStatus `json:"status"`
StatusDescription string `json:"status_description"`
Count int64 `json:"count"`
}
type UserStatusUpdateForm struct {
Status consts.UserStatus `json:"status" validate:"required"`
}
type UserRolesUpdateForm struct {
Roles []consts.Role `json:"roles" validate:"required,min=1"`
}
type UserTenantItem struct {
TenantID int64 `json:"tenant_id"`
TenantStatus consts.TenantStatus `json:"tenant_status"`
TenantStatusDescription string `json:"tenant_status_description"`
Name string `json:"name"`
Code string `json:"code"`
Owner *TenantOwnerUserLite `json:"owner"`
Role []consts.TenantUserRole `json:"role"`
MemberStatus consts.UserStatus `json:"member_status"`
MemberStatusDescription string `json:"member_status_description"`
JoinedAt string `json:"joined_at"`
ExpiredAt string `json:"expired_at"`
}
// Tenant Related
type TenantCreateForm struct {
Name string `json:"name" validate:"required,max=128"`
Code string `json:"code" validate:"required,max=64"`
AdminUserID int64 `json:"admin_user_id" validate:"required"`
Duration int `json:"duration" validate:"required,oneof=7 30 90 180 365"`
}
type TenantItem struct {
ID int64 `json:"id"`
UUID string `json:"uuid"`
Code string `json:"code"`
Name string `json:"name"`
Status consts.TenantStatus `json:"status"`
StatusDescription string `json:"status_description"`
Config []int `json:"config"` // Replace with actual config struct if needed
Owner *TenantOwnerUserLite `json:"owner"`
AdminUsers []*TenantAdminUserLite `json:"admin_users"`
UserCount int64 `json:"user_count"`
IncomeAmountPaidSum int64 `json:"income_amount_paid_sum"`
ExpiredAt string `json:"expired_at"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
UserID int64 `json:"user_id"`
Users []*SuperUserLite `json:"users"`
}
type TenantOwnerUserLite struct {
ID int64 `json:"id"`
Username string `json:"username"`
}
type TenantAdminUserLite struct {
ID int64 `json:"id"`
Username string `json:"username"`
}
type TenantExpireUpdateForm struct {
Duration int `json:"duration" validate:"required,oneof=7 30 90 180 365"`
}
type TenantStatusUpdateForm struct {
Status consts.TenantStatus `json:"status" validate:"required"`
}
type SuperTenantContentItem struct {
Content *ContentItem `json:"content"`
StatusDescription string `json:"status_description"`
VisibilityDescription string `json:"visibility_description"`
Tenant *SuperContentTenantLite `json:"tenant"`
Owner *SuperUserLite `json:"owner"`
Price *ContentPrice `json:"price"` // Reuse or define specific price struct
}
type SuperContentTenantLite struct {
ID int64 `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
}
type SuperTenantContentStatusUpdateForm struct {
Status consts.ContentStatus `json:"status" validate:"required,oneof=unpublished blocked"`
}
type SuperTenantUserItem struct {
User *SuperUserLite `json:"user"`
TenantUser *TenantUser `json:"tenant_user"`
}
type TenantUser struct {
ID int64 `json:"id"`
TenantID int64 `json:"tenant_id"`
UserID int64 `json:"user_id"`
Role []consts.TenantUserRole `json:"role"`
Status consts.UserStatus `json:"status"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// Order Related
type SuperOrderItem struct {
ID int64 `json:"id"`
Type consts.OrderType `json:"type"`
Status consts.OrderStatus `json:"status"`
StatusDescription string `json:"status_description"`
Currency consts.Currency `json:"currency"`
AmountOriginal int64 `json:"amount_original"`
AmountDiscount int64 `json:"amount_discount"`
AmountPaid int64 `json:"amount_paid"`
Tenant *OrderTenantLite `json:"tenant"`
Buyer *OrderBuyerLite `json:"buyer"`
PaidAt string `json:"paid_at"`
RefundedAt string `json:"refunded_at"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type OrderTenantLite struct {
ID int64 `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
}
type OrderBuyerLite struct {
ID int64 `json:"id"`
Username string `json:"username"`
}
type OrderStatisticsResponse struct {
TotalCount int64 `json:"total_count"`
TotalAmountPaidSum int64 `json:"total_amount_paid_sum"`
ByStatus []OrderStatisticsRow `json:"by_status"`
}
type OrderStatisticsRow struct {
Status consts.OrderStatus `json:"status"`
StatusDescription string `json:"status_description"`
Count int64 `json:"count"`
AmountPaidSum int64 `json:"amount_paid_sum"`
}
type SuperOrderDetail struct {
Order *SuperOrderItem `json:"order"` // Using SuperOrderItem as base, extend if needed
Tenant *OrderTenantLite `json:"tenant"`
Buyer *OrderBuyerLite `json:"buyer"`
}
type SuperOrderRefundForm struct {
Force bool `json:"force"`
Reason string `json:"reason"`
IdempotencyKey string `json:"idempotency_key"`
}
// AdminContentItem for super admin view
type AdminContentItem struct {
Content *ContentItem `json:"content"`
Owner *AdminContentOwnerLite `json:"owner"`
Price *ContentPrice `json:"price"`
StatusDescription string `json:"status_description"`
VisibilityDescription string `json:"visibility_description"`
}
type AdminContentOwnerLite struct {
ID int64 `json:"id"`
Username string `json:"username"`
Roles []consts.Role `json:"roles"`
Status consts.UserStatus `json:"status"`
}

View File

@@ -44,7 +44,6 @@ func Provide(opts ...opt.Option) error {
content *Content,
creator *Creator,
middlewares *middlewares.Middlewares,
super *Super,
tenant *Tenant,
transaction *Transaction,
user *User,
@@ -55,7 +54,6 @@ func Provide(opts ...opt.Option) error {
content: content,
creator: creator,
middlewares: middlewares,
super: super,
tenant: tenant,
transaction: transaction,
user: user,
@@ -68,13 +66,6 @@ func Provide(opts ...opt.Option) error {
}, atom.GroupRoutes); err != nil {
return err
}
if err := container.Container.Provide(func() (*Super, error) {
obj := &Super{}
return obj, nil
}); err != nil {
return err
}
if err := container.Container.Provide(func() (*Tenant, error) {
obj := &Tenant{}

View File

@@ -28,7 +28,6 @@ type Routes struct {
common *Common
content *Content
creator *Creator
super *Super
tenant *Tenant
transaction *Transaction
user *User
@@ -169,109 +168,6 @@ func (r *Routes) Register(router fiber.Router) {
r.creator.UpdateSettings,
Body[dto.Settings]("form"),
))
// Register routes for controller: Super
r.log.Debugf("Registering route: Get /v1/auth/token -> super.CheckToken")
router.Get("/v1/auth/token"[len(r.Path()):], DataFunc0(
r.super.CheckToken,
))
r.log.Debugf("Registering route: Get /v1/contents -> super.ListContents")
router.Get("/v1/contents"[len(r.Path()):], DataFunc1(
r.super.ListContents,
Query[dto.SuperContentListFilter]("filter"),
))
r.log.Debugf("Registering route: Get /v1/orders -> super.ListOrders")
router.Get("/v1/orders"[len(r.Path()):], DataFunc1(
r.super.ListOrders,
Query[dto.SuperOrderListFilter]("filter"),
))
r.log.Debugf("Registering route: Get /v1/orders/:id -> super.GetOrder")
router.Get("/v1/orders/:id"[len(r.Path()):], DataFunc1(
r.super.GetOrder,
PathParam[int64]("id"),
))
r.log.Debugf("Registering route: Get /v1/orders/statistics -> super.OrderStatistics")
router.Get("/v1/orders/statistics"[len(r.Path()):], DataFunc0(
r.super.OrderStatistics,
))
r.log.Debugf("Registering route: Get /v1/tenants -> super.ListTenants")
router.Get("/v1/tenants"[len(r.Path()):], DataFunc1(
r.super.ListTenants,
Query[dto.TenantListFilter]("filter"),
))
r.log.Debugf("Registering route: Get /v1/tenants/:id -> super.GetTenant")
router.Get("/v1/tenants/:id"[len(r.Path()):], DataFunc1(
r.super.GetTenant,
PathParam[int64]("id"),
))
r.log.Debugf("Registering route: Get /v1/tenants/statuses -> super.TenantStatuses")
router.Get("/v1/tenants/statuses"[len(r.Path()):], DataFunc0(
r.super.TenantStatuses,
))
r.log.Debugf("Registering route: Get /v1/users -> super.ListUsers")
router.Get("/v1/users"[len(r.Path()):], DataFunc1(
r.super.ListUsers,
Query[dto.UserListFilter]("filter"),
))
r.log.Debugf("Registering route: Get /v1/users/:id -> super.GetUser")
router.Get("/v1/users/:id"[len(r.Path()):], DataFunc1(
r.super.GetUser,
PathParam[int64]("id"),
))
r.log.Debugf("Registering route: Get /v1/users/statistics -> super.UserStatistics")
router.Get("/v1/users/statistics"[len(r.Path()):], DataFunc0(
r.super.UserStatistics,
))
r.log.Debugf("Registering route: Get /v1/users/statuses -> super.UserStatuses")
router.Get("/v1/users/statuses"[len(r.Path()):], DataFunc0(
r.super.UserStatuses,
))
r.log.Debugf("Registering route: Patch /v1/tenants/:id -> super.UpdateTenantExpire")
router.Patch("/v1/tenants/:id"[len(r.Path()):], Func2(
r.super.UpdateTenantExpire,
PathParam[int64]("id"),
Body[dto.TenantExpireUpdateForm]("form"),
))
r.log.Debugf("Registering route: Patch /v1/tenants/:id/status -> super.UpdateTenantStatus")
router.Patch("/v1/tenants/:id/status"[len(r.Path()):], Func2(
r.super.UpdateTenantStatus,
PathParam[int64]("id"),
Body[dto.TenantStatusUpdateForm]("form"),
))
r.log.Debugf("Registering route: Patch /v1/tenants/:tenantID/contents/:contentID/status -> super.UpdateContentStatus")
router.Patch("/v1/tenants/:tenantID/contents/:contentID/status"[len(r.Path()):], Func3(
r.super.UpdateContentStatus,
PathParam[int64]("tenantID"),
PathParam[int64]("contentID"),
Body[dto.SuperTenantContentStatusUpdateForm]("form"),
))
r.log.Debugf("Registering route: Patch /v1/users/:id/roles -> super.UpdateUserRoles")
router.Patch("/v1/users/:id/roles"[len(r.Path()):], Func2(
r.super.UpdateUserRoles,
PathParam[int64]("id"),
Body[dto.UserRolesUpdateForm]("form"),
))
r.log.Debugf("Registering route: Patch /v1/users/:id/status -> super.UpdateUserStatus")
router.Patch("/v1/users/:id/status"[len(r.Path()):], Func2(
r.super.UpdateUserStatus,
PathParam[int64]("id"),
Body[dto.UserStatusUpdateForm]("form"),
))
r.log.Debugf("Registering route: Post /v1/auth/login -> super.Login")
router.Post("/v1/auth/login"[len(r.Path()):], DataFunc1(
r.super.Login,
Body[dto.LoginForm]("form"),
))
r.log.Debugf("Registering route: Post /v1/orders/:id/refund -> super.RefundOrder")
router.Post("/v1/orders/:id/refund"[len(r.Path()):], Func2(
r.super.RefundOrder,
PathParam[int64]("id"),
Body[dto.SuperOrderRefundForm]("form"),
))
r.log.Debugf("Registering route: Post /v1/tenants -> super.CreateTenant")
router.Post("/v1/tenants"[len(r.Path()):], Func1(
r.super.CreateTenant,
Body[dto.TenantCreateForm]("form"),
))
// Register routes for controller: Tenant
r.log.Debugf("Registering route: Delete /v1/tenants/:id/follow -> tenant.Unfollow")
router.Delete("/v1/tenants/:id/follow"[len(r.Path()):], Func1(

View File

@@ -1,322 +0,0 @@
package v1
import (
"quyun/v2/app/http/v1/dto"
"quyun/v2/app/requests"
"quyun/v2/app/services"
"github.com/gofiber/fiber/v3"
)
// @provider
type Super struct{}
// Login
//
// @Router /v1/auth/login [post]
// @Summary Login
// @Description Login
// @Tags Auth
// @Accept json
// @Produce json
// @Param form body dto.LoginForm true "Login form"
// @Success 200 {object} dto.LoginResponse
// @Bind form body
func (c *Super) Login(ctx fiber.Ctx, form *dto.LoginForm) (*dto.LoginResponse, error) {
return services.Super.Login(ctx.Context(), form)
}
// Check Token
//
// @Router /v1/auth/token [get]
// @Summary Check token
// @Description Check token
// @Tags Auth
// @Accept json
// @Produce json
// @Success 200 {object} dto.LoginResponse
func (c *Super) CheckToken(ctx fiber.Ctx) (*dto.LoginResponse, error) {
return services.Super.CheckToken(ctx.Context())
}
// List users
//
// @Router /v1/users [get]
// @Summary List users
// @Description List users
// @Tags User
// @Accept json
// @Produce json
// @Param page query int false "Page number"
// @Param limit query int false "Page size"
// @Param username query string false "Username"
// @Success 200 {object} requests.Pager{items=[]dto.UserItem}
// @Bind filter query
func (c *Super) ListUsers(ctx fiber.Ctx, filter *dto.UserListFilter) (*requests.Pager, error) {
return services.Super.ListUsers(ctx.Context(), filter)
}
// Get user
//
// @Router /v1/users/:id [get]
// @Summary Get user
// @Description Get user
// @Tags User
// @Accept json
// @Produce json
// @Param id path int64 true "User ID"
// @Success 200 {object} dto.UserItem
// @Bind id path
func (c *Super) GetUser(ctx fiber.Ctx, id int64) (*dto.UserItem, error) {
return services.Super.GetUser(ctx.Context(), id)
}
// Update user status
//
// @Router /v1/users/:id/status [patch]
// @Summary Update user status
// @Description Update user status
// @Tags User
// @Accept json
// @Produce json
// @Param id path int64 true "User ID"
// @Param form body dto.UserStatusUpdateForm true "Update form"
// @Success 200 {string} string "Updated"
// @Bind id path
// @Bind form body
func (c *Super) UpdateUserStatus(ctx fiber.Ctx, id int64, form *dto.UserStatusUpdateForm) error {
return services.Super.UpdateUserStatus(ctx.Context(), id, form)
}
// Update user roles
//
// @Router /v1/users/:id/roles [patch]
// @Summary Update user roles
// @Description Update user roles
// @Tags User
// @Accept json
// @Produce json
// @Param id path int64 true "User ID"
// @Param form body dto.UserRolesUpdateForm true "Update form"
// @Success 200 {string} string "Updated"
// @Bind id path
// @Bind form body
func (c *Super) UpdateUserRoles(ctx fiber.Ctx, id int64, form *dto.UserRolesUpdateForm) error {
return services.Super.UpdateUserRoles(ctx.Context(), id, form)
}
// List tenants
//
// @Router /v1/tenants [get]
// @Summary List tenants
// @Description List tenants
// @Tags Tenant
// @Accept json
// @Produce json
// @Param page query int false "Page number"
// @Param limit query int false "Page size"
// @Param name query string false "Name"
// @Success 200 {object} requests.Pager{items=[]dto.TenantItem}
// @Bind filter query
func (c *Super) ListTenants(ctx fiber.Ctx, filter *dto.TenantListFilter) (*requests.Pager, error) {
return services.Super.ListTenants(ctx.Context(), filter)
}
// Create tenant
//
// @Router /v1/tenants [post]
// @Summary Create tenant
// @Description Create tenant
// @Tags Tenant
// @Accept json
// @Produce json
// @Param form body dto.TenantCreateForm true "Create form"
// @Success 200 {string} string "Created"
// @Bind form body
func (c *Super) CreateTenant(ctx fiber.Ctx, form *dto.TenantCreateForm) error {
return services.Super.CreateTenant(ctx.Context(), form)
}
// Get tenant
//
// @Router /v1/tenants/:id [get]
// @Summary Get tenant
// @Description Get tenant
// @Tags Tenant
// @Accept json
// @Produce json
// @Param id path int64 true "Tenant ID"
// @Success 200 {object} dto.TenantItem
// @Bind id path
func (c *Super) GetTenant(ctx fiber.Ctx, id int64) (*dto.TenantItem, error) {
return services.Super.GetTenant(ctx.Context(), id)
}
// Update tenant status
//
// @Router /v1/tenants/:id/status [patch]
// @Summary Update tenant status
// @Description Update tenant status
// @Tags Tenant
// @Accept json
// @Produce json
// @Param id path int64 true "Tenant ID"
// @Param form body dto.TenantStatusUpdateForm true "Update form"
// @Success 200 {string} string "Updated"
// @Bind id path
// @Bind form body
func (c *Super) UpdateTenantStatus(ctx fiber.Ctx, id int64, form *dto.TenantStatusUpdateForm) error {
return services.Super.UpdateTenantStatus(ctx.Context(), id, form)
}
// Update tenant expire
//
// @Router /v1/tenants/:id [patch]
// @Summary Update tenant expire
// @Description Update tenant expire
// @Tags Tenant
// @Accept json
// @Produce json
// @Param id path int64 true "Tenant ID"
// @Param form body dto.TenantExpireUpdateForm true "Update form"
// @Success 200 {string} string "Updated"
// @Bind id path
// @Bind form body
func (c *Super) UpdateTenantExpire(ctx fiber.Ctx, id int64, form *dto.TenantExpireUpdateForm) error {
return services.Super.UpdateTenantExpire(ctx.Context(), id, form)
}
// List contents
//
// @Router /v1/contents [get]
// @Summary List contents
// @Description List contents
// @Tags Content
// @Accept json
// @Produce json
// @Param page query int false "Page number"
// @Param limit query int false "Page size"
// @Success 200 {object} requests.Pager{items=[]dto.AdminContentItem}
// @Bind filter query
func (c *Super) ListContents(ctx fiber.Ctx, filter *dto.SuperContentListFilter) (*requests.Pager, error) {
return services.Super.ListContents(ctx.Context(), filter)
}
// Update content status
//
// @Router /v1/tenants/:tenantID/contents/:contentID/status [patch]
// @Summary Update content status
// @Description Update content status
// @Tags Content
// @Accept json
// @Produce json
// @Param tenantID path int64 true "Tenant ID"
// @Param contentID path int64 true "Content ID"
// @Param form body dto.SuperTenantContentStatusUpdateForm true "Update form"
// @Success 200 {string} string "Updated"
// @Bind tenantID path
// @Bind contentID path
// @Bind form body
func (c *Super) UpdateContentStatus(ctx fiber.Ctx, tenantID, contentID int64, form *dto.SuperTenantContentStatusUpdateForm) error {
return services.Super.UpdateContentStatus(ctx.Context(), tenantID, contentID, form)
}
// List orders
//
// @Router /v1/orders [get]
// @Summary List orders
// @Description List orders
// @Tags Order
// @Accept json
// @Produce json
// @Param page query int false "Page number"
// @Param limit query int false "Page size"
// @Success 200 {object} requests.Pager{items=[]dto.SuperOrderItem}
// @Bind filter query
func (c *Super) ListOrders(ctx fiber.Ctx, filter *dto.SuperOrderListFilter) (*requests.Pager, error) {
return services.Super.ListOrders(ctx.Context(), filter)
}
// Get order
//
// @Router /v1/orders/:id [get]
// @Summary Get order
// @Description Get order
// @Tags Order
// @Accept json
// @Produce json
// @Param id path int64 true "Order ID"
// @Success 200 {object} dto.SuperOrderDetail
// @Bind id path
func (c *Super) GetOrder(ctx fiber.Ctx, id int64) (*dto.SuperOrderDetail, error) {
return services.Super.GetOrder(ctx.Context(), id)
}
// Refund order
//
// @Router /v1/orders/:id/refund [post]
// @Summary Refund order
// @Description Refund order
// @Tags Order
// @Accept json
// @Produce json
// @Param id path int64 true "Order ID"
// @Param form body dto.SuperOrderRefundForm true "Refund form"
// @Success 200 {string} string "Refunded"
// @Bind id path
// @Bind form body
func (c *Super) RefundOrder(ctx fiber.Ctx, id int64, form *dto.SuperOrderRefundForm) error {
return services.Super.RefundOrder(ctx.Context(), id, form)
}
// Order statistics
//
// @Router /v1/orders/statistics [get]
// @Summary Order statistics
// @Description Order statistics
// @Tags Order
// @Accept json
// @Produce json
// @Success 200 {object} dto.OrderStatisticsResponse
func (c *Super) OrderStatistics(ctx fiber.Ctx) (*dto.OrderStatisticsResponse, error) {
return services.Super.OrderStatistics(ctx.Context())
}
// User statistics
//
// @Router /v1/users/statistics [get]
// @Summary User statistics
// @Description User statistics
// @Tags User
// @Accept json
// @Produce json
// @Success 200 {array} dto.UserStatistics
func (c *Super) UserStatistics(ctx fiber.Ctx) ([]dto.UserStatistics, error) {
return services.Super.UserStatistics(ctx.Context())
}
// User statuses
//
// @Router /v1/users/statuses [get]
// @Summary User statuses
// @Description User statuses
// @Tags User
// @Accept json
// @Produce json
// @Success 200 {array} requests.KV
func (c *Super) UserStatuses(ctx fiber.Ctx) ([]requests.KV, error) {
return services.Super.UserStatuses(ctx.Context())
}
// Tenant statuses
//
// @Router /v1/tenants/statuses [get]
// @Summary Tenant statuses
// @Description Tenant statuses
// @Tags Tenant
// @Accept json
// @Produce json
// @Success 200 {array} requests.KV
func (c *Super) TenantStatuses(ctx fiber.Ctx) ([]requests.KV, error) {
return services.Super.TenantStatuses(ctx.Context())
}