Skip to content

Commit

Permalink
allow fields choice in query files resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoubfaouzi committed Dec 21, 2023
1 parent f47d9ed commit 70730a9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export SAFERWALL_VER = 0.4.0 # Current Version
export SAFERWALL_VER = 0.5.0 # Current Version
export DOCKER_HUB_USR = saferwall # Dockerhub username
export DOCKER_HUB_IMG = webapis # Dockerhub image name
export COUCHBASE_BUCKETS_LIST = sfw # Couchbase bucket names list
Expand Down
6 changes: 5 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd"
"program": "${workspaceFolder}/cmd",
"env": {
"SFW_WEBAPIS_DEPLOYMENT_KIND": "local"
},
"dlvFlags": ["--check-go-version=false"],
}
]
}
17 changes: 16 additions & 1 deletion internal/file/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,27 @@ func (r resource) list(c echo.Context) error {
return errors.Forbidden("")
}

// the `fields` query parameter is used to limit the fields
// to include in the response.
var fields []string
if fieldsParam := c.QueryParam("fields"); fieldsParam != "" {
fields = strings.Split(fieldsParam, ",")
}

if len(fields) > 0 {
allowed := areFieldsAllowed(fields)
if !allowed {
return errors.BadRequest("field not allowed")
}
}

count, err := r.service.Count(ctx)
if err != nil {
return err
}

pages := pagination.NewFromRequest(c.Request(), count)
files, err := r.service.Query(ctx, pages.Offset(), pages.Limit())
files, err := r.service.Query(ctx, pages.Offset(), pages.Limit(), fields)
if err != nil {
return err
}
Expand Down
18 changes: 15 additions & 3 deletions internal/file/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package file
import (
"context"
"encoding/json"
"fmt"
"strings"

dbcontext "github.com/saferwall/saferwall-api/internal/db"
Expand All @@ -23,7 +24,7 @@ type Repository interface {
// Exists return true when the doc exists in the DB.
Exists(ctx context.Context, id string) (bool, error)
// Query returns the list of files with the given offset and limit.
Query(ctx context.Context, offset, limit int) ([]entity.File, error)
Query(ctx context.Context, offset, limit int, fields []string) ([]entity.File, error)
// Create saves a new file in the storage.
Create(ctx context.Context, id string, file entity.File) error
// Update updates the whole file with given ID in the storage.
Expand Down Expand Up @@ -121,16 +122,27 @@ func (r repository) Count(ctx context.Context) (int, error) {

// Query retrieves the file records with the specified offset and limit
// from the database.
func (r repository) Query(ctx context.Context, offset, limit int) (
func (r repository) Query(ctx context.Context, offset, limit int, fields []string) (
[]entity.File, error) {
var res interface{}

params := make(map[string]interface{}, 1)
params["docType"] = "file"
params["offset"] = offset
params["limit"] = limit

statement := r.db.N1QLQuery[dbcontext.GetAllDocType]

if len(fields) > 0 {
statement = "SELECT "
for _, field := range fields {
statement += fmt.Sprintf("%s,", field)
}
statement = strings.TrimSuffix(statement, ",")
statement += fmt.Sprintf(" FROM `%s` WHERE type = $docType OFFSET $offset LIMIT $limit",
r.db.Bucket.Name())

}

err := r.db.Query(ctx, statement, params, &res)
if err != nil {
return []entity.File{}, err
Expand Down
6 changes: 3 additions & 3 deletions internal/file/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Service interface {
Create(ctx context.Context, input CreateFileRequest) (File, error)
Update(ctx context.Context, id string, input UpdateFileRequest) (File, error)
Delete(ctx context.Context, id string) (File, error)
Query(ctx context.Context, offset, limit int) ([]File, error)
Query(ctx context.Context, offset, limit int, fields []string) ([]File, error)
Patch(ctx context.Context, key, path string, val interface{}) error
Summary(ctx context.Context, id string) (interface{}, error)
Like(ctx context.Context, id string) error
Expand Down Expand Up @@ -315,10 +315,10 @@ func (s service) Exists(ctx context.Context, id string) (bool, error) {
}

// Query returns the files with the specified offset and limit.
func (s service) Query(ctx context.Context, offset, limit int) (
func (s service) Query(ctx context.Context, offset, limit int, fields []string) (
[]File, error) {

items, err := s.repo.Query(ctx, offset, limit)
items, err := s.repo.Query(ctx, offset, limit, fields)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 70730a9

Please sign in to comment.