Skip to content

Commit 9fdcfe8

Browse files
authored
Merge pull request #34 from yokowu/main
feat: 记录列表筛选
2 parents 73d56dd + b8e5011 commit 9fdcfe8

6 files changed

Lines changed: 94 additions & 24 deletions

File tree

backend/docs/swagger.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,24 @@
399399
"summary": "获取对话记录",
400400
"operationId": "list-chat-record",
401401
"parameters": [
402+
{
403+
"type": "string",
404+
"description": "作者",
405+
"name": "author",
406+
"in": "query"
407+
},
408+
{
409+
"type": "boolean",
410+
"description": "是否接受筛选",
411+
"name": "is_accept",
412+
"in": "query"
413+
},
414+
{
415+
"type": "string",
416+
"description": "语言",
417+
"name": "language",
418+
"in": "query"
419+
},
402420
{
403421
"type": "string",
404422
"description": "下一页标识",
@@ -500,6 +518,24 @@
500518
"summary": "获取补全记录",
501519
"operationId": "list-completion-record",
502520
"parameters": [
521+
{
522+
"type": "string",
523+
"description": "作者",
524+
"name": "author",
525+
"in": "query"
526+
},
527+
{
528+
"type": "boolean",
529+
"description": "是否接受筛选",
530+
"name": "is_accept",
531+
"in": "query"
532+
},
533+
{
534+
"type": "string",
535+
"description": "语言",
536+
"name": "language",
537+
"in": "query"
538+
},
503539
{
504540
"type": "string",
505541
"description": "下一页标识",

backend/domain/billing.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,26 @@ import (
1010
)
1111

1212
type BillingUsecase interface {
13-
ListChatRecord(ctx context.Context, page *web.Pagination) (*ListChatRecordResp, error)
14-
ListCompletionRecord(ctx context.Context, page *web.Pagination) (*ListCompletionRecordResp, error)
13+
ListChatRecord(ctx context.Context, req ListRecordReq) (*ListChatRecordResp, error)
14+
ListCompletionRecord(ctx context.Context, req ListRecordReq) (*ListCompletionRecordResp, error)
1515
CompletionInfo(ctx context.Context, id string) (*CompletionInfo, error)
1616
ChatInfo(ctx context.Context, id string) (*ChatInfo, error)
1717
}
1818

1919
type BillingRepo interface {
20-
ListChatRecord(ctx context.Context, page *web.Pagination) (*ListChatRecordResp, error)
21-
ListCompletionRecord(ctx context.Context, page *web.Pagination) (*ListCompletionRecordResp, error)
20+
ListChatRecord(ctx context.Context, req ListRecordReq) (*ListChatRecordResp, error)
21+
ListCompletionRecord(ctx context.Context, req ListRecordReq) (*ListCompletionRecordResp, error)
2222
CompletionInfo(ctx context.Context, id string) (*CompletionInfo, error)
2323
ChatInfo(ctx context.Context, id string) (*ChatInfo, error)
2424
}
2525

26+
type ListRecordReq struct {
27+
*web.Pagination
28+
Author string `json:"author" query:"author"` // 作者
29+
Language string `json:"language" query:"language"` // 语言
30+
IsAccept *bool `json:"is_accept" query:"is_accept"` // 是否接受筛选
31+
}
32+
2633
type ListChatRecordResp struct {
2734
*db.PageInfo
2835

backend/internal/billing/handler/http/v1/billing.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func NewBillingHandler(
2323
g := w.Group("/api/v1/billing")
2424
g.Use(auth.Auth())
2525

26-
g.GET("/chat/record", web.BaseHandler(b.ListChatRecord, web.WithPage()))
27-
g.GET("/completion/record", web.BaseHandler(b.ListCompletionRecord, web.WithPage()))
26+
g.GET("/chat/record", web.BindHandler(b.ListChatRecord, web.WithPage()))
27+
g.GET("/completion/record", web.BindHandler(b.ListCompletionRecord, web.WithPage()))
2828
g.GET("/completion/info", web.BaseHandler(b.CompletionInfo))
2929
g.GET("/chat/info", web.BaseHandler(b.ChatInfo))
3030

@@ -39,11 +39,12 @@ func NewBillingHandler(
3939
// @ID list-chat-record
4040
// @Accept json
4141
// @Produce json
42-
// @Param page query web.Pagination true "分页"
42+
// @Param page query domain.ListRecordReq true "参数"
4343
// @Success 200 {object} web.Resp{data=domain.ListChatRecordResp}
4444
// @Router /api/v1/billing/chat/record [get]
45-
func (h *BillingHandler) ListChatRecord(c *web.Context) error {
46-
records, err := h.usecase.ListChatRecord(c.Request().Context(), c.Page())
45+
func (h *BillingHandler) ListChatRecord(c *web.Context, req domain.ListRecordReq) error {
46+
req.Pagination = c.Page()
47+
records, err := h.usecase.ListChatRecord(c.Request().Context(), req)
4748
if err != nil {
4849
return err
4950
}
@@ -58,11 +59,12 @@ func (h *BillingHandler) ListChatRecord(c *web.Context) error {
5859
// @ID list-completion-record
5960
// @Accept json
6061
// @Produce json
61-
// @Param page query web.Pagination true "分页"
62+
// @Param page query domain.ListRecordReq true "参数"
6263
// @Success 200 {object} web.Resp{data=domain.ListCompletionRecordResp}
6364
// @Router /api/v1/billing/completion/record [get]
64-
func (h *BillingHandler) ListCompletionRecord(c *web.Context) error {
65-
records, err := h.usecase.ListCompletionRecord(c.Request().Context(), c.Page())
65+
func (h *BillingHandler) ListCompletionRecord(c *web.Context, req domain.ListRecordReq) error {
66+
req.Pagination = c.Page()
67+
records, err := h.usecase.ListCompletionRecord(c.Request().Context(), req)
6668
if err != nil {
6769
return err
6870
}

backend/internal/billing/repo/billing.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import (
55

66
"entgo.io/ent/dialect/sql"
77

8-
"github.com/GoYoko/web"
9-
108
"github.com/chaitin/MonkeyCode/backend/consts"
119
"github.com/chaitin/MonkeyCode/backend/db"
1210
"github.com/chaitin/MonkeyCode/backend/db/task"
1311
"github.com/chaitin/MonkeyCode/backend/db/taskrecord"
12+
"github.com/chaitin/MonkeyCode/backend/db/user"
1413
"github.com/chaitin/MonkeyCode/backend/domain"
1514
"github.com/chaitin/MonkeyCode/backend/pkg/cvt"
1615
)
@@ -54,15 +53,17 @@ func (b *BillingRepo) CompletionInfo(ctx context.Context, id string) (*domain.Co
5453
}
5554

5655
// ListChatRecord implements domain.BillingRepo.
57-
func (b *BillingRepo) ListChatRecord(ctx context.Context, page *web.Pagination) (*domain.ListChatRecordResp, error) {
56+
func (b *BillingRepo) ListChatRecord(ctx context.Context, req domain.ListRecordReq) (*domain.ListChatRecordResp, error) {
5857
q := b.db.Task.Query().
5958
WithUser().
6059
WithModel().
6160
WithTaskRecords().
6261
Where(task.ModelType(consts.ModelTypeLLM)).
6362
Order(task.ByCreatedAt(sql.OrderDesc()))
6463

65-
records, p, err := q.Page(ctx, page.Page, page.Size)
64+
filterTask(q, req)
65+
66+
records, p, err := q.Page(ctx, req.Page, req.Size)
6667
if err != nil {
6768
return nil, err
6869
}
@@ -75,14 +76,37 @@ func (b *BillingRepo) ListChatRecord(ctx context.Context, page *web.Pagination)
7576
}, nil
7677
}
7778

79+
func filterTask(q *db.TaskQuery, req domain.ListRecordReq) {
80+
if req.IsAccept != nil {
81+
q.Where(task.IsAccept(*req.IsAccept))
82+
}
83+
84+
if req.Author != "" {
85+
q.Where(task.HasUserWith(func(s *sql.Selector) {
86+
s.Where(sql.Like(s.C(user.FieldUsername), "%"+req.Author+"%"))
87+
}))
88+
}
89+
90+
if req.Language != "" {
91+
q.Where(func(s *sql.Selector) {
92+
s.Where(
93+
sql.Like(s.C(task.FieldProgramLanguage), "%"+req.Language+"%"),
94+
)
95+
})
96+
}
97+
}
98+
7899
// ListCompletionRecord implements domain.BillingRepo.
79-
func (b *BillingRepo) ListCompletionRecord(ctx context.Context, page *web.Pagination) (*domain.ListCompletionRecordResp, error) {
100+
func (b *BillingRepo) ListCompletionRecord(ctx context.Context, req domain.ListRecordReq) (*domain.ListCompletionRecordResp, error) {
80101
q := b.db.Task.Query().
81102
WithUser().
82103
WithModel().
83104
Where(task.ModelType(consts.ModelTypeCoder)).
84105
Order(task.ByCreatedAt(sql.OrderDesc()))
85-
records, p, err := q.Page(ctx, page.Page, page.Size)
106+
107+
filterTask(q, req)
108+
109+
records, p, err := q.Page(ctx, req.Page, req.Size)
86110
if err != nil {
87111
return nil, err
88112
}

backend/internal/billing/usecase/billing.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package usecase
33
import (
44
"context"
55

6-
"github.com/GoYoko/web"
7-
86
"github.com/chaitin/MonkeyCode/backend/domain"
97
)
108

@@ -17,13 +15,13 @@ func NewBillingUsecase(repo domain.BillingRepo) domain.BillingUsecase {
1715
}
1816

1917
// ListChatRecord implements domain.BillingUsecase.
20-
func (b *BillingUsecase) ListChatRecord(ctx context.Context, page *web.Pagination) (*domain.ListChatRecordResp, error) {
21-
return b.repo.ListChatRecord(ctx, page)
18+
func (b *BillingUsecase) ListChatRecord(ctx context.Context, req domain.ListRecordReq) (*domain.ListChatRecordResp, error) {
19+
return b.repo.ListChatRecord(ctx, req)
2220
}
2321

2422
// ListCompletionRecord implements domain.BillingUsecase.
25-
func (b *BillingUsecase) ListCompletionRecord(ctx context.Context, page *web.Pagination) (*domain.ListCompletionRecordResp, error) {
26-
return b.repo.ListCompletionRecord(ctx, page)
23+
func (b *BillingUsecase) ListCompletionRecord(ctx context.Context, req domain.ListRecordReq) (*domain.ListCompletionRecordResp, error) {
24+
return b.repo.ListCompletionRecord(ctx, req)
2725
}
2826

2927
// CompletionInfo implements domain.BillingUsecase.

backend/internal/model/usecase/model.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,8 @@ func (m *ModelUsecase) Update(ctx context.Context, req *domain.UpdateModelReq) (
109109

110110
func (m *ModelUsecase) InitModel(ctx context.Context) error {
111111
m.logger.With("init_model", m.cfg.InitModel).Debug("init model")
112+
if m.cfg.InitModel.ModelName == "" {
113+
return nil
114+
}
112115
return m.repo.InitModel(ctx, m.cfg.InitModel.ModelName, m.cfg.InitModel.ModelKey, m.cfg.InitModel.ModelURL)
113116
}

0 commit comments

Comments
 (0)