Skip to content

Commit 9956dc2

Browse files
committed
feat: 增加文本搜索条件
1 parent 0cb7802 commit 9956dc2

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

api/sentence/v1/sentence.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ type DelRes struct {
4848
type ListReq struct {
4949
g.Meta `path:"sentence/list" method:"get" sm:"查询列表" tags:"句子"`
5050
*model.Paging
51-
BookId model.Id `json:"book_id" dc:"和tag_ids二选一"`
52-
TagIds []model.Id `json:"tag_ids" dc:"和book_id二选一"`
51+
BookId model.Id `json:"book_id" dc:"书籍id"`
52+
TagIds []model.Id `json:"tag_ids" dc:"标签id"`
53+
Search string `json:"search" dc:"搜索文本"`
5354
}
5455

5556
type ListRes struct {

internal/controller/sentence/sentence_v1_list.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ func (c *ControllerV1) List(ctx context.Context, req *v1.ListReq) (res *v1.ListR
2323
total uint
2424
)
2525
if len(req.TagIds) > 0 {
26-
data, total, err = listByTagIds(ctx, req)
26+
data, total, err = c.listByTagIds(ctx, req)
2727
} else {
28-
data, total, err = listByBookId(ctx, req)
28+
data, total, err = c.listByBookId(ctx, req)
2929
}
3030

3131
if err != nil {
@@ -46,7 +46,7 @@ func (c *ControllerV1) List(ctx context.Context, req *v1.ListReq) (res *v1.ListR
4646
}, nil
4747
}
4848

49-
func listByTagIds(ctx context.Context, req *v1.ListReq) (data []entity.Sentence, total uint, err error) {
49+
func (c *ControllerV1) listByTagIds(ctx context.Context, req *v1.ListReq) (data []entity.Sentence, total uint, err error) {
5050
var ids []model.Id
5151
ids, total, err = sentence.GetIdsByTagIds(ctx, req.TagIds, *req.Paging)
5252
if err != nil {
@@ -65,7 +65,7 @@ func listByTagIds(ctx context.Context, req *v1.ListReq) (data []entity.Sentence,
6565
return
6666
}
6767

68-
func listByBookId(ctx context.Context, req *v1.ListReq) (data []entity.Sentence, total uint, err error) {
68+
func (c *ControllerV1) listByBookId(ctx context.Context, req *v1.ListReq) (data []entity.Sentence, total uint, err error) {
6969
query := &model.SentenceQuery{
7070
Paging: *req.Paging,
7171
BookId: req.BookId,
@@ -74,3 +74,13 @@ func listByBookId(ctx context.Context, req *v1.ListReq) (data []entity.Sentence,
7474
data, total, err = sentence.List(ctx, query)
7575
return
7676
}
77+
78+
func (c *ControllerV1) listBySearch(ctx context.Context, req *v1.ListReq) (data []entity.Sentence, total uint, err error) {
79+
query := &model.SentenceQuery{
80+
Paging: *req.Paging,
81+
Search: req.Search,
82+
}
83+
84+
data, total, err = sentence.List(ctx, query)
85+
return
86+
}

internal/logic/sentence/sentence.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ func List(ctx context.Context, query *model.SentenceQuery) (list []entity.Senten
120120
if len(query.Ids) > 0 {
121121
db = db.WhereIn("id", query.Ids)
122122
}
123+
if query.Search != "" {
124+
db = db.Where("sentence like ?", "%"+query.Search+"%")
125+
}
123126
data, totalInt, err := db.Page(query.Page, query.Size).AllAndCount(true)
124127

125128
if err != nil {

internal/model/sentence.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ type SentenceQuery struct {
44
Paging
55
BookId Id
66
// 根据指定id查询
7-
Ids []Id
7+
Ids []Id
8+
Search string
89
}
910

1011
type SentenceInput struct {

0 commit comments

Comments
 (0)