Skip to content

Commit 1fe49ff

Browse files
authored
Merge pull request #76 from pgulb/44-flush-pagination-on-pwa-root
44 flush pagination on pwa root
2 parents 5b36c5d + 1d716a1 commit 1fe49ff

File tree

7 files changed

+325
-68
lines changed

7 files changed

+325
-68
lines changed

api/main.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,32 @@ def delete_flush_by_id(
214214
@app.get("/flushes", status_code=status.HTTP_200_OK)
215215
def get_flushes(
216216
export_format: Union[str, None] = None,
217+
skip: Union[int, None] = None,
217218
credentials: HTTPBasicCredentials = Depends(security),
218219
):
219220
check_creds(credentials)
220221
flushes = client.flush.flushes
221222
try:
222-
entries = [
223-
x
224-
for x in flushes.find(
225-
filter={"user_id": credentials.username},
226-
sort=[("time_start", pymongo.DESCENDING)],
227-
)
228-
]
223+
if skip is not None:
224+
entries = [
225+
x
226+
for x in flushes.find(
227+
filter={
228+
"user_id": credentials.username,
229+
},
230+
limit=3,
231+
skip=skip,
232+
sort=[("time_start", pymongo.DESCENDING)],
233+
)
234+
]
235+
else:
236+
entries = [
237+
x
238+
for x in flushes.find(
239+
filter={"user_id": credentials.username},
240+
sort=[("time_start", pymongo.DESCENDING)],
241+
)
242+
]
229243
for entry in entries:
230244
entry["_id"] = str(entry["_id"])
231245
del entry["user_id"]

api/tests/universal/test_getting_flushes.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,81 @@ def test_getting_flushes_noflushes():
8080
)
8181
assert response.status_code == status.HTTP_200_OK
8282
assert response.json() == []
83+
84+
85+
def test_getting_flushes_with_skip():
86+
flushes = [
87+
{
88+
"time_start": "2021-01-01T00:00:00",
89+
"time_end": "2021-01-01T01:00:00",
90+
"rating": 5,
91+
"note": "test",
92+
"phone_used": True,
93+
},
94+
{
95+
"time_start": "2021-01-01T01:00:00",
96+
"time_end": "2021-01-01T02:00:00",
97+
"rating": 5,
98+
"note": "test",
99+
"phone_used": True,
100+
},
101+
{
102+
"time_start": "2021-01-01T02:00:00",
103+
"time_end": "2021-01-01T03:00:00",
104+
"rating": 5,
105+
"note": "test",
106+
"phone_used": True,
107+
},
108+
{
109+
"time_start": "2021-01-01T03:00:00",
110+
"time_end": "2021-01-01T04:00:00",
111+
"rating": 5,
112+
"note": "test",
113+
"phone_used": True,
114+
},
115+
{
116+
"time_start": "2021-01-01T04:00:00",
117+
"time_end": "2021-01-01T05:00:00",
118+
"rating": 5,
119+
"note": "test",
120+
"phone_used": True,
121+
},
122+
{
123+
"time_start": "2021-01-01T05:00:00",
124+
"time_end": "2021-01-01T06:00:00",
125+
"rating": 5,
126+
"note": "test",
127+
"phone_used": True,
128+
},
129+
]
130+
username, password = "testgettingflushesskip", "testgettingflushesskip"
131+
create_user(client, username, password)
132+
for f in flushes:
133+
create_flush(client, username, password, f)
134+
response = client.get(
135+
"/flushes",
136+
auth=BasicAuth(username=username, password=password),
137+
params={"skip": 0},
138+
)
139+
assert response.status_code == status.HTTP_200_OK
140+
js = response.json()
141+
assert len(js) == 3
142+
assert js[0]["time_start"] == flushes[-1]["time_start"]
143+
response = client.get(
144+
"/flushes",
145+
auth=BasicAuth(username=username, password=password),
146+
params={"skip": 3},
147+
)
148+
assert response.status_code == status.HTTP_200_OK
149+
js = response.json()
150+
assert len(js) == 3
151+
assert js[0]["time_start"] == flushes[2]["time_start"]
152+
153+
response = client.get(
154+
"/flushes",
155+
auth=BasicAuth(username=username, password=password),
156+
params={"skip": 6},
157+
)
158+
assert response.status_code == status.HTTP_200_OK
159+
js = response.json()
160+
assert len(js) == 0

pwa/flush/apicalls.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"io"
1010
"log"
1111
"net/http"
12+
"net/url"
13+
"strconv"
1214
"time"
1315

1416
"github.com/maxence-charriere/go-app/v10/pkg/app"
@@ -141,7 +143,7 @@ func TryAddFlush(creds Creds, flush Flush) (int, error) {
141143
return r.StatusCode, nil
142144
}
143145

144-
func GetFlushes(ctx app.Context) ([]Flush, error) {
146+
func GetFlushes(ctx app.Context, skip int) ([]Flush, error) {
145147
apiUrl, err := GetApiUrl()
146148
if err != nil {
147149
return nil, err
@@ -153,6 +155,11 @@ func GetFlushes(ctx app.Context) ([]Flush, error) {
153155
var c Creds
154156
ctx.GetState("creds", &c)
155157
req.Header.Add("Authorization", "Basic "+c.UserColonPass)
158+
log.Println("flush fetch skip: ", skip)
159+
log.Println("adding skip to /flushes...")
160+
q := url.Values{}
161+
q.Add("skip", strconv.Itoa(skip))
162+
req.URL.RawQuery = q.Encode()
156163
r, err := http.DefaultClient.Do(req)
157164
if err != nil {
158165
return nil, err
@@ -195,6 +202,9 @@ func GetFlushes(ctx app.Context) ([]Flush, error) {
195202
}
196203
log.Println("temporary flush struct: ", temp)
197204
log.Println("Flushes: ", flushes)
205+
if len(flushes) > 0 {
206+
ctx.SetState("skip", skip+3)
207+
}
198208
return flushes, nil
199209
}
200210

0 commit comments

Comments
 (0)