feat: update medias
This commit is contained in:
@@ -13,6 +13,8 @@ type medias struct{}
|
|||||||
// List medias
|
// List medias
|
||||||
// @Router /v1/admin/medias [get]
|
// @Router /v1/admin/medias [get]
|
||||||
// @Bind pagination query
|
// @Bind pagination query
|
||||||
func (ctl *medias) List(ctx fiber.Ctx, pagination *requests.Pagination) (*requests.Pager, error) {
|
// @Bind query query
|
||||||
return models.Medias.List(ctx.Context(), pagination)
|
func (ctl *medias) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
|
||||||
|
cond := models.Medias.BuildConditionWithKey(query.Keyword)
|
||||||
|
return models.Medias.List(ctx.Context(), pagination, cond)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,9 +32,10 @@ func (r *Routes) Name() string {
|
|||||||
|
|
||||||
func (r *Routes) Register(router fiber.Router) {
|
func (r *Routes) Register(router fiber.Router) {
|
||||||
// 注册路由组: medias
|
// 注册路由组: medias
|
||||||
router.Get("/v1/admin/medias", DataFunc1(
|
router.Get("/v1/admin/medias", DataFunc2(
|
||||||
r.medias.List,
|
r.medias.List,
|
||||||
Query[requests.Pagination]("pagination"),
|
Query[requests.Pagination]("pagination"),
|
||||||
|
Query[ListQuery]("query"),
|
||||||
))
|
))
|
||||||
|
|
||||||
// 注册路由组: posts
|
// 注册路由组: posts
|
||||||
|
|||||||
@@ -44,6 +44,22 @@ func (m *mediasModel) Prepare() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *mediasModel) BuildConditionWithKey(key *string) BoolExpression {
|
||||||
|
tbl := table.Medias
|
||||||
|
|
||||||
|
cond := Bool(true)
|
||||||
|
|
||||||
|
if key == nil || *key == "" {
|
||||||
|
return cond
|
||||||
|
}
|
||||||
|
|
||||||
|
cond = cond.AND(
|
||||||
|
tbl.Name.LIKE(String("%" + *key + "%")),
|
||||||
|
)
|
||||||
|
|
||||||
|
return cond
|
||||||
|
}
|
||||||
|
|
||||||
// countByCond
|
// countByCond
|
||||||
func (m *mediasModel) countByCondition(ctx context.Context, expr BoolExpression) (int64, error) {
|
func (m *mediasModel) countByCondition(ctx context.Context, expr BoolExpression) (int64, error) {
|
||||||
var cnt struct {
|
var cnt struct {
|
||||||
@@ -63,12 +79,13 @@ func (m *mediasModel) countByCondition(ctx context.Context, expr BoolExpression)
|
|||||||
return cnt.Cnt, nil
|
return cnt.Cnt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mediasModel) List(ctx context.Context, pagination *requests.Pagination) (*requests.Pager, error) {
|
func (m *mediasModel) List(ctx context.Context, pagination *requests.Pagination, expr BoolExpression) (*requests.Pager, error) {
|
||||||
pagination.Format()
|
pagination.Format()
|
||||||
|
|
||||||
tbl := table.Medias
|
tbl := table.Medias
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(tbl.AllColumns).
|
SELECT(tbl.AllColumns).
|
||||||
|
WHERE(expr).
|
||||||
ORDER_BY(tbl.ID.DESC()).
|
ORDER_BY(tbl.ID.DESC()).
|
||||||
LIMIT(pagination.Limit).
|
LIMIT(pagination.Limit).
|
||||||
OFFSET(pagination.Offset)
|
OFFSET(pagination.Offset)
|
||||||
@@ -81,7 +98,7 @@ func (m *mediasModel) List(ctx context.Context, pagination *requests.Pagination)
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
count, err := m.countByCondition(ctx, Bool(true))
|
count, err := m.countByCondition(ctx, expr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.log.Errorf("error getting media count: %v", err)
|
m.log.Errorf("error getting media count: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -167,20 +167,20 @@ func (s *MediasTestSuite) Test_Page() {
|
|||||||
|
|
||||||
Convey("Page", func() {
|
Convey("Page", func() {
|
||||||
Convey("page 1", func() {
|
Convey("page 1", func() {
|
||||||
pager, err := Medias.List(context.Background(), &requests.Pagination{Page: 1, Limit: 10})
|
pager, err := Medias.List(context.Background(), &requests.Pagination{Page: 1, Limit: 10}, nil)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(pager.Total, ShouldEqual, 20)
|
So(pager.Total, ShouldEqual, 20)
|
||||||
So(pager.Items, ShouldHaveLength, 10)
|
So(pager.Items, ShouldHaveLength, 10)
|
||||||
})
|
})
|
||||||
Convey("page 2", func() {
|
Convey("page 2", func() {
|
||||||
pager, err := Medias.List(context.Background(), &requests.Pagination{Page: 2, Limit: 10})
|
pager, err := Medias.List(context.Background(), &requests.Pagination{Page: 2, Limit: 10}, nil)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(pager.Total, ShouldEqual, 20)
|
So(pager.Total, ShouldEqual, 20)
|
||||||
So(pager.Items, ShouldHaveLength, 10)
|
So(pager.Items, ShouldHaveLength, 10)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("page 3", func() {
|
Convey("page 3", func() {
|
||||||
pager, err := Medias.List(context.Background(), &requests.Pagination{Page: 3, Limit: 10})
|
pager, err := Medias.List(context.Background(), &requests.Pagination{Page: 3, Limit: 10}, nil)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(pager.Total, ShouldEqual, 20)
|
So(pager.Total, ShouldEqual, 20)
|
||||||
So(pager.Items, ShouldBeEmpty)
|
So(pager.Items, ShouldBeEmpty)
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package models
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"quyun/app/requests"
|
"quyun/app/requests"
|
||||||
@@ -35,7 +34,6 @@ func (m *postsModel) BuildConditionWithKey(key *string) BoolExpression {
|
|||||||
)
|
)
|
||||||
|
|
||||||
if key == nil || *key == "" {
|
if key == nil || *key == "" {
|
||||||
log.Fatal(*key)
|
|
||||||
return cond
|
return cond
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user