Skip to content

Commit

Permalink
feat: list users order by
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdh committed May 22, 2024
1 parent 06e1209 commit c15457c
Show file tree
Hide file tree
Showing 7 changed files with 1,383 additions and 1,325 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ GOVERSION := $(shell go version | cut -d ' ' -f 3 | cut -d '.' -f 2)

.PHONY: build check fmt lint test test-race vet test-cover-html help install proto
.DEFAULT_GOAL := build
PROTON_COMMIT := "7e380e055d82cd8378989354785f6434d8615d70"
PROTON_COMMIT := "0a8041a647ed5e6e0a88ad2f576430b5d6ffb38f"

install:
@echo "Clean up imports..."
Expand Down
2 changes: 2 additions & 0 deletions core/user/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ type Filter struct {
Limit int32
Page int32
Keyword string
SortBy string
OrderBy string
}
2 changes: 2 additions & 0 deletions internal/api/v1beta1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func (h Handler) ListUsers(ctx context.Context, request *shieldv1beta1.ListUsers
Limit: request.GetPageSize(),
Page: request.GetPageNum(),
Keyword: request.GetKeyword(),
SortBy: request.GetSort(),
OrderBy: request.GetOrder(),
})
if err != nil {
logger.Error(err.Error())
Expand Down
26 changes: 25 additions & 1 deletion internal/store/postgres/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"errors"
"fmt"
"regexp"
"slices"
"strings"
"time"

"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/exp"
"github.com/jmoiron/sqlx"
newrelic "github.com/newrelic/go-agent"

Expand All @@ -19,6 +21,10 @@ import (
"github.com/goto/shield/pkg/uuid"
)

var (
allowedListUsersSortByColumns = []string{"created_at", "updated_at", "email", "name"}
)

type UserRepository struct {
dbc *db.Client
}
Expand Down Expand Up @@ -276,6 +282,23 @@ func (r UserRepository) List(ctx context.Context, flt user.Filter) ([]user.User,
flt.Page = defaultPage
}

var (
orderExpr exp.OrderedExpression
orderedColumn = goqu.C("email")
)

if slices.Contains(allowedListUsersSortByColumns, flt.SortBy) {
orderedColumn = goqu.C(flt.SortBy)
}

if flt.OrderBy == "asc" {
orderExpr = orderedColumn.Asc()
} else if flt.OrderBy == "desc" {
orderExpr = orderedColumn.Desc()
} else {
orderExpr = orderedColumn.Asc()
}

offset := (flt.Page - 1) * flt.Limit

query, params, err := dialect.From(TABLE_USERS).LeftOuterJoin(
Expand All @@ -291,7 +314,8 @@ func (r UserRepository) List(ctx context.Context, flt user.Filter) ([]user.User,
),
).
Limit(uint(flt.Limit)).
Offset(uint(offset)),
Offset(uint(offset)).
Order(orderExpr),
),
).ToSQL()
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions proto/shield.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,14 @@ paths:
in: query
required: false
type: string
- name: sort
in: query
required: false
type: string
- name: order
in: query
required: false
type: string
tags:
- User
post:
Expand Down
2,664 changes: 1,341 additions & 1,323 deletions proto/v1beta1/shield.pb.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions proto/v1beta1/shield.pb.validate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c15457c

Please sign in to comment.