Skip to content

Commit 7682e8c

Browse files
authored
feat: add sort options in runs and projects list api (#1381)
* feat: add sort options in runs and projects list api * feat: add sort options in runs and projects list api * feat: add sort options in runs and projects list api
1 parent a062c84 commit 7682e8c

File tree

20 files changed

+206
-42
lines changed

20 files changed

+206
-42
lines changed

api/openapispec/docs.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,18 @@ const docTemplate = `{
10461046
"description": "The size of the page. Default to 10",
10471047
"name": "pageSize",
10481048
"in": "query"
1049+
},
1050+
{
1051+
"type": "string",
1052+
"description": "Which field to sort the list by. Default to id",
1053+
"name": "sortBy",
1054+
"in": "query"
1055+
},
1056+
{
1057+
"type": "boolean",
1058+
"description": "Whether to sort the list in ascending order. Default to false",
1059+
"name": "ascending",
1060+
"in": "query"
10491061
}
10501062
],
10511063
"responses": {
@@ -1649,6 +1661,18 @@ const docTemplate = `{
16491661
"description": "The size of the page. Default to 10",
16501662
"name": "pageSize",
16511663
"in": "query"
1664+
},
1665+
{
1666+
"type": "string",
1667+
"description": "Which field to sort the list by. Default to id",
1668+
"name": "sortBy",
1669+
"in": "query"
1670+
},
1671+
{
1672+
"type": "boolean",
1673+
"description": "Whether to sort the list in ascending order. Default to false",
1674+
"name": "ascending",
1675+
"in": "query"
16521676
}
16531677
],
16541678
"responses": {
@@ -3866,18 +3890,18 @@ const docTemplate = `{
38663890
"constant.SourceProviderType": {
38673891
"type": "string",
38683892
"enum": [
3869-
"git",
38703893
"git",
38713894
"github",
38723895
"oci",
3873-
"local"
3896+
"local",
3897+
"git"
38743898
],
38753899
"x-enum-varnames": [
3876-
"DefaultSourceType",
38773900
"SourceProviderTypeGit",
38783901
"SourceProviderTypeGithub",
38793902
"SourceProviderTypeOCI",
3880-
"SourceProviderTypeLocal"
3903+
"SourceProviderTypeLocal",
3904+
"DefaultSourceType"
38813905
]
38823906
},
38833907
"constant.StackState": {
@@ -4277,6 +4301,10 @@ const docTemplate = `{
42774301
"description": "ResourceType is the type of the resource.",
42784302
"type": "string"
42794303
},
4304+
"resourceURN": {
4305+
"description": "ResourceURN is the urn of the resource.",
4306+
"type": "string"
4307+
},
42804308
"status": {
42814309
"description": "Status is the status of the resource.",
42824310
"type": "string"

api/openapispec/swagger.json

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,18 @@
10351035
"description": "The size of the page. Default to 10",
10361036
"name": "pageSize",
10371037
"in": "query"
1038+
},
1039+
{
1040+
"type": "string",
1041+
"description": "Which field to sort the list by. Default to id",
1042+
"name": "sortBy",
1043+
"in": "query"
1044+
},
1045+
{
1046+
"type": "boolean",
1047+
"description": "Whether to sort the list in ascending order. Default to false",
1048+
"name": "ascending",
1049+
"in": "query"
10381050
}
10391051
],
10401052
"responses": {
@@ -1638,6 +1650,18 @@
16381650
"description": "The size of the page. Default to 10",
16391651
"name": "pageSize",
16401652
"in": "query"
1653+
},
1654+
{
1655+
"type": "string",
1656+
"description": "Which field to sort the list by. Default to id",
1657+
"name": "sortBy",
1658+
"in": "query"
1659+
},
1660+
{
1661+
"type": "boolean",
1662+
"description": "Whether to sort the list in ascending order. Default to false",
1663+
"name": "ascending",
1664+
"in": "query"
16411665
}
16421666
],
16431667
"responses": {
@@ -3855,18 +3879,18 @@
38553879
"constant.SourceProviderType": {
38563880
"type": "string",
38573881
"enum": [
3858-
"git",
38593882
"git",
38603883
"github",
38613884
"oci",
3862-
"local"
3885+
"local",
3886+
"git"
38633887
],
38643888
"x-enum-varnames": [
3865-
"DefaultSourceType",
38663889
"SourceProviderTypeGit",
38673890
"SourceProviderTypeGithub",
38683891
"SourceProviderTypeOCI",
3869-
"SourceProviderTypeLocal"
3892+
"SourceProviderTypeLocal",
3893+
"DefaultSourceType"
38703894
]
38713895
},
38723896
"constant.StackState": {
@@ -4266,6 +4290,10 @@
42664290
"description": "ResourceType is the type of the resource.",
42674291
"type": "string"
42684292
},
4293+
"resourceURN": {
4294+
"description": "ResourceURN is the urn of the resource.",
4295+
"type": "string"
4296+
},
42694297
"status": {
42704298
"description": "Status is the status of the resource.",
42714299
"type": "string"

api/openapispec/swagger.yaml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ definitions:
3030
constant.SourceProviderType:
3131
enum:
3232
- git
33-
- git
3433
- github
3534
- oci
3635
- local
36+
- git
3737
type: string
3838
x-enum-varnames:
39-
- DefaultSourceType
4039
- SourceProviderTypeGit
4140
- SourceProviderTypeGithub
4241
- SourceProviderTypeOCI
4342
- SourceProviderTypeLocal
43+
- DefaultSourceType
4444
constant.StackState:
4545
enum:
4646
- UnSynced
@@ -325,6 +325,9 @@ definitions:
325325
resourceType:
326326
description: ResourceType is the type of the resource.
327327
type: string
328+
resourceURN:
329+
description: ResourceURN is the urn of the resource.
330+
type: string
328331
status:
329332
description: Status is the status of the resource.
330333
type: string
@@ -2086,6 +2089,14 @@ paths:
20862089
in: query
20872090
name: pageSize
20882091
type: integer
2092+
- description: Which field to sort the list by. Default to id
2093+
in: query
2094+
name: sortBy
2095+
type: string
2096+
- description: Whether to sort the list in ascending order. Default to false
2097+
in: query
2098+
name: ascending
2099+
type: boolean
20892100
produces:
20902101
- application/json
20912102
responses:
@@ -2478,6 +2489,14 @@ paths:
24782489
in: query
24792490
name: pageSize
24802491
type: integer
2492+
- description: Which field to sort the list by. Default to id
2493+
in: query
2494+
name: sortBy
2495+
type: string
2496+
- description: Whether to sort the list in ascending order. Default to false
2497+
in: query
2498+
name: ascending
2499+
type: boolean
24812500
produces:
24822501
- application/json
24832502
responses:

pkg/domain/constant/global.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ const (
2727
ResourcePageSizeLarge = 1000
2828
CommonPageDefault = 1
2929
CommonPageSizeDefault = 10
30+
SortByCreateTimestamp = "createTimestamp"
31+
SortByModifiedTimestamp = "modifiedTimestamp"
32+
SortByName = "name"
33+
SortByID = "id"
3034
)
3135

3236
var (

pkg/domain/entity/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ type Pagination struct {
44
Page int `json:"page"`
55
PageSize int `json:"pageSize"`
66
}
7+
8+
type SortOptions struct {
9+
Field string
10+
Ascending bool
11+
}

pkg/domain/repository/repository.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type ProjectRepository interface {
3737
// GetByName retrieves a project by its name.
3838
GetByName(ctx context.Context, name string) (*entity.Project, error)
3939
// List retrieves all existing projects.
40-
List(ctx context.Context, filter *entity.ProjectFilter) (*entity.ProjectListResult, error)
40+
List(ctx context.Context, filter *entity.ProjectFilter, sortOptions *entity.SortOptions) (*entity.ProjectListResult, error)
4141
}
4242

4343
// StackRepository is an interface that defines the repository operations
@@ -150,5 +150,5 @@ type RunRepository interface {
150150
// Get retrieves a run by its ID.
151151
Get(ctx context.Context, id uint) (*entity.Run, error)
152152
// List retrieves all existing run.
153-
List(ctx context.Context, filter *entity.RunFilter) (*entity.RunListResult, error)
153+
List(ctx context.Context, filter *entity.RunFilter, sortOptions *entity.SortOptions) (*entity.RunListResult, error)
154154
}

pkg/infra/persistence/project.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,20 @@ func (r *projectRepository) GetByName(ctx context.Context, name string) (*entity
110110
}
111111

112112
// List retrieves all projects.
113-
func (r *projectRepository) List(ctx context.Context, filter *entity.ProjectFilter) (*entity.ProjectListResult, error) {
113+
func (r *projectRepository) List(ctx context.Context, filter *entity.ProjectFilter, sortOptions *entity.SortOptions) (*entity.ProjectListResult, error) {
114114
var dataModel []ProjectModel
115115
projectEntityList := make([]*entity.Project, 0)
116116
pattern, args := GetProjectQuery(filter)
117+
118+
sortArgs := sortOptions.Field
119+
if !sortOptions.Ascending {
120+
sortArgs += " DESC"
121+
}
122+
117123
searchResult := r.db.WithContext(ctx).
118124
Preload("Source").
119125
Preload("Organization").
126+
Order(sortArgs).
120127
Where(pattern, args...)
121128

122129
// Get total rows

pkg/infra/persistence/project_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func TestProjectRepository(t *testing.T) {
162162
sqlmock.NewRows([]string{"count"}).
163163
AddRow(2))
164164

165-
sqlMock.ExpectQuery("SELECT .* FROM `project` .* IS NULL LIMIT").
165+
sqlMock.ExpectQuery("SELECT .* FROM `project` .* IS NULL .* LIMIT").
166166
WillReturnRows(
167167
sqlmock.NewRows([]string{"id", "name", "path", "Organization__id", "Organization__name", "Organization__owners", "Source__id", "Source__remote", "Source__source_provider"}).
168168
AddRow(expectedID, expectedName, expectedPath, 1, "mockedOrg", expectedOrgOwners, 1, "https://github.com/test/repo", constant.SourceProviderTypeGithub).
@@ -173,6 +173,8 @@ func TestProjectRepository(t *testing.T) {
173173
Page: constant.CommonPageDefault,
174174
PageSize: constant.CommonPageSizeDefault,
175175
},
176+
}, &entity.SortOptions{
177+
Field: constant.SortByID,
176178
})
177179
require.NoError(t, err)
178180
require.Len(t, actual.Projects, 2)

pkg/infra/persistence/run.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,22 @@ func (r *runRepository) Get(ctx context.Context, id uint) (*entity.Run, error) {
9696
}
9797

9898
// List retrieves all runs.
99-
func (r *runRepository) List(ctx context.Context, filter *entity.RunFilter) (*entity.RunListResult, error) {
99+
func (r *runRepository) List(ctx context.Context, filter *entity.RunFilter, sortOptions *entity.SortOptions) (*entity.RunListResult, error) {
100100
var dataModel []RunModel
101101
runEntityList := make([]*entity.Run, 0)
102102
pattern, args := GetRunQuery(filter)
103+
104+
sortArgs := sortOptions.Field
105+
if !sortOptions.Ascending {
106+
sortArgs += " DESC"
107+
}
108+
103109
searchResult := r.db.WithContext(ctx).
104110
Preload("Stack").Preload("Stack.Project").
105111
Joins("JOIN stack ON stack.id = run.stack_id").
106112
Joins("JOIN project ON project.id = stack.project_id").
107113
Joins("JOIN workspace ON workspace.name = run.workspace").
114+
Order(sortArgs).
108115
Where(pattern, args...)
109116

110117
// Get total rows

pkg/server/handler/module/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (h *Handler) DeleteModule() http.HandlerFunc {
9797
// @Failure 429 {object} error "Too Many Requests"
9898
// @Failure 404 {object} error "Not Found"
9999
// @Failure 500 {object} error "Internal Server Error"
100-
// @Router /api/v1/modules/{moduleName} [put]
100+
// @Router /api/v1/modules/{moduleName} [put]
101101
func (h *Handler) UpdateModule() http.HandlerFunc {
102102
return func(w http.ResponseWriter, r *http.Request) {
103103
// Getting stuff from context.
@@ -139,7 +139,7 @@ func (h *Handler) UpdateModule() http.HandlerFunc {
139139
// @Failure 429 {object} error "Too Many Requests"
140140
// @Failure 404 {object} error "Not Found"
141141
// @Failure 500 {object} error "Internal Server Error"
142-
// @Router /api/v1/modules/{moduleName} [get]
142+
// @Router /api/v1/modules/{moduleName} [get]
143143
func (h *Handler) GetModule() http.HandlerFunc {
144144
return func(w http.ResponseWriter, r *http.Request) {
145145
// Getting stuff from context.

0 commit comments

Comments
 (0)