fix: query
This commit is contained in:
@@ -1,14 +1,13 @@
|
|||||||
package admin
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"quyun/v2/app/http/dto"
|
||||||
"quyun/v2/app/requests"
|
"quyun/v2/app/requests"
|
||||||
"quyun/v2/app/services"
|
"quyun/v2/app/services"
|
||||||
"quyun/v2/database"
|
|
||||||
"quyun/v2/database/models"
|
"quyun/v2/database/models"
|
||||||
"quyun/v2/providers/ali"
|
"quyun/v2/providers/ali"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
"go.ipao.vip/gen"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// @provider
|
// @provider
|
||||||
@@ -21,19 +20,12 @@ type medias struct {
|
|||||||
// @Summary 媒体列表
|
// @Summary 媒体列表
|
||||||
// @Tags Admin Medias
|
// @Tags Admin Medias
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param pagination query requests.Pagination false "分页参数"
|
|
||||||
// @Param query query ListQuery false "筛选条件"
|
// @Param query query ListQuery false "筛选条件"
|
||||||
// @Success 200 {object} requests.Pager{items=models.Medium} "成功"
|
// @Success 200 {object} requests.Pager{items=models.Medium} "成功"
|
||||||
// @Router /admin/v1/medias [get]
|
// @Router /admin/v1/medias [get]
|
||||||
// @Bind pagination query
|
|
||||||
// @Bind query query
|
// @Bind query query
|
||||||
func (ctl *medias) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
|
func (ctl *medias) List(ctx fiber.Ctx, query *dto.ListQuery) (*requests.Pager, error) {
|
||||||
conds := []gen.Condition{}
|
return services.Media.List(ctx, query)
|
||||||
if query.Keyword != nil {
|
|
||||||
conds = append(conds, models.MediumQuery.Name.Like(database.WrapLike(*query.Keyword)))
|
|
||||||
}
|
|
||||||
|
|
||||||
return services.Media.List(ctx, pagination, conds...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show media
|
// Show media
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package admin
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"quyun/v2/app/http/admin/dto"
|
"quyun/v2/app/http/dto"
|
||||||
"quyun/v2/app/requests"
|
"quyun/v2/app/requests"
|
||||||
"quyun/v2/app/services"
|
"quyun/v2/app/services"
|
||||||
"quyun/v2/database/models"
|
"quyun/v2/database/models"
|
||||||
|
|||||||
@@ -1,22 +1,17 @@
|
|||||||
package admin
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"quyun/v2/app/http/dto"
|
||||||
"quyun/v2/app/requests"
|
"quyun/v2/app/requests"
|
||||||
"quyun/v2/app/services"
|
"quyun/v2/app/services"
|
||||||
"quyun/v2/database"
|
|
||||||
"quyun/v2/database/models"
|
"quyun/v2/database/models"
|
||||||
"quyun/v2/pkg/fields"
|
"quyun/v2/pkg/fields"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
"go.ipao.vip/gen"
|
|
||||||
"go.ipao.vip/gen/types"
|
"go.ipao.vip/gen/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ListQuery struct {
|
|
||||||
Keyword *string `query:"keyword"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// @provider
|
// @provider
|
||||||
type posts struct{}
|
type posts struct{}
|
||||||
|
|
||||||
@@ -25,18 +20,12 @@ type posts struct{}
|
|||||||
// @Summary 作品列表
|
// @Summary 作品列表
|
||||||
// @Tags Admin Posts
|
// @Tags Admin Posts
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param pagination query requests.Pagination false "分页参数"
|
// @Param query query dto.PostListQuery false "筛选条件"
|
||||||
// @Param query query ListQuery false "筛选条件"
|
|
||||||
// @Success 200 {object} requests.Pager{items=PostItem} "成功"
|
// @Success 200 {object} requests.Pager{items=PostItem} "成功"
|
||||||
// @Router /admin/v1/posts [get]
|
// @Router /admin/v1/posts [get]
|
||||||
// @Bind pagination query
|
|
||||||
// @Bind query query
|
// @Bind query query
|
||||||
func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
|
func (ctl *posts) List(ctx fiber.Ctx, query *dto.PostListQuery) (*requests.Pager, error) {
|
||||||
conds := []gen.Condition{}
|
pager, err := services.Posts.List(ctx, query)
|
||||||
if query.Keyword != nil && *query.Keyword != "" {
|
|
||||||
conds = append(conds, models.PostQuery.Title.Like(database.WrapLike(*query.Keyword)))
|
|
||||||
}
|
|
||||||
pager, err := services.Posts.List(ctx, pagination, conds...)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ package admin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"go.ipao.vip/gen/field"
|
"go.ipao.vip/gen/field"
|
||||||
"quyun/v2/app/http/admin/dto"
|
"quyun/v2/app/http/dto"
|
||||||
"quyun/v2/app/middlewares"
|
"quyun/v2/app/middlewares"
|
||||||
"quyun/v2/app/requests"
|
"quyun/v2/app/requests"
|
||||||
"quyun/v2/database/models"
|
"quyun/v2/database/models"
|
||||||
@@ -66,10 +66,9 @@ func (r *Routes) Register(router fiber.Router) {
|
|||||||
},
|
},
|
||||||
))
|
))
|
||||||
r.log.Debugf("Registering route: Get /admin/v1/medias -> medias.List")
|
r.log.Debugf("Registering route: Get /admin/v1/medias -> medias.List")
|
||||||
router.Get("/admin/v1/medias"[len(r.Path()):], DataFunc2(
|
router.Get("/admin/v1/medias"[len(r.Path()):], DataFunc1(
|
||||||
r.medias.List,
|
r.medias.List,
|
||||||
Query[requests.Pagination]("pagination"),
|
Query[dto.ListQuery]("query"),
|
||||||
Query[ListQuery]("query"),
|
|
||||||
))
|
))
|
||||||
r.log.Debugf("Registering route: Get /admin/v1/medias/:id -> medias.Show")
|
r.log.Debugf("Registering route: Get /admin/v1/medias/:id -> medias.Show")
|
||||||
router.Get("/admin/v1/medias/:id"[len(r.Path()):], Func1(
|
router.Get("/admin/v1/medias/:id"[len(r.Path()):], Func1(
|
||||||
@@ -103,10 +102,9 @@ func (r *Routes) Register(router fiber.Router) {
|
|||||||
},
|
},
|
||||||
))
|
))
|
||||||
r.log.Debugf("Registering route: Get /admin/v1/posts -> posts.List")
|
r.log.Debugf("Registering route: Get /admin/v1/posts -> posts.List")
|
||||||
router.Get("/admin/v1/posts"[len(r.Path()):], DataFunc2(
|
router.Get("/admin/v1/posts"[len(r.Path()):], DataFunc1(
|
||||||
r.posts.List,
|
r.posts.List,
|
||||||
Query[requests.Pagination]("pagination"),
|
Query[dto.PostListQuery]("query"),
|
||||||
Query[ListQuery]("query"),
|
|
||||||
))
|
))
|
||||||
r.log.Debugf("Registering route: Get /admin/v1/posts/:id -> posts.Show")
|
r.log.Debugf("Registering route: Get /admin/v1/posts/:id -> posts.Show")
|
||||||
router.Get("/admin/v1/posts/:id"[len(r.Path()):], DataFunc1(
|
router.Get("/admin/v1/posts/:id"[len(r.Path()):], DataFunc1(
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package admin
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"quyun/v2/app/http/admin/dto"
|
"quyun/v2/app/http/dto"
|
||||||
"quyun/v2/app/requests"
|
"quyun/v2/app/requests"
|
||||||
"quyun/v2/app/services"
|
"quyun/v2/app/services"
|
||||||
"quyun/v2/database/models"
|
"quyun/v2/database/models"
|
||||||
|
|||||||
8
backend_v1/app/http/dto/common.go
Normal file
8
backend_v1/app/http/dto/common.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
import "quyun/v2/app/requests"
|
||||||
|
|
||||||
|
type ListQuery struct {
|
||||||
|
*requests.Pagination
|
||||||
|
Keyword *string `query:"keyword"`
|
||||||
|
}
|
||||||
8
backend_v1/app/http/dto/post.go
Normal file
8
backend_v1/app/http/dto/post.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
import "quyun/v2/app/requests"
|
||||||
|
|
||||||
|
type PostListQuery struct {
|
||||||
|
*requests.Pagination
|
||||||
|
Keyword *string `query:"keyword"`
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"quyun/v2/app/http/dto"
|
||||||
"quyun/v2/app/jobs"
|
"quyun/v2/app/jobs"
|
||||||
"quyun/v2/app/requests"
|
"quyun/v2/app/requests"
|
||||||
"quyun/v2/app/services"
|
"quyun/v2/app/services"
|
||||||
@@ -39,30 +40,13 @@ type posts struct {
|
|||||||
// @Summary 作品列表
|
// @Summary 作品列表
|
||||||
// @Tags Posts
|
// @Tags Posts
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param pagination query requests.Pagination false "分页参数"
|
// @Param query query dto.PostListQuery false "筛选条件"
|
||||||
// @Param query query ListQuery false "筛选条件"
|
|
||||||
// @Success 200 {object} requests.Pager{items=PostItem} "成功"
|
// @Success 200 {object} requests.Pager{items=PostItem} "成功"
|
||||||
// @Router /v1/posts [get]
|
// @Router /v1/posts [get]
|
||||||
// @Bind pagination query
|
|
||||||
// @Bind query query
|
// @Bind query query
|
||||||
// @Bind user local
|
// @Bind user local
|
||||||
func (ctl *posts) List(
|
func (ctl *posts) List(ctx fiber.Ctx, query *dto.PostListQuery, user *models.User) (*requests.Pager, error) {
|
||||||
ctx fiber.Ctx,
|
pager, err := services.Posts.List(ctx, query)
|
||||||
pagination *requests.Pagination,
|
|
||||||
query *ListQuery,
|
|
||||||
user *models.User,
|
|
||||||
) (*requests.Pager, error) {
|
|
||||||
tbl, _ := models.PostQuery.QueryContext(ctx)
|
|
||||||
conds := []gen.Condition{
|
|
||||||
tbl.Status.Eq(fields.PostStatusPublished),
|
|
||||||
}
|
|
||||||
if query.Keyword != nil && *query.Keyword != "" {
|
|
||||||
conds = append(conds,
|
|
||||||
tbl.Title.Like(database.WrapLike(*query.Keyword)),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pager, err := services.Posts.List(ctx, pagination, conds...)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Errorf("post list err: %v", err)
|
log.WithError(err).Errorf("post list err: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"go.ipao.vip/gen/field"
|
"go.ipao.vip/gen/field"
|
||||||
|
"quyun/v2/app/http/dto"
|
||||||
"quyun/v2/app/middlewares"
|
"quyun/v2/app/middlewares"
|
||||||
"quyun/v2/app/requests"
|
"quyun/v2/app/requests"
|
||||||
"quyun/v2/database/models"
|
"quyun/v2/database/models"
|
||||||
@@ -58,10 +59,9 @@ func (r *Routes) Register(router fiber.Router) {
|
|||||||
))
|
))
|
||||||
// Register routes for controller: posts
|
// Register routes for controller: posts
|
||||||
r.log.Debugf("Registering route: Get /v1/posts -> posts.List")
|
r.log.Debugf("Registering route: Get /v1/posts -> posts.List")
|
||||||
router.Get("/v1/posts"[len(r.Path()):], DataFunc3(
|
router.Get("/v1/posts"[len(r.Path()):], DataFunc2(
|
||||||
r.posts.List,
|
r.posts.List,
|
||||||
Query[requests.Pagination]("pagination"),
|
Query[dto.PostListQuery]("query"),
|
||||||
Query[ListQuery]("query"),
|
|
||||||
Local[*models.User]("user"),
|
Local[*models.User]("user"),
|
||||||
))
|
))
|
||||||
r.log.Debugf("Registering route: Get /v1/posts/:id/play -> posts.Play")
|
r.log.Debugf("Registering route: Get /v1/posts/:id/play -> posts.Play")
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package services
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"quyun/v2/app/http/dto"
|
||||||
"quyun/v2/app/requests"
|
"quyun/v2/app/requests"
|
||||||
|
"quyun/v2/database"
|
||||||
"quyun/v2/database/models"
|
"quyun/v2/database/models"
|
||||||
"quyun/v2/pkg/fields"
|
"quyun/v2/pkg/fields"
|
||||||
|
|
||||||
@@ -14,19 +16,18 @@ import (
|
|||||||
// @provider
|
// @provider
|
||||||
type media struct{}
|
type media struct{}
|
||||||
|
|
||||||
func (m *media) List(
|
func (m *media) List(ctx context.Context, filter *dto.ListQuery) (*requests.Pager, error) {
|
||||||
ctx context.Context,
|
filter.Format()
|
||||||
pagination *requests.Pagination,
|
|
||||||
conds ...gen.Condition,
|
|
||||||
) (*requests.Pager, error) {
|
|
||||||
pagination.Format()
|
|
||||||
|
|
||||||
tbl, query := models.MediumQuery.QueryContext(ctx)
|
tbl, query := models.MediumQuery.QueryContext(ctx)
|
||||||
|
|
||||||
items, cnt, err := query.
|
query = query.Order(tbl.ID.Desc())
|
||||||
Where(conds...).
|
|
||||||
Order(tbl.ID.Desc()).
|
if filter.Keyword != nil && *filter.Keyword != "" {
|
||||||
FindByPage(int(pagination.Offset()), int(pagination.Limit))
|
query = query.Where(tbl.Name.Like(database.WrapLike(*filter.Keyword)))
|
||||||
|
}
|
||||||
|
|
||||||
|
items, cnt, err := query.FindByPage(int(filter.Offset()), int(filter.Limit))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to list media items")
|
return nil, errors.Wrap(err, "failed to list media items")
|
||||||
}
|
}
|
||||||
@@ -34,7 +35,7 @@ func (m *media) List(
|
|||||||
return &requests.Pager{
|
return &requests.Pager{
|
||||||
Items: items,
|
Items: items,
|
||||||
Total: cnt,
|
Total: cnt,
|
||||||
Pagination: *pagination,
|
Pagination: *filter.Pagination,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"quyun/v2/app/http/admin/dto"
|
"quyun/v2/app/http/dto"
|
||||||
"quyun/v2/app/requests"
|
"quyun/v2/app/requests"
|
||||||
"quyun/v2/database"
|
"quyun/v2/database"
|
||||||
"quyun/v2/database/models"
|
"quyun/v2/database/models"
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"quyun/v2/app/http/dto"
|
||||||
"quyun/v2/app/requests"
|
"quyun/v2/app/requests"
|
||||||
"quyun/v2/database"
|
"quyun/v2/database"
|
||||||
"quyun/v2/database/models"
|
"quyun/v2/database/models"
|
||||||
@@ -28,17 +29,20 @@ func (m *posts) IncrViewCount(ctx context.Context, postID int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List
|
// List
|
||||||
func (m *posts) List(
|
func (m *posts) List(ctx context.Context, filter *dto.PostListQuery) (*requests.Pager, error) {
|
||||||
ctx context.Context,
|
filter.Pagination.Format()
|
||||||
pagination *requests.Pagination,
|
|
||||||
conds ...gen.Condition,
|
// conds := []gen.Condition{}
|
||||||
) (*requests.Pager, error) {
|
|
||||||
pagination.Format()
|
|
||||||
|
|
||||||
tbl, query := models.PostQuery.QueryContext(ctx)
|
tbl, query := models.PostQuery.QueryContext(ctx)
|
||||||
items, cnt, err := query.Where(conds...).
|
query = query.Order(tbl.ID.Desc())
|
||||||
Order(tbl.ID.Desc()).
|
|
||||||
FindByPage(int(pagination.Offset()), int(pagination.Limit))
|
if filter.Keyword != nil && *filter.Keyword != "" {
|
||||||
|
keyword := database.WrapLike(*filter.Keyword)
|
||||||
|
query = query.Where(tbl.Title.Like(keyword))
|
||||||
|
}
|
||||||
|
|
||||||
|
items, cnt, err := query.FindByPage(int(filter.Offset()), int(filter.Limit))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "list post failed")
|
return nil, errors.Wrap(err, "list post failed")
|
||||||
}
|
}
|
||||||
@@ -46,7 +50,7 @@ func (m *posts) List(
|
|||||||
return &requests.Pager{
|
return &requests.Pager{
|
||||||
Items: items,
|
Items: items,
|
||||||
Total: cnt,
|
Total: cnt,
|
||||||
Pagination: *pagination,
|
Pagination: *filter.Pagination,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"quyun/v2/app/http/admin/dto"
|
"quyun/v2/app/http/dto"
|
||||||
"quyun/v2/app/requests"
|
"quyun/v2/app/requests"
|
||||||
"quyun/v2/database"
|
"quyun/v2/database"
|
||||||
"quyun/v2/database/models"
|
"quyun/v2/database/models"
|
||||||
|
|||||||
Reference in New Issue
Block a user