Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
api
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
Bram Hagens
api
Commits
8b06b9b2
Commit
8b06b9b2
authored
4 years ago
by
Dean
Browse files
Options
Downloads
Patches
Plain Diff
Add type filter to list objects endpoint
parent
a1c7d08e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
lib/apierrors/errors.go
+3
-0
3 additions, 0 deletions
lib/apierrors/errors.go
lib/db/queries.go
+8
-2
8 additions, 2 deletions
lib/db/queries.go
lib/db/sql.go
+1
-1
1 addition, 1 deletion
lib/db/sql.go
lib/routes/listobjects.go
+21
-4
21 additions, 4 deletions
lib/routes/listobjects.go
with
33 additions
and
7 deletions
lib/apierrors/errors.go
+
3
−
0
View file @
8b06b9b2
...
...
@@ -61,6 +61,9 @@ var (
// FileIsNotBanned is a 404 not found error.
FileIsNotBanned
=
APIError
{
false
,
404
,
"specified file is not banned"
,
false
}
// InvalidObjectFilter is a 400 bad request error.
InvalidObjectFilter
=
APIError
{
false
,
400
,
`invalid filter, must be "", "files" or "links"`
,
false
}
)
// Pomf errors
...
...
This diff is collapsed.
Click to expand it.
lib/db/queries.go
+
8
−
2
View file @
8b06b9b2
...
...
@@ -217,13 +217,19 @@ func scanObject(scanner scanner) (Object, error) {
// ListObjectsByAssociatedUser returns all objects (paginated) associated with a
// user.
func
ListObjectsByAssociatedUser
(
userID
string
,
asc
bool
,
offset
,
limit
int
)
([]
Object
,
error
)
{
func
ListObjectsByAssociatedUser
(
userID
string
,
typ
int
,
asc
bool
,
offset
,
limit
int
)
([]
Object
,
error
)
{
objects
:=
[]
Object
{}
order
:=
"DESC"
if
asc
{
order
=
"ASC"
}
rows
,
err
:=
DB
.
Query
(
fmt
.
Sprintf
(
listObjectsByAssociatedUser
,
order
),
userID
,
limit
,
offset
)
typeFilter
:=
"!= 0"
if
typ
!=
-
1
{
typeFilter
=
fmt
.
Sprintf
(
"= %v"
,
typ
)
}
rows
,
err
:=
DB
.
Query
(
fmt
.
Sprintf
(
listObjectsByAssociatedUser
,
typeFilter
,
order
),
userID
,
limit
,
offset
)
if
err
!=
nil
{
return
objects
,
err
}
...
...
This diff is collapsed.
Click to expand it.
lib/db/sql.go
+
1
−
1
View file @
8b06b9b2
...
...
@@ -84,7 +84,7 @@ FROM
objects
WHERE
associated_user = $1 AND
"type"
!= 2
"type"
%v
ORDER BY
created_at %s
LIMIT $2
...
...
This diff is collapsed.
Click to expand it.
lib/routes/listobjects.go
+
21
−
4
View file @
8b06b9b2
...
...
@@ -14,8 +14,14 @@ import (
"github.com/spf13/viper"
)
// Maximum objects per page
const
maxLimit
=
100
const
(
// Maximum objects per page
maxLimit
=
100
// filter keys for file vs short link.
filterFiles
=
"file"
filterLinks
=
"link"
)
// listObjectsResponse is the response format for ListObjects.
type
listObjectsResponse
struct
{
...
...
@@ -41,7 +47,7 @@ func ListObjects(w http.ResponseWriter, r *http.Request) {
panic
(
apierrors
.
InternalServerError
)
}
// Determine offset
and
limit
information
// Determine offset
,
limit
and filter params
query
:=
r
.
URL
.
Query
()
l
:=
query
.
Get
(
"limit"
)
limit
,
err
:=
strconv
.
Atoi
(
l
)
...
...
@@ -60,9 +66,20 @@ func ListObjects(w http.ResponseWriter, r *http.Request) {
if
query
.
Get
(
"order"
)
==
"asc"
{
asc
=
true
}
filter
:=
query
.
Get
(
"type"
)
if
filter
!=
""
&&
filter
!=
filterFiles
&&
filter
!=
filterLinks
{
panic
(
apierrors
.
InvalidObjectFilter
)
}
f
:=
-
1
if
filter
==
filterFiles
{
f
=
0
}
else
if
filter
==
filterLinks
{
f
=
1
}
// Get the data
objects
,
err
:=
db
.
ListObjectsByAssociatedUser
(
user
.
ID
,
asc
,
offset
,
limit
)
objects
,
err
:=
db
.
ListObjectsByAssociatedUser
(
user
.
ID
,
f
,
asc
,
offset
,
limit
)
if
err
!=
nil
{
log
.
Error
()
.
Err
(
err
)
.
Msg
(
"failed to list objects for user"
)
panic
(
apierrors
.
InternalServerError
)
...
...
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