Skip to content
Snippets Groups Projects
Verified Commit 4dd6ce3f authored by Spotlight Deveaux's avatar Spotlight Deveaux :fox:
Browse files

Add total_objects

This adds a `total_objects` count to the response, which is useful to know the count of objects associated to a user to determine the amount of pages required ahead of time for ease purposes.
parent a9ceec32
No related branches found
No related tags found
1 merge request!2Implement total_objects for object listing
...@@ -246,6 +246,23 @@ func ListObjectsByAssociatedUser(userID string, typ int, asc bool, offset, limit ...@@ -246,6 +246,23 @@ func ListObjectsByAssociatedUser(userID string, typ int, asc bool, offset, limit
return objects, nil return objects, nil
} }
// CountObjectsByAssociatedUser returns the count of all objects associated with a user.
func CountObjectsByAssociatedUser(userID string, typ int) (int, error) {
typeFilter := "!= 2"
if typ != -1 {
typeFilter = fmt.Sprintf("= %v", typ)
}
var count int
query := fmt.Sprintf(countObjectsByAssociatedUser, typeFilter)
fmt.Printf(query)
err := DB.QueryRow(query, userID).Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
// GetObject returns an object. // GetObject returns an object.
func GetObject(bucket, key string) (Object, error) { func GetObject(bucket, key string) (Object, error) {
row := DB.QueryRow(getObjectByBucketKey, fmt.Sprintf("%s/%s", bucket, key)) row := DB.QueryRow(getObjectByBucketKey, fmt.Sprintf("%s/%s", bucket, key))
......
...@@ -65,6 +65,16 @@ VALUES ...@@ -65,6 +65,16 @@ VALUES
($1, $2) ($1, $2)
` `
var countObjectsByAssociatedUser = `
SELECT
COUNT(*) as count
FROM
objects
WHERE
associated_user = $1 AND
"type" %v
`
var listObjectsByAssociatedUser = ` var listObjectsByAssociatedUser = `
SELECT SELECT
bucket, bucket,
......
...@@ -25,8 +25,9 @@ const ( ...@@ -25,8 +25,9 @@ const (
// listObjectsResponse is the response format for ListObjects. // listObjectsResponse is the response format for ListObjects.
type listObjectsResponse struct { type listObjectsResponse struct {
Success bool `json:"success"` Success bool `json:"success"`
Data []db.Object `json:"data"` TotalObjects int `json:"total_objects"`
Data []db.Object `json:"data"`
} }
// ListObjects returns a paginated list of all objects owned by a user. // ListObjects returns a paginated list of all objects owned by a user.
...@@ -79,6 +80,12 @@ func ListObjects(w http.ResponseWriter, r *http.Request) { ...@@ -79,6 +80,12 @@ func ListObjects(w http.ResponseWriter, r *http.Request) {
} }
// Get the data // Get the data
count, err := db.CountObjectsByAssociatedUser(user.ID, f)
if err != nil {
log.Error().Err(err).Msg("failed to count objects for user")
panic(apierrors.InternalServerError)
}
objects, err := db.ListObjectsByAssociatedUser(user.ID, f, asc, offset, limit) objects, err := db.ListObjectsByAssociatedUser(user.ID, f, asc, offset, limit)
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to list objects for user") log.Error().Err(err).Msg("failed to list objects for user")
...@@ -92,5 +99,5 @@ func ListObjects(w http.ResponseWriter, r *http.Request) { ...@@ -92,5 +99,5 @@ func ListObjects(w http.ResponseWriter, r *http.Request) {
// Return response // Return response
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
render.JSON(w, r, listObjectsResponse{true, objects}) render.JSON(w, r, listObjectsResponse{true, count, objects})
} }
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