mirror of
https://github.com/teamhanko/hanko.git
synced 2025-11-01 22:28:27 +08:00
feat: remove unnecessary query param
This commit is contained in:
@ -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"`
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
@ -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)
|
||||
@ -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 {
|
||||
@ -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)
|
||||
|
||||
@ -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
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user