Skip to content

Commit 9724f2c

Browse files
committed
feat: list users order by
1 parent 06e1209 commit 9724f2c

File tree

7 files changed

+1405
-1338
lines changed

7 files changed

+1405
-1338
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ GOVERSION := $(shell go version | cut -d ' ' -f 3 | cut -d '.' -f 2)
22

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

77
install:
88
@echo "Clean up imports..."

core/user/filter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ type Filter struct {
44
Limit int32
55
Page int32
66
Keyword string
7+
SortBy string
8+
OrderBy string
79
}

internal/api/v1beta1/user.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ func (h Handler) ListUsers(ctx context.Context, request *shieldv1beta1.ListUsers
4343
Limit: request.GetPageSize(),
4444
Page: request.GetPageNum(),
4545
Keyword: request.GetKeyword(),
46+
SortBy: request.GetSort(),
47+
OrderBy: request.GetOrder(),
4648
})
4749
if err != nil {
4850
logger.Error(err.Error())

internal/store/postgres/user_repository.go

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
"errors"
88
"fmt"
99
"regexp"
10+
"slices"
1011
"strings"
1112
"time"
1213

1314
"github.com/doug-martin/goqu/v9"
15+
"github.com/doug-martin/goqu/v9/exp"
1416
"github.com/jmoiron/sqlx"
1517
newrelic "github.com/newrelic/go-agent"
1618

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

24+
var (
25+
allowedListUsersSortByColumns = []string{"created_at", "updated_at", "email", "name"}
26+
)
27+
2228
type UserRepository struct {
2329
dbc *db.Client
2430
}
@@ -276,24 +282,51 @@ func (r UserRepository) List(ctx context.Context, flt user.Filter) ([]user.User,
276282
flt.Page = defaultPage
277283
}
278284

285+
var (
286+
orderExpr exp.OrderedExpression
287+
orderedColumn = goqu.C("id")
288+
)
289+
290+
if slices.Contains(allowedListUsersSortByColumns, flt.SortBy) {
291+
orderedColumn = goqu.C(flt.SortBy)
292+
}
293+
294+
if flt.OrderBy == "desc" {
295+
orderExpr = orderedColumn.Desc()
296+
} else {
297+
orderExpr = orderedColumn.Asc()
298+
}
299+
279300
offset := (flt.Page - 1) * flt.Limit
280301

281302
query, params, err := dialect.From(TABLE_USERS).LeftOuterJoin(
282303
goqu.T(TABLE_METADATA),
283-
goqu.On(goqu.Ex{"users.id": goqu.I("metadata.user_id")})).Select("users.id", "name", "email", "key", "value", "users.created_at", "users.updated_at").Where(
284-
goqu.I("users.email").In(
285-
goqu.From("users").
286-
Select(goqu.DISTINCT("email")).
287-
Where(
288-
goqu.Or(
289-
goqu.C("name").ILike(fmt.Sprintf("%%%s%%", flt.Keyword)),
290-
goqu.C("email").ILike(fmt.Sprintf("%%%s%%", flt.Keyword)),
291-
),
292-
).
293-
Limit(uint(flt.Limit)).
294-
Offset(uint(offset)),
295-
),
296-
).ToSQL()
304+
goqu.On(goqu.Ex{"users.id": goqu.I("metadata.user_id")})).
305+
Select(
306+
goqu.L("users.id").As("id"),
307+
"name",
308+
"email",
309+
"key",
310+
"value",
311+
goqu.L("users.created_at").As("created_at"),
312+
goqu.L("users.updated_at").As("updated_at"),
313+
).
314+
Where(
315+
goqu.I("users.email").In(
316+
goqu.From("users").
317+
Select(goqu.DISTINCT("email")).
318+
Where(
319+
goqu.Or(
320+
goqu.C("name").ILike(fmt.Sprintf("%%%s%%", flt.Keyword)),
321+
goqu.C("email").ILike(fmt.Sprintf("%%%s%%", flt.Keyword)),
322+
),
323+
).
324+
Limit(uint(flt.Limit)).
325+
Offset(uint(offset)),
326+
),
327+
).
328+
Order(orderExpr).
329+
ToSQL()
297330
if err != nil {
298331
return []user.User{}, fmt.Errorf("%w: %s", queryErr, err)
299332
}

proto/shield.swagger.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,14 @@ paths:
841841
in: query
842842
required: false
843843
type: string
844+
- name: sort
845+
in: query
846+
required: false
847+
type: string
848+
- name: order
849+
in: query
850+
required: false
851+
type: string
844852
tags:
845853
- User
846854
post:

proto/v1beta1/shield.pb.go

Lines changed: 1341 additions & 1323 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/v1beta1/shield.pb.validate.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)