From 1ef2f9318e23c667fa6328b0843fb81e84ce6c2a Mon Sep 17 00:00:00 2001 From: yanghao05 Date: Wed, 9 Apr 2025 19:08:35 +0800 Subject: [PATCH] feat: update medias --- backend/app/http/admin/medias.go | 6 ++++-- backend/app/http/admin/routes.gen.go | 3 ++- backend/app/models/medias.go | 21 +++++++++++++++++++-- backend/app/models/medias_test.go | 6 +++--- backend/app/models/posts.go | 2 -- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/backend/app/http/admin/medias.go b/backend/app/http/admin/medias.go index cc66d26..28ad3b8 100644 --- a/backend/app/http/admin/medias.go +++ b/backend/app/http/admin/medias.go @@ -13,6 +13,8 @@ type medias struct{} // List medias // @Router /v1/admin/medias [get] // @Bind pagination query -func (ctl *medias) List(ctx fiber.Ctx, pagination *requests.Pagination) (*requests.Pager, error) { - return models.Medias.List(ctx.Context(), pagination) +// @Bind query query +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) } diff --git a/backend/app/http/admin/routes.gen.go b/backend/app/http/admin/routes.gen.go index f90b8e0..e7006ca 100644 --- a/backend/app/http/admin/routes.gen.go +++ b/backend/app/http/admin/routes.gen.go @@ -32,9 +32,10 @@ func (r *Routes) Name() string { func (r *Routes) Register(router fiber.Router) { // 注册路由组: medias - router.Get("/v1/admin/medias", DataFunc1( + router.Get("/v1/admin/medias", DataFunc2( r.medias.List, Query[requests.Pagination]("pagination"), + Query[ListQuery]("query"), )) // 注册路由组: posts diff --git a/backend/app/models/medias.go b/backend/app/models/medias.go index 3805120..986956e 100644 --- a/backend/app/models/medias.go +++ b/backend/app/models/medias.go @@ -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 diff --git a/backend/app/models/medias_test.go b/backend/app/models/medias_test.go index 27acacd..1ea0a6b 100644 --- a/backend/app/models/medias_test.go +++ b/backend/app/models/medias_test.go @@ -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) diff --git a/backend/app/models/posts.go b/backend/app/models/posts.go index dcffa0f..5fb1e8c 100644 --- a/backend/app/models/posts.go +++ b/backend/app/models/posts.go @@ -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 }