feat: update medias

This commit is contained in:
yanghao05
2025-04-09 19:08:35 +08:00
parent f328de00fd
commit 1ef2f9318e
5 changed files with 28 additions and 10 deletions

View File

@@ -44,6 +44,22 @@ func (m *mediasModel) Prepare() error {
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
func (m *mediasModel) countByCondition(ctx context.Context, expr BoolExpression) (int64, error) {
var cnt struct {
@@ -63,12 +79,13 @@ func (m *mediasModel) countByCondition(ctx context.Context, expr BoolExpression)
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()
tbl := table.Medias
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(expr).
ORDER_BY(tbl.ID.DESC()).
LIMIT(pagination.Limit).
OFFSET(pagination.Offset)
@@ -81,7 +98,7 @@ func (m *mediasModel) List(ctx context.Context, pagination *requests.Pagination)
return nil, err
}
count, err := m.countByCondition(ctx, Bool(true))
count, err := m.countByCondition(ctx, expr)
if err != nil {
m.log.Errorf("error getting media count: %v", err)
return nil, err

View File

@@ -167,20 +167,20 @@ func (s *MediasTestSuite) Test_Page() {
Convey("Page", 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(pager.Total, ShouldEqual, 20)
So(pager.Items, ShouldHaveLength, 10)
})
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(pager.Total, ShouldEqual, 20)
So(pager.Items, ShouldHaveLength, 10)
})
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(pager.Total, ShouldEqual, 20)
So(pager.Items, ShouldBeEmpty)

View File

@@ -3,7 +3,6 @@ package models
import (
"context"
"errors"
"log"
"time"
"quyun/app/requests"
@@ -35,7 +34,6 @@ func (m *postsModel) BuildConditionWithKey(key *string) BoolExpression {
)
if key == nil || *key == "" {
log.Fatal(*key)
return cond
}