Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDP-974] data/httphandler/validators: Adding sorting to GET /users endpoint. #104

Merged
merged 10 commits into from
Nov 29, 2023
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
Loading