Skip to content

Commit 92efcbc

Browse files
committed
add sorting
1 parent 57a031d commit 92efcbc

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed

apps/workspace-engine/pkg/server/openapi/deployments/server.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,27 @@ func (s *Deployments) GetDeploymentResources(c *gin.Context, workspaceId string,
5353
resourceList = append(resourceList, resource)
5454
}
5555

56+
// Sort the resourceList by resource.Name (nil-safe, ascending); if Name is the same, sort by CreatedAt
57+
sort.Slice(resourceList, func(i, j int) bool {
58+
if resourceList[i] == nil && resourceList[j] == nil {
59+
return false
60+
}
61+
if resourceList[i] == nil {
62+
return false
63+
}
64+
if resourceList[j] == nil {
65+
return true
66+
}
67+
if resourceList[i].Name < resourceList[j].Name {
68+
return true
69+
}
70+
if resourceList[i].Name > resourceList[j].Name {
71+
return false
72+
}
73+
// Names are equal; compare CreatedAt
74+
return resourceList[i].CreatedAt.Before(resourceList[j].CreatedAt)
75+
})
76+
5677
// Get pagination parameters with defaults
5778
limit := 50
5879
if params.Limit != nil {
@@ -110,6 +131,27 @@ func (s *Deployments) ListDeployments(c *gin.Context, workspaceId string, params
110131
deploymentsList = append(deploymentsList, deployment)
111132
}
112133

134+
// Sort the deploymentsList by deployment.Name (nil-safe, ascending); if Name is the same, sort by CreatedAt
135+
sort.Slice(deploymentsList, func(i, j int) bool {
136+
if deploymentsList[i] == nil && deploymentsList[j] == nil {
137+
return false
138+
}
139+
if deploymentsList[i] == nil {
140+
return false
141+
}
142+
if deploymentsList[j] == nil {
143+
return true
144+
}
145+
if deploymentsList[i].Name < deploymentsList[j].Name {
146+
return true
147+
}
148+
if deploymentsList[i].Name > deploymentsList[j].Name {
149+
return false
150+
}
151+
// Names are equal; compare CreatedAt
152+
return deploymentsList[i].Id < deploymentsList[j].Id
153+
})
154+
113155
deploymentsWithSystem := make([]*oapi.DeploymentAndSystem, 0, total)
114156
for _, deployment := range deploymentsList[start:end] {
115157
system, ok := ws.Systems().Get(deployment.SystemId)

apps/workspace-engine/pkg/server/openapi/environments/server.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package environments
22

33
import (
44
"net/http"
5+
"sort"
56

67
"github.com/gin-gonic/gin"
78

@@ -60,6 +61,26 @@ func (s *Environments) ListEnvironments(c *gin.Context, workspaceId string, para
6061
environmentsList = append(environmentsList, environment)
6162
}
6263

64+
sort.Slice(environmentsList, func(i, j int) bool {
65+
if environmentsList[i] == nil && environmentsList[j] == nil {
66+
return false
67+
}
68+
if environmentsList[i] == nil {
69+
return false
70+
}
71+
if environmentsList[j] == nil {
72+
return true
73+
}
74+
if environmentsList[i].Name < environmentsList[j].Name {
75+
return true
76+
}
77+
if environmentsList[i].Name > environmentsList[j].Name {
78+
return false
79+
}
80+
// Names are equal; compare Id
81+
return environmentsList[i].Id < environmentsList[j].Id
82+
})
83+
6384
c.JSON(http.StatusOK, gin.H{
6485
"total": total,
6586
"offset": offset,
@@ -90,6 +111,26 @@ func (s *Environments) GetEnvironmentResources(c *gin.Context, workspaceId strin
90111
resourceList = append(resourceList, resource)
91112
}
92113

114+
sort.Slice(resourceList, func(i, j int) bool {
115+
if resourceList[i] == nil && resourceList[j] == nil {
116+
return false
117+
}
118+
if resourceList[i] == nil {
119+
return false
120+
}
121+
if resourceList[j] == nil {
122+
return true
123+
}
124+
if resourceList[i].Name < resourceList[j].Name {
125+
return true
126+
}
127+
if resourceList[i].Name > resourceList[j].Name {
128+
return false
129+
}
130+
// Names are equal; compare Id
131+
return resourceList[i].Id < resourceList[j].Id
132+
})
133+
93134
// Get pagination parameters with defaults
94135
limit := 50
95136
if params.Limit != nil {
@@ -103,14 +144,8 @@ func (s *Environments) GetEnvironmentResources(c *gin.Context, workspaceId strin
103144
total := len(resourceList)
104145

105146
// Apply pagination
106-
start := offset
107-
if start > total {
108-
start = total
109-
}
110-
end := start + limit
111-
if end > total {
112-
end = total
113-
}
147+
start := min(offset, total)
148+
end := min(start + limit, total)
114149
paginatedResources := resourceList[start:end]
115150

116151
c.JSON(http.StatusOK, gin.H{

0 commit comments

Comments
 (0)