fix: list query issues

This commit is contained in:
yanghao05
2025-04-09 17:25:41 +08:00
parent 1f4c05c54a
commit 05d368ef25
6 changed files with 43 additions and 17 deletions

View File

@@ -9,7 +9,7 @@ import (
)
type ListQuery struct {
Key *string `query:"key"`
Keyword *string `query:"keyword"`
}
// @provider
@@ -20,7 +20,7 @@ type posts struct{}
// @Bind pagination query
// @Bind query query
func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
cond := models.Posts.BuildConditionWithKey(query.Key)
cond := models.Posts.BuildConditionWithKey(query.Keyword)
return models.Posts.List(ctx.Context(), pagination, cond)
}

View File

@@ -9,7 +9,7 @@ import (
)
type ListQuery struct {
Key *string `query:"key"`
keyword *string `query:"keyword"`
}
// @provider
@@ -20,7 +20,7 @@ type posts struct{}
// @Bind pagination query
// @Bind query query
func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
cond := models.Posts.BuildConditionWithKey(query.Key)
cond := models.Posts.BuildConditionWithKey(query.keyword)
return models.Posts.List(ctx.Context(), pagination, cond)
}

View File

@@ -3,6 +3,7 @@ package models
import (
"context"
"errors"
"log"
"time"
"quyun/app/requests"
@@ -34,16 +35,19 @@ func (m *postsModel) BuildConditionWithKey(key *string) BoolExpression {
)
if key == nil || *key == "" {
log.Fatal(*key)
return cond
}
cond = tbl.Title.LIKE(String("%" + *key + "%")).
OR(
tbl.Content.LIKE(String("%" + *key + "%")),
).
OR(
tbl.Description.LIKE(String("%" + *key + "%")),
)
cond = cond.AND(
tbl.Title.LIKE(String("%" + *key + "%")).
OR(
tbl.Content.LIKE(String("%" + *key + "%")),
).
OR(
tbl.Description.LIKE(String("%" + *key + "%")),
),
)
return cond
}
@@ -135,7 +139,7 @@ func (m *postsModel) List(ctx context.Context, pagination *requests.Pagination,
OFFSET(pagination.Offset)
m.log.Infof("sql: %s", stmt.DebugSql())
var posts []model.Posts
var posts []model.Posts = make([]model.Posts, 0)
err := stmt.QueryContext(ctx, db, &posts)
if err != nil {
m.log.Errorf("error querying post items: %v", err)