Skip to content

Commit

Permalink
feat: remove unnecessary query param
Browse files Browse the repository at this point in the history
  • Loading branch information
FreddyDevelop committed Jan 26, 2023
1 parent b129bfc commit 112602e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
6 changes: 2 additions & 4 deletions backend/handler/user_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func (h *UserHandlerAdmin) Delete(c echo.Context) error {
type UserListRequest struct {
PerPage int `query:"per_page"`
Page int `query:"page"`
Q string `query:"q"`
Email string `query:"email"`
UserId string `query:"user_id"`
}
Expand Down Expand Up @@ -78,14 +77,13 @@ func (h *UserHandlerAdmin) List(c echo.Context) error {
}

email := strings.ToLower(request.Email)
q := strings.ToLower(request.Q)

users, err := h.persister.GetUserPersister().List(request.Page, request.PerPage, q, userId, email)
users, err := h.persister.GetUserPersister().List(request.Page, request.PerPage, userId, email)
if err != nil {
return fmt.Errorf("failed to get list of users: %w", err)
}

userCount, err := h.persister.GetUserPersister().Count(q, userId, email)
userCount, err := h.persister.GetUserPersister().Count(userId, email)
if err != nil {
return fmt.Errorf("failed to get total count of users: %w", err)
}
Expand Down
24 changes: 8 additions & 16 deletions backend/persistence/user_persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type UserPersister interface {
Create(models.User) error
Update(models.User) error
Delete(models.User) error
List(page int, perPage int, q string, userId uuid.UUID, email string) ([]models.User, error)
Count(q string, userId uuid.UUID, email string) (int, error)
List(page int, perPage int, userId uuid.UUID, email string) ([]models.User, error)
Count(userId uuid.UUID, email string) (int, error)
}

type userPersister struct {
Expand Down Expand Up @@ -87,14 +87,14 @@ func (p *userPersister) Delete(user models.User) error {
return nil
}

func (p *userPersister) List(page int, perPage int, q string, userId uuid.UUID, email string) ([]models.User, error) {
func (p *userPersister) List(page int, perPage int, userId uuid.UUID, email string) ([]models.User, error) {
users := []models.User{}

query := p.db.
Q().
EagerPreload("Emails", "Emails.PrimaryEmail", "WebauthnCredentials").
LeftJoin("emails", "emails.user_id = users.id")
query = p.addQueryParamsToSqlQuery(query, q, userId, email)
query = p.addQueryParamsToSqlQuery(query, userId, email)
err := query.GroupBy("users.id").
Paginate(page, perPage).
All(&users)
Expand All @@ -109,11 +109,11 @@ func (p *userPersister) List(page int, perPage int, q string, userId uuid.UUID,
return users, nil
}

func (p *userPersister) Count(q string, userId uuid.UUID, email string) (int, error) {
func (p *userPersister) Count(userId uuid.UUID, email string) (int, error) {
query := p.db.
Q().
LeftJoin("emails", "emails.user_id = users.id")
query = p.addQueryParamsToSqlQuery(query, q, userId, email)
query = p.addQueryParamsToSqlQuery(query, userId, email)
count, err := query.GroupBy("users.id").
Count(&models.User{})
if err != nil {
Expand All @@ -123,17 +123,9 @@ func (p *userPersister) Count(q string, userId uuid.UUID, email string) (int, er
return count, nil
}

func (p *userPersister) addQueryParamsToSqlQuery(query *pop.Query, q string, userId uuid.UUID, email string) *pop.Query {
if q != "" {
switch p.db.Dialect.Name() {
case "postgres", "cockroach":
query = query.Where("emails.address LIKE ? OR users.id::text LIKE ?", "%"+q+"%", "%"+q+"%")
case "mysql", "mariadb":
query = query.Where("emails.address LIKE ? OR users.id LIKE ?", "%"+q+"%", "%"+q+"%")
}
}
func (p *userPersister) addQueryParamsToSqlQuery(query *pop.Query, userId uuid.UUID, email string) *pop.Query {
if email != "" {
query = query.Where("emails.address = ?", email)
query = query.Where("emails.address LIKE ?", "%"+email+"%")
}
if !userId.IsNil() {
query = query.Where("users.id = ?", userId)
Expand Down
4 changes: 2 additions & 2 deletions backend/test/user_persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (p *userPersister) Delete(user models.User) error {
return nil
}

func (p *userPersister) List(page int, perPage int, q string, userId uuid.UUID, email string) ([]models.User, error) {
func (p *userPersister) List(page int, perPage int, userId uuid.UUID, email string) ([]models.User, error) {
if len(p.users) == 0 {
return p.users, nil
}
Expand Down Expand Up @@ -81,6 +81,6 @@ func (p *userPersister) List(page int, perPage int, q string, userId uuid.UUID,
return result[page-1], nil
}

func (p *userPersister) Count(q string, userId uuid.UUID, email string) (int, error) {
func (p *userPersister) Count(userId uuid.UUID, email string) (int, error) {
return len(p.users), nil
}

0 comments on commit 112602e

Please sign in to comment.