Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
tank
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AURA
tank
Commits
4b7cdade
Commit
4b7cdade
authored
5 years ago
by
Christian Pointner
Browse files
Options
Downloads
Patches
Plain Diff
implemented pagination for shows
parent
32030bd8
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
api/v1/shows.go
+27
-6
27 additions, 6 deletions
api/v1/shows.go
api/v1/utils.go
+25
-0
25 additions, 0 deletions
api/v1/utils.go
with
52 additions
and
6 deletions
api/v1/shows.go
+
27
−
6
View file @
4b7cdade
...
...
@@ -26,6 +26,7 @@ package v1
import
(
"net/http"
"sort"
"github.com/gin-gonic/gin"
"gitlab.servus.at/autoradio/tank/store"
...
...
@@ -38,10 +39,6 @@ func (api *API) ListShows(c *gin.Context) {
return
}
// TODO: implemement pagination, since the list of shows is rather short
// and the result-set is filtered below we should implement this manually and
// not use the database...
s
:=
getAuthSession
(
c
.
Request
)
// this will panic if there is no session
showsMap
:=
make
(
map
[
string
]
store
.
Show
)
for
_
,
showName
:=
range
s
.
Shows
{
...
...
@@ -55,9 +52,33 @@ func (api *API) ListShows(c *gin.Context) {
showsMap
[
show
.
Name
]
=
show
}
}
showNames
:=
[]
string
{}
for
name
,
_
:=
range
showsMap
{
showNames
=
append
(
showNames
,
name
)
}
sort
.
Strings
(
showNames
)
offset
,
limit
,
ok
:=
getPaginationParameter
(
c
)
if
!
ok
{
return
}
result
:=
ShowsListing
{}
for
_
,
show
:=
range
showsMap
{
result
.
Shows
=
append
(
result
.
Shows
,
show
)
if
offset
>
0
{
if
offset
>=
len
(
showNames
)
{
result
.
Shows
=
store
.
Shows
{}
c
.
JSON
(
http
.
StatusOK
,
result
)
return
}
showNames
=
showNames
[
offset
:
]
}
if
limit
>
0
&&
limit
<
len
(
showNames
)
{
showNames
=
showNames
[
:
limit
]
}
for
_
,
name
:=
range
showNames
{
result
.
Shows
=
append
(
result
.
Shows
,
showsMap
[
name
])
}
c
.
JSON
(
http
.
StatusOK
,
result
)
...
...
This diff is collapsed.
Click to expand it.
api/v1/utils.go
+
25
−
0
View file @
4b7cdade
...
...
@@ -98,6 +98,31 @@ func sendError(c *gin.Context, err error) {
c
.
JSON
(
code
,
response
)
}
func
parsePositiveIntegerParameter
(
c
*
gin
.
Context
,
name
string
)
(
int
,
bool
)
{
valueStr
:=
c
.
Query
(
name
)
if
valueStr
==
""
{
return
-
1
,
true
}
value
,
err
:=
strconv
.
Atoi
(
valueStr
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusBadRequest
,
ErrorResponse
{
Error
:
"query parameter "
+
name
+
" is invalid: "
+
err
.
Error
()})
return
-
1
,
false
}
if
value
<
0
{
c
.
JSON
(
http
.
StatusBadRequest
,
ErrorResponse
{
Error
:
"query parameter "
+
name
+
" must be >= 0"
})
return
-
1
,
false
}
return
value
,
true
}
func
getPaginationParameter
(
c
*
gin
.
Context
)
(
offset
,
limit
int
,
ok
bool
)
{
if
offset
,
ok
=
parsePositiveIntegerParameter
(
c
,
"offset"
);
!
ok
{
return
}
limit
,
ok
=
parsePositiveIntegerParameter
(
c
,
"limit"
)
return
}
func
getAuthSession
(
r
*
http
.
Request
)
*
auth
.
Session
{
s
,
ok
:=
auth
.
SessionFromRequest
(
r
)
if
!
ok
||
s
==
nil
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment