feat: tenant-scoped routing and portal navigation

This commit is contained in:
2026-01-08 21:30:46 +08:00
parent f3aa92078a
commit 3e095c57f3
52 changed files with 1111 additions and 670 deletions

View File

@@ -1,11 +1,10 @@
package v1
import (
"quyun/v2/app/errorx"
"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"
)
@@ -15,7 +14,7 @@ type Content struct{}
// List contents (Explore / Search)
//
// @Router /v1/contents [get]
// @Router /t/:tenantCode/v1/contents [get]
// @Summary List contents
// @Description List contents with filtering and pagination
// @Tags Content
@@ -32,12 +31,19 @@ func (c *Content) List(
ctx fiber.Ctx,
filter *dto.ContentListFilter,
) (*requests.Pager, error) {
return services.Content.List(ctx, filter)
tenantID := getTenantID(ctx)
if tenantID > 0 {
if filter.TenantID != nil && *filter.TenantID != tenantID {
return nil, errorx.ErrForbidden.WithMsg("租户不匹配")
}
filter.TenantID = &tenantID
}
return services.Content.List(ctx, tenantID, filter)
}
// Get content detail
//
// @Router /v1/contents/:id<int> [get]
// @Router /t/:tenantCode/v1/contents/:id<int> [get]
// @Summary Get content detail
// @Description Get content detail by ID
// @Tags Content
@@ -47,13 +53,14 @@ func (c *Content) List(
// @Success 200 {object} dto.ContentDetail
// @Bind id path
func (c *Content) Get(ctx fiber.Ctx, id int64) (*dto.ContentDetail, error) {
tenantID := getTenantID(ctx)
uid := getUserID(ctx)
return services.Content.Get(ctx, uid, id)
return services.Content.Get(ctx, tenantID, uid, id)
}
// Get comments for a content
//
// @Router /v1/contents/:id<int>/comments [get]
// @Router /t/:tenantCode/v1/contents/:id<int>/comments [get]
// @Summary Get comments
// @Description Get comments for a content
// @Tags Content
@@ -65,13 +72,14 @@ func (c *Content) Get(ctx fiber.Ctx, id int64) (*dto.ContentDetail, error) {
// @Bind id path
// @Bind page query
func (c *Content) ListComments(ctx fiber.Ctx, id int64, page int) (*requests.Pager, error) {
tenantID := getTenantID(ctx)
uid := getUserID(ctx)
return services.Content.ListComments(ctx, uid, id, page)
return services.Content.ListComments(ctx, tenantID, uid, id, page)
}
// Post a comment
//
// @Router /v1/contents/:id<int>/comments [post]
// @Router /t/:tenantCode/v1/contents/:id<int>/comments [post]
// @Summary Post comment
// @Description Post a comment to a content
// @Tags Content
@@ -83,13 +91,14 @@ func (c *Content) ListComments(ctx fiber.Ctx, id int64, page int) (*requests.Pag
// @Bind id path
// @Bind form body
func (c *Content) CreateComment(ctx fiber.Ctx, id int64, form *dto.CommentCreateForm) error {
tenantID := getTenantID(ctx)
uid := getUserID(ctx)
return services.Content.CreateComment(ctx, uid, id, form)
return services.Content.CreateComment(ctx, tenantID, uid, id, form)
}
// Like a comment
//
// @Router /v1/comments/:id<int>/like [post]
// @Router /t/:tenantCode/v1/comments/:id<int>/like [post]
// @Summary Like comment
// @Description Like a comment
// @Tags Content
@@ -99,65 +108,70 @@ func (c *Content) CreateComment(ctx fiber.Ctx, id int64, form *dto.CommentCreate
// @Success 200 {string} string "Liked"
// @Bind id path
func (c *Content) LikeComment(ctx fiber.Ctx, id int64) error {
tenantID := getTenantID(ctx)
uid := getUserID(ctx)
return services.Content.LikeComment(ctx, uid, id)
return services.Content.LikeComment(ctx, tenantID, uid, id)
}
// Add like
//
// @Router /v1/contents/:id<int>/like [post]
// @Router /t/:tenantCode/v1/contents/:id<int>/like [post]
// @Summary Add like
// @Tags Content
// @Param id path int64 true "Content ID"
// @Success 200 {string} string "Liked"
// @Bind id path
func (c *Content) AddLike(ctx fiber.Ctx, id int64) error {
tenantID := getTenantID(ctx)
uid := getUserID(ctx)
return services.Content.AddLike(ctx, uid, id)
return services.Content.AddLike(ctx, tenantID, uid, id)
}
// Remove like
//
// @Router /v1/contents/:id<int>/like [delete]
// @Router /t/:tenantCode/v1/contents/:id<int>/like [delete]
// @Summary Remove like
// @Tags Content
// @Param id path int64 true "Content ID"
// @Success 200 {string} string "Unliked"
// @Bind id path
func (c *Content) RemoveLike(ctx fiber.Ctx, id int64) error {
tenantID := getTenantID(ctx)
uid := getUserID(ctx)
return services.Content.RemoveLike(ctx, uid, id)
return services.Content.RemoveLike(ctx, tenantID, uid, id)
}
// Add favorite
//
// @Router /v1/contents/:id<int>/favorite [post]
// @Router /t/:tenantCode/v1/contents/:id<int>/favorite [post]
// @Summary Add favorite
// @Tags Content
// @Param id path int64 true "Content ID"
// @Success 200 {string} string "Favorited"
// @Bind id path
func (c *Content) AddFavorite(ctx fiber.Ctx, id int64) error {
tenantID := getTenantID(ctx)
uid := getUserID(ctx)
return services.Content.AddFavorite(ctx, uid, id)
return services.Content.AddFavorite(ctx, tenantID, uid, id)
}
// Remove favorite
//
// @Router /v1/contents/:id<int>/favorite [delete]
// @Router /t/:tenantCode/v1/contents/:id<int>/favorite [delete]
// @Summary Remove favorite
// @Tags Content
// @Param id path int64 true "Content ID"
// @Success 200 {string} string "Unfavorited"
// @Bind id path
func (c *Content) RemoveFavorite(ctx fiber.Ctx, id int64) error {
tenantID := getTenantID(ctx)
uid := getUserID(ctx)
return services.Content.RemoveFavorite(ctx, uid, id)
return services.Content.RemoveFavorite(ctx, tenantID, uid, id)
}
// List curated topics
//
// @Router /v1/topics [get]
// @Router /t/:tenantCode/v1/topics [get]
// @Summary List topics
// @Description List curated topics
// @Tags Content
@@ -165,14 +179,6 @@ func (c *Content) RemoveFavorite(ctx fiber.Ctx, id int64) error {
// @Produce json
// @Success 200 {array} dto.Topic
func (c *Content) ListTopics(ctx fiber.Ctx) ([]dto.Topic, error) {
return services.Content.ListTopics(ctx)
}
func getUserID(ctx fiber.Ctx) int64 {
if u := ctx.Locals(consts.CtxKeyUser); u != nil {
if user, ok := u.(*models.User); ok {
return user.ID
}
}
return 0
tenantID := getTenantID(ctx)
return services.Content.ListTopics(ctx, tenantID)
}