Skip to content

Commit 2fdb7c6

Browse files
committed
feat(common): toolbox config list filter
1 parent 5934920 commit 2fdb7c6

11 files changed

Lines changed: 286 additions & 21 deletions

File tree

api/handler/api/common_service.go

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/handler/api/common_service_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ func TestListToolboxConfigs(t *testing.T) {
306306
mockResp []*model.ToolboxConfigDetail
307307
mockTotal int64
308308
mockErr error
309+
expectToolID *int64
310+
expectStudent *string
311+
expectPlatform *string
312+
expectVersion *int64
309313
expectContains []string
310314
}
311315

@@ -329,6 +333,16 @@ func TestListToolboxConfigs(t *testing.T) {
329333
`"student_id":"102300217"`,
330334
},
331335
},
336+
{
337+
name: "success_with_filters",
338+
url: "/api/v1/toolbox/configs?secret=abc&tool_id=1&student_id=102300217&platform=android&version=2",
339+
expectToolID: new(int64(1)),
340+
expectStudent: new("102300217"),
341+
expectPlatform: new("android"),
342+
expectVersion: new(int64(2)),
343+
mockResp: []*model.ToolboxConfigDetail{},
344+
expectContains: []string{`"config":[]`, `"total":0`},
345+
},
332346
{
333347
name: "rpc error",
334348
url: "/api/v1/toolbox/configs?secret=abc&page_num=1&page_size=20",
@@ -364,6 +378,10 @@ func TestListToolboxConfigs(t *testing.T) {
364378
for _, tc := range testCases {
365379
mockey.PatchConvey(tc.name, t, func() {
366380
mockey.Mock(rpc.ListToolboxConfigsRPC).To(func(ctx context.Context, req *common.ListToolboxConfigsRequest) ([]*model.ToolboxConfigDetail, int64, error) {
381+
assert.Equal(t, tc.expectToolID, req.ToolId)
382+
assert.Equal(t, tc.expectStudent, req.StudentId)
383+
assert.Equal(t, tc.expectPlatform, req.Platform)
384+
assert.Equal(t, tc.expectVersion, req.Version)
367385
return tc.mockResp, tc.mockTotal, tc.mockErr
368386
}).Build()
369387

api/model/api/api.go

Lines changed: 59 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

idl/api.thrift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,10 @@ struct ListToolboxConfigsRequest {
729729
1: required string secret
730730
2: optional i64 page_num
731731
3: optional i64 page_size
732+
4: optional i64 tool_id
733+
5: optional string student_id
734+
6: optional string platform
735+
7: optional i64 version
732736
}
733737

734738
struct ListToolboxConfigsResponse {

idl/common.thrift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ struct ListToolboxConfigsRequest {
100100
1: required string secret
101101
2: optional i64 page_num
102102
3: optional i64 page_size
103+
4: optional i64 tool_id
104+
5: optional string student_id
105+
6: optional string platform
106+
7: optional i64 version
103107
}
104108

105109
struct ListToolboxConfigsResponse {

internal/common/handler.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/west2-online/fzuhelper-server/pkg/base"
2727
"github.com/west2-online/fzuhelper-server/pkg/constants"
2828
"github.com/west2-online/fzuhelper-server/pkg/db/model"
29+
"github.com/west2-online/fzuhelper-server/pkg/db/toolbox"
2930
"github.com/west2-online/fzuhelper-server/pkg/logger"
3031
"github.com/west2-online/fzuhelper-server/pkg/singleflight"
3132
"github.com/west2-online/fzuhelper-server/pkg/taskqueue"
@@ -249,6 +250,12 @@ func (s *CommonServiceImpl) ListToolboxConfigs(ctx context.Context,
249250
req.Secret,
250251
req.GetPageNum(),
251252
req.GetPageSize(),
253+
toolbox.ListToolboxConfigsFilter{
254+
ToolID: req.ToolId,
255+
StudentID: req.StudentId,
256+
Platform: req.Platform,
257+
MinVersion: req.Version,
258+
},
252259
)
253260
if err != nil {
254261
r.Base = base.BuildBaseResp(err)

internal/common/service/list_toolbox_configs.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121

2222
"github.com/west2-online/fzuhelper-server/pkg/db/model"
23+
"github.com/west2-online/fzuhelper-server/pkg/db/toolbox"
2324
"github.com/west2-online/fzuhelper-server/pkg/errno"
2425
)
2526

@@ -46,7 +47,12 @@ func normalizeToolboxConfigListPage(pageNum, pageSize int64) (int, int, error) {
4647
}
4748

4849
// ListToolboxConfigs returns one page of admin-visible toolbox configurations.
49-
func (s *CommonService) ListToolboxConfigs(ctx context.Context, secret string, pageNum, pageSize int64) ([]*model.ToolboxConfig, int64, error) {
50+
func (s *CommonService) ListToolboxConfigs(
51+
ctx context.Context,
52+
secret string,
53+
pageNum, pageSize int64,
54+
filter toolbox.ListToolboxConfigsFilter,
55+
) ([]*model.ToolboxConfig, int64, error) {
5056
if err := validateToolboxAdminSecret(secret); err != nil {
5157
return nil, 0, err
5258
}
@@ -56,7 +62,7 @@ func (s *CommonService) ListToolboxConfigs(ctx context.Context, secret string, p
5662
return nil, 0, err
5763
}
5864

59-
configs, total, err := s.db.Toolbox.ListToolboxConfigs(ctx, normalizedPageNum, normalizedPageSize)
65+
configs, total, err := s.db.Toolbox.ListToolboxConfigs(ctx, normalizedPageNum, normalizedPageSize, filter)
6066
if err != nil {
6167
return nil, 0, err
6268
}

internal/common/service/list_toolbox_configs_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func TestListToolboxConfigs(t *testing.T) {
3838
secret string
3939
pageNum int64
4040
pageSize int64
41+
filter toolbox.ListToolboxConfigsFilter
4142
mockCheckPwd bool
4243
mockDBResult []*model.ToolboxConfig
4344
mockDBTotal int64
@@ -75,6 +76,23 @@ func TestListToolboxConfigs(t *testing.T) {
7576
expectPageNum: 2,
7677
expectPageSize: 2,
7778
},
79+
{
80+
name: "success_with_filters",
81+
secret: "secret",
82+
pageNum: 1,
83+
pageSize: 20,
84+
filter: toolbox.ListToolboxConfigsFilter{
85+
ToolID: new(int64(1)),
86+
StudentID: new("102300217"),
87+
Platform: new("android"),
88+
MinVersion: new(int64(2)),
89+
},
90+
mockCheckPwd: true,
91+
mockDBResult: configs,
92+
mockDBTotal: 1,
93+
expectPageNum: 1,
94+
expectPageSize: 20,
95+
},
7896
{
7997
name: "invalid_secret",
8098
secret: "wrong",
@@ -127,15 +145,16 @@ func TestListToolboxConfigs(t *testing.T) {
127145

128146
mockey.Mock(utils.CheckPwd).Return(tc.mockCheckPwd).Build()
129147
mockey.Mock((*toolbox.DBToolbox).ListToolboxConfigs).To(
130-
func(ctx context.Context, pageNum, pageSize int) ([]*model.ToolboxConfig, int64, error) {
148+
func(ctx context.Context, pageNum, pageSize int, filter toolbox.ListToolboxConfigsFilter) ([]*model.ToolboxConfig, int64, error) {
131149
assert.Equal(t, tc.expectPageNum, pageNum)
132150
assert.Equal(t, tc.expectPageSize, pageSize)
151+
assert.Equal(t, tc.filter, filter)
133152
return tc.mockDBResult, tc.mockDBTotal, tc.mockDBError
134153
},
135154
).Build()
136155

137156
commonService := NewCommonService(context.Background(), mockClientSet, new(taskqueue.BaseTaskQueue))
138-
result, total, err := commonService.ListToolboxConfigs(context.Background(), tc.secret, tc.pageNum, tc.pageSize)
157+
result, total, err := commonService.ListToolboxConfigs(context.Background(), tc.secret, tc.pageNum, tc.pageSize, tc.filter)
139158

140159
if tc.expectError != "" {
141160
assert.ErrorContains(t, err, tc.expectError)

kitex_gen/common/common.go

Lines changed: 71 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)