Skip to content

Commit 1143e9e

Browse files
authored
Hide last link when needed, add first link (#532)
* No lastLink on last page * Lint * Copilot * Out of offset * Add firstLink
1 parent 6f680d1 commit 1143e9e

3 files changed

Lines changed: 98 additions & 8 deletions

File tree

broker/api/common.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,35 @@ func CollectAboutData(fullCount int64, offset int32, limit int32, r *http.Reques
5757
}
5858
limit64 := int64(limit)
5959
offset64 := int64(offset)
60+
lastOffset := int64(0)
61+
if fullCount > 0 {
62+
lastOffset = ((fullCount - 1) / limit64) * limit64
63+
}
6064
if fullCount > limit64 {
61-
lastOffset := ((fullCount - 1) / limit64) * limit64
62-
urlValues := r.URL.Query()
63-
urlValues["offset"] = []string{strconv.FormatInt(lastOffset, 10)}
64-
link := ToLinkUrlValues(r, urlValues)
65-
about.LastLink = &link
65+
if offset64 != lastOffset {
66+
urlValues := r.URL.Query()
67+
urlValues["offset"] = []string{strconv.FormatInt(lastOffset, 10)}
68+
link := ToLinkUrlValues(r, urlValues)
69+
about.LastLink = &link
70+
}
6671
}
6772
if offset64 > 0 {
73+
urlValues := r.URL.Query()
74+
urlValues["offset"] = []string{"0"}
75+
firstLink := ToLinkUrlValues(r, urlValues)
76+
about.FirstLink = &firstLink
77+
6878
pOffset := offset64 - limit64
6979
if pOffset < 0 {
7080
pOffset = 0
7181
}
72-
urlValues := r.URL.Query()
82+
if pOffset > lastOffset {
83+
pOffset = lastOffset
84+
}
85+
urlValues = r.URL.Query()
7386
urlValues["offset"] = []string{strconv.FormatInt(pOffset, 10)}
74-
link := ToLinkUrlValues(r, urlValues)
75-
about.PrevLink = &link
87+
prevLink := ToLinkUrlValues(r, urlValues)
88+
about.PrevLink = &prevLink
7689
}
7790
if fullCount > offset64+limit64 {
7891
noffset := offset64 + limit64

broker/api/common_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"net/http"
5+
"net/http/httptest"
56
"strings"
67
"testing"
78

@@ -25,3 +26,76 @@ func TestGetSymbolForRequest(t *testing.T) {
2526
assert.Equal(t, "tenant mapping must be specified", err.Error())
2627
assert.Equal(t, "", resolved)
2728
}
29+
30+
func TestCollectAboutDataLastLink(t *testing.T) {
31+
reqOffset0 := httptest.NewRequest("GET", "http://localhost/ill_transactions?symbol=ISIL:DK-BIB1&offset=0", nil)
32+
reqOffset10 := httptest.NewRequest("GET", "http://localhost/ill_transactions?symbol=ISIL:DK-BIB1&offset=10", nil)
33+
reqOffset20 := httptest.NewRequest("GET", "http://localhost/ill_transactions?symbol=ISIL:DK-BIB1&offset=20", nil)
34+
reqOffset1000 := httptest.NewRequest("GET", "http://localhost/ill_transactions?symbol=ISIL:DK-BIB1&offset=1000", nil)
35+
36+
// First page (count=21, limit=10, offset=0): prevLink omitted, next/last present.
37+
about := CollectAboutData(21, 0, 10, reqOffset0)
38+
assert.Equal(t, int64(21), about.Count)
39+
assert.Nil(t, about.FirstLink)
40+
assert.Nil(t, about.PrevLink)
41+
assert.NotNil(t, about.NextLink)
42+
assert.Contains(t, *about.NextLink, "offset=10")
43+
assert.Contains(t, *about.NextLink, "symbol=ISIL%3ADK-BIB1")
44+
assert.NotNil(t, about.LastLink)
45+
assert.Contains(t, *about.LastLink, "offset=20")
46+
assert.Contains(t, *about.LastLink, "symbol=ISIL%3ADK-BIB1")
47+
48+
// Not last page (count=21, limit=10, offset=10): all links present
49+
about = CollectAboutData(21, 10, 10, reqOffset10)
50+
assert.Equal(t, int64(21), about.Count)
51+
assert.NotNil(t, about.FirstLink)
52+
assert.Contains(t, *about.FirstLink, "offset=0")
53+
assert.Contains(t, *about.FirstLink, "symbol=ISIL%3ADK-BIB1")
54+
assert.NotNil(t, about.PrevLink)
55+
assert.Contains(t, *about.PrevLink, "offset=0")
56+
assert.Contains(t, *about.PrevLink, "symbol=ISIL%3ADK-BIB1")
57+
assert.NotNil(t, about.NextLink)
58+
assert.Contains(t, *about.NextLink, "offset=20")
59+
assert.Contains(t, *about.NextLink, "symbol=ISIL%3ADK-BIB1")
60+
assert.NotNil(t, about.LastLink)
61+
assert.Contains(t, *about.LastLink, "offset=20")
62+
assert.Contains(t, *about.LastLink, "symbol=ISIL%3ADK-BIB1")
63+
64+
// Last page (count=20, limit=10, offset=10): lastLink and nextLink should be omitted.
65+
about = CollectAboutData(20, 10, 10, reqOffset10)
66+
assert.Equal(t, int64(20), about.Count)
67+
assert.NotNil(t, about.FirstLink)
68+
assert.Contains(t, *about.FirstLink, "offset=0")
69+
assert.Contains(t, *about.FirstLink, "symbol=ISIL%3ADK-BIB1")
70+
assert.NotNil(t, about.PrevLink)
71+
assert.Contains(t, *about.PrevLink, "offset=0")
72+
assert.Contains(t, *about.PrevLink, "symbol=ISIL%3ADK-BIB1")
73+
assert.Nil(t, about.NextLink)
74+
assert.Nil(t, about.LastLink)
75+
76+
// Last partial page (count=21, limit=10, offset=20): lastLink and nextLink should be omitted.
77+
about = CollectAboutData(21, 20, 10, reqOffset20)
78+
assert.Equal(t, int64(21), about.Count)
79+
assert.NotNil(t, about.FirstLink)
80+
assert.Contains(t, *about.FirstLink, "offset=0")
81+
assert.Contains(t, *about.FirstLink, "symbol=ISIL%3ADK-BIB1")
82+
assert.NotNil(t, about.PrevLink)
83+
assert.Contains(t, *about.PrevLink, "offset=10")
84+
assert.Contains(t, *about.PrevLink, "symbol=ISIL%3ADK-BIB1")
85+
assert.Nil(t, about.NextLink)
86+
assert.Nil(t, about.LastLink)
87+
88+
// Out-of-range page (count=21, limit=10, offset=1000): prevLink and lastLink should be present.
89+
about = CollectAboutData(21, 1000, 10, reqOffset1000)
90+
assert.Equal(t, int64(21), about.Count)
91+
assert.NotNil(t, about.FirstLink)
92+
assert.Contains(t, *about.FirstLink, "offset=0")
93+
assert.Contains(t, *about.FirstLink, "symbol=ISIL%3ADK-BIB1")
94+
assert.NotNil(t, about.PrevLink)
95+
assert.Contains(t, *about.PrevLink, "offset=20")
96+
assert.Contains(t, *about.PrevLink, "symbol=ISIL%3ADK-BIB1")
97+
assert.Nil(t, about.NextLink)
98+
assert.NotNil(t, about.LastLink)
99+
assert.Contains(t, *about.LastLink, "offset=20")
100+
assert.Contains(t, *about.LastLink, "symbol=ISIL%3ADK-BIB1")
101+
}

broker/oapi/open-api.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ components:
212212
prevLink:
213213
type: string
214214
description: Link to the previous page of results
215+
firstLink:
216+
type: string
217+
description: Link to the first page of results
215218
lastLink:
216219
type: string
217220
description: Link to the last page of results

0 commit comments

Comments
 (0)