Skip to content

Commit

Permalink
[SDP-974] data/httphandler/validators: Adding sorting to GET /users e…
Browse files Browse the repository at this point in the history
…ndpoint. (#104)

What
Add sort and direction parameters to sort GET /users endpoint.

Why
Sort users according to query parameters.
  • Loading branch information
ceciliaromao authored Nov 29, 2023
1 parent a665505 commit 7df3634
Show file tree
Hide file tree
Showing 5 changed files with 340 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased

> Place unreleased changes here.
- Add sorting to `GET /users` endpoint [#104](https://github.com/stellar/stellar-disbursement-platform-backend/pull/104)

## [1.0.0](https://github.com/stellar/stellar-disbursement-platform-backend/compare/1.0.0-rc2...1.0.0)

Expand Down
2 changes: 2 additions & 0 deletions internal/data/query_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type SortField string

const (
SortFieldName SortField = "name"
SortFieldEmail SortField = "email"
SortFieldIsActive SortField = "is_active"
SortFieldCreatedAt SortField = "created_at"
SortFieldUpdatedAt SortField = "updated_at"
)
Expand Down
37 changes: 36 additions & 1 deletion internal/serve/httphandler/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
"sort"

"github.com/stellar/go/support/http/httpdecode"
"github.com/stellar/go/support/log"
Expand Down Expand Up @@ -32,6 +33,18 @@ type UserActivationRequest struct {
IsActive *bool `json:"is_active"`
}

type UserSorterByEmail []auth.User

func (a UserSorterByEmail) Len() int { return len(a) }
func (a UserSorterByEmail) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a UserSorterByEmail) Less(i, j int) bool { return a[i].Email < a[j].Email }

type UserSorterByIsActive []auth.User

func (a UserSorterByIsActive) Len() int { return len(a) }
func (a UserSorterByIsActive) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a UserSorterByIsActive) Less(i, j int) bool { return a[i].IsActive }

func (uar UserActivationRequest) validate() *httperror.HTTPError {
validator := validators.NewValidator()

Expand Down Expand Up @@ -275,6 +288,13 @@ func (h UserHandler) UpdateUserRoles(rw http.ResponseWriter, req *http.Request)
}

func (h UserHandler) GetAllUsers(rw http.ResponseWriter, req *http.Request) {
validator := validators.NewUserQueryValidator()
queryParams := validator.ParseParametersFromRequest(req)
if validator.HasErrors() {
httperror.BadRequest("request invalid", nil, validator.Errors).Render(rw)
return
}

ctx := req.Context()

token, ok := ctx.Value(middleware.TokenContextKey).(string)
Expand All @@ -290,10 +310,25 @@ func (h UserHandler) GetAllUsers(rw http.ResponseWriter, req *http.Request) {
httperror.Unauthorized("", err, nil).Render(rw)
return
}

httperror.InternalError(ctx, "Cannot get all users", err, nil).Render(rw)
return
}

// Order users
switch queryParams.SortBy {
case data.SortFieldEmail:
if queryParams.SortOrder == data.SortOrderDESC {
sort.Sort(sort.Reverse(UserSorterByEmail(users)))
} else {
sort.Sort(UserSorterByEmail(users))
}
case data.SortFieldIsActive:
if queryParams.SortOrder == data.SortOrderDESC {
sort.Sort(sort.Reverse(UserSorterByIsActive(users)))
} else {
sort.Sort(UserSorterByIsActive(users))
}
}

httpjson.RenderStatus(rw, http.StatusOK, users, httpjson.JSON)
}
Loading

0 comments on commit 7df3634

Please sign in to comment.