Skip to content

Commit

Permalink
fix(tibuild): fix sql
Browse files Browse the repository at this point in the history
Signed-off-by: wuhuizuo <wuhuizuo@126.com>
  • Loading branch information
wuhuizuo committed May 21, 2024
1 parent c4a966c commit 8b7b398
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
10 changes: 5 additions & 5 deletions tibuild/pkg/rest/repo/dev_build_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@ func (m DevBuildRepo) Update(ctx context.Context, id int, req DevBuild) (resp *D
return &req, nil
}

func (m DevBuildRepo) List(ctx context.Context, option DevBuildListOption) (resp []DevBuild, err error) {
func (m DevBuildRepo) List(ctx context.Context, option DevBuildListOption) (resp []*DevBuild, err error) {
db := m.Db.Order("created_at DESC").Offset(int(option.Offset)).Limit(int(option.Size))
if option.Hotfix != nil {
db = db.Where(&DevBuild{Spec: DevBuildSpec{IsHotfix: *option.Hotfix}}, *option.Hotfix)
db = db.Where(&DevBuild{Spec: DevBuildSpec{IsHotfix: *option.Hotfix}}, "IsHotfix")
}
if option.CreatedBy != nil && *option.CreatedBy != "" {
db = db.Where(&DevBuild{Meta: DevBuildMeta{CreatedBy: *option.CreatedBy}}, *option.CreatedBy)
db = db.Where(&DevBuild{Meta: DevBuildMeta{CreatedBy: *option.CreatedBy}}, "CreatedBy")
}

result := []DevBuild{}
result := []*DevBuild{}
if err := db.Find(&result).Error; err != nil {
return nil, fmt.Errorf("%s%w", err.Error(), ErrInternalError)
}

for i := range result {
err = outofDB(&result[i])
err = outofDB(result[i])
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion tibuild/pkg/rest/repo/dev_build_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func TestDevBuildList(t *testing.T) {
require.NoError(t, err)
repo := DevBuildRepo{Db: odb}
rows := sqlmock.NewRows([]string{"id"}).AddRow(1).AddRow(2)
mock.ExpectQuery("SELECT \\* FROM `dev_builds` WHERE `dev_builds`.`is_hotfix` = \\? ORDER BY created_at DESC LIMIT 10 OFFSET 5").WithArgs(false).WillReturnRows(rows)
mock.ExpectQuery("SELECT \\* FROM `dev_builds` WHERE `dev_builds`.`is_hotfix` = \\? ORDER BY created_at DESC LIMIT \\? OFFSET \\?").
WithArgs(false, 10, 5).WillReturnRows(rows)
entities, err := repo.List(context.TODO(), DevBuildListOption{Offset: 5, Size: 10, Hotfix: &[]bool{false}[0]})
require.NoError(t, err)
require.Equal(t, 2, len(entities))
Expand Down

0 comments on commit 8b7b398

Please sign in to comment.