This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
package dto
|
||||
|
||||
import "quyun/v2/app/requests"
|
||||
import (
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/pkg/fields"
|
||||
)
|
||||
|
||||
type PostListQuery struct {
|
||||
*requests.Pagination
|
||||
Keyword *string `query:"keyword"`
|
||||
Keyword *string `query:"keyword"`
|
||||
Status fields.PostStatus `query:"status"`
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"quyun/v2/app/jobs"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/fields"
|
||||
"quyun/v2/providers/ali"
|
||||
@@ -21,7 +20,6 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"go.ipao.vip/gen"
|
||||
)
|
||||
|
||||
type ListQuery struct {
|
||||
@@ -46,6 +44,8 @@ type posts struct {
|
||||
// @Bind query query
|
||||
// @Bind user local
|
||||
func (ctl *posts) List(ctx fiber.Ctx, query *dto.PostListQuery, user *models.User) (*requests.Pager, error) {
|
||||
query.Status = fields.PostStatusPublished
|
||||
|
||||
pager, err := services.Posts.List(ctx, query)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("post list err: %v", err)
|
||||
@@ -232,31 +232,19 @@ func (ctl *posts) Play(ctx fiber.Ctx, post *models.Post, user *models.User) (*Pl
|
||||
// @Summary 我的已购作品
|
||||
// @Tags Posts
|
||||
// @Produce json
|
||||
// @Param pagination query requests.Pagination false "分页参数"
|
||||
// @Param query query ListQuery false "筛选条件"
|
||||
// @Success 200 {object} requests.Pager{items=PostItem} "成功"
|
||||
// @Router /v1/posts/mine [get]
|
||||
// @Bind pagination query
|
||||
// @Bind query query
|
||||
// @Bind user local
|
||||
func (ctl *posts) Mine(
|
||||
ctx fiber.Ctx,
|
||||
pagination *requests.Pagination,
|
||||
query *ListQuery,
|
||||
query *dto.PostListQuery,
|
||||
user *models.User,
|
||||
) (*requests.Pager, error) {
|
||||
log.Infof("Fetching posts for user with pagination: %+v and keyword: %v", pagination, query.Keyword)
|
||||
log.Infof("Fetching posts for user with keyword: %v", query.Keyword)
|
||||
|
||||
conds := []gen.Condition{
|
||||
models.PostQuery.Status.Eq(fields.PostStatusPublished),
|
||||
}
|
||||
if query.Keyword != nil && *query.Keyword != "" {
|
||||
conds = append(conds,
|
||||
models.PostQuery.Title.Like(database.WrapLike(*query.Keyword)),
|
||||
)
|
||||
}
|
||||
|
||||
pager, err := services.Users.PostList(ctx, user.ID, pagination, conds...)
|
||||
pager, err := services.Users.PostList(ctx, user.ID, query)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("post list err: %v", err)
|
||||
return nil, err
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"go.ipao.vip/gen/field"
|
||||
"quyun/v2/app/http/dto"
|
||||
"quyun/v2/app/middlewares"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
@@ -83,10 +82,9 @@ func (r *Routes) Register(router fiber.Router) {
|
||||
Local[*models.User]("user"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /v1/posts/mine -> posts.Mine")
|
||||
router.Get("/v1/posts/mine"[len(r.Path()):], DataFunc3(
|
||||
router.Get("/v1/posts/mine"[len(r.Path()):], DataFunc2(
|
||||
r.posts.Mine,
|
||||
Query[requests.Pagination]("pagination"),
|
||||
Query[ListQuery]("query"),
|
||||
Query[dto.PostListQuery]("query"),
|
||||
Local[*models.User]("user"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /v1/posts/:id/buy -> posts.Buy")
|
||||
|
||||
@@ -71,30 +71,15 @@ func (m *users) List(
|
||||
}
|
||||
|
||||
// PostList returns a paginated list of posts for a user
|
||||
func (m *users) PostList(
|
||||
ctx context.Context,
|
||||
userId int64,
|
||||
pagination *requests.Pagination,
|
||||
conds ...gen.Condition,
|
||||
) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
// stmt := SELECT(tbl.AllColumns).
|
||||
// FROM(tbl.
|
||||
// RIGHT_JOIN(
|
||||
// tblUserPosts,
|
||||
// tblUserPosts.PostID.EQ(tbl.ID),
|
||||
// ),
|
||||
// ).
|
||||
// WHERE(CondTrue(cond...)).
|
||||
// ORDER_BY(tblUserPosts.ID.DESC()).
|
||||
// LIMIT(pagination.Limit).
|
||||
// OFFSET(pagination.Offset)
|
||||
// m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
func (m *users) PostList(ctx context.Context, userId int64, filter *dto.PostListQuery) (*requests.Pager, error) {
|
||||
filter.Format()
|
||||
|
||||
tbl, query := models.UserPostQuery.QueryContext(ctx)
|
||||
query = query.Order(tbl.CreatedAt.Desc())
|
||||
|
||||
pagePosts, cnt, err := query.Select(tbl.PostID).
|
||||
Where(tbl.UserID.Eq(userId)).
|
||||
FindByPage(int(pagination.Offset()), int(pagination.Limit))
|
||||
FindByPage(int(filter.Offset()), int(filter.Limit))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -105,10 +90,21 @@ func (m *users) PostList(
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
itemMap := lo.KeyBy(items, func(item *models.Post) int64 { return item.ID })
|
||||
|
||||
tmpItems := []*models.Post{}
|
||||
|
||||
for _, id := range postIds {
|
||||
if i, ok := itemMap[id]; ok {
|
||||
tmpItems = append(tmpItems, i)
|
||||
}
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Items: items,
|
||||
Items: tmpItems,
|
||||
Total: cnt,
|
||||
Pagination: *pagination,
|
||||
Pagination: *filter.Pagination,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user