Skip to content
Snippets Groups Projects
Commit 8b06b9b2 authored by Dean's avatar Dean
Browse files

Add type filter to list objects endpoint

parent a1c7d08e
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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
}
......
......@@ -84,7 +84,7 @@ FROM
objects
WHERE
associated_user = $1 AND
"type" != 2
"type" %v
ORDER BY
created_at %s
LIMIT $2
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment