Skip to content

Commit 8f3ac6d

Browse files
Refactor pagination logic in RestHandler
Improved handling of pagination by introducing checks for the presence of offset and size query parameters. Adjusted logic to conditionally apply pagination based on provided parameters and defaults. This enhances clarity and ensures more robust request handling.
1 parent cf9da27 commit 8f3ac6d

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

api/RestHandler.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,18 @@ func (impl *RestHandlerImpl) GetReleases(w http.ResponseWriter, r *http.Request)
121121
size := 10
122122
var err error
123123
offsetQueryParam := r.URL.Query().Get("offset")
124-
if len(offsetQueryParam) > 0 {
124+
sizeQueryParam := r.URL.Query().Get("size")
125+
hasOffsetParam := len(offsetQueryParam) > 0
126+
hasSizeParam := len(sizeQueryParam) > 0
127+
128+
if hasOffsetParam {
125129
offset, err = strconv.Atoi(offsetQueryParam)
126130
if err != nil {
127131
impl.WriteJsonResp(w, err, "invalid offset", http.StatusBadRequest)
128132
return
129133
}
130134
}
131-
sizeQueryParam := r.URL.Query().Get("size")
132-
if len(sizeQueryParam) > 0 {
135+
if hasSizeParam {
133136
size, err = strconv.Atoi(sizeQueryParam)
134137
if err != nil {
135138
impl.WriteJsonResp(w, err, "invalid size", http.StatusBadRequest)
@@ -158,13 +161,23 @@ func (impl *RestHandlerImpl) GetReleases(w http.ResponseWriter, r *http.Request)
158161
}
159162
}
160163
response = filteredResponse
161-
}
162164

163-
if size > 0 {
164-
if offset+size <= len(response) {
165-
response = response[offset : offset+size]
166-
} else {
167-
response = response[offset:]
165+
// If serverVersion is provided, only apply pagination if size are explicitly provided
166+
if hasSizeParam && size > 0 {
167+
if offset+size <= len(response) {
168+
response = response[offset : offset+size]
169+
} else {
170+
response = response[offset:]
171+
}
172+
}
173+
} else {
174+
// If serverVersion is not provided, apply pagination with default or provided values
175+
if size > 0 {
176+
if offset+size <= len(response) {
177+
response = response[offset : offset+size]
178+
} else {
179+
response = response[offset:]
180+
}
168181
}
169182
}
170183
if len(response) == 0 {

0 commit comments

Comments
 (0)