Skip to content

Commit

Permalink
Merge pull request #120 from openinfradev/TKS-786
Browse files Browse the repository at this point in the history
bugfix. fix filtering logic in users
  • Loading branch information
cho4036 authored Aug 2, 2023
2 parents ef95b14 + 3546f8a commit 14e357e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 36 deletions.
2 changes: 1 addition & 1 deletion internal/delivery/http/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (u UserHandler) List(w http.ResponseWriter, r *http.Request) {
ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "", ""))
return
}
users, err := u.usecase.List(r.Context(), organizationId, pg)
users, err := u.usecase.ListWithPagination(r.Context(), organizationId, pg)
if err != nil {
if _, status := httpErrors.ErrorResponse(err); status == http.StatusNotFound {
ResponseJSON(w, r, http.StatusNoContent, domain.ListUserResponse{})
Expand Down
2 changes: 1 addition & 1 deletion internal/pagination/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func NewPagination(urlParams *url.Values) (*Pagination, error) {
//"combinedFilter=key1,key2:value"
filterArray := strings.Split(value[0], ":")
if len(filterArray) == 2 {
keys := strings.Split(filterArray[0], ",")
keys := strings.Split(helper.ToSnakeCase(strings.Replace(filterArray[0], "[]", "", -1)), ",")
value := filterArray[1]

pg.CombinedFilter = CombinedFilter{
Expand Down
57 changes: 35 additions & 22 deletions internal/repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type IUserRepository interface {
Create(accountId string, organizationId string, password string, name string) (domain.User, error)
CreateWithUuid(uuid uuid.UUID, accountId string, name string, password string, email string,
department string, description string, organizationId string, roleId uuid.UUID) (domain.User, error)
List(pg *pagination.Pagination, filters ...FilterFunc) (out *[]domain.User, err error)
List(filters ...FilterFunc) (out *[]domain.User, err error)
ListWithPagination(pg *pagination.Pagination, organizationId string) (out *[]domain.User, err error)
Get(accountId string, organizationId string) (domain.User, error)
GetByUuid(userId uuid.UUID) (domain.User, error)
UpdateWithUuid(uuid uuid.UUID, accountId string, name string, password string, roleId uuid.UUID, email string,
Expand Down Expand Up @@ -143,23 +144,11 @@ func (r *UserRepository) NameFilter(name string) FilterFunc {
}
}

func (r *UserRepository) List(pg *pagination.Pagination, filters ...FilterFunc) (*[]domain.User, error) {
var res *gorm.DB
func (r *UserRepository) List(filters ...FilterFunc) (*[]domain.User, error) {
var users []User
var total int64

if pg == nil {
pg = pagination.NewDefaultPagination()
}

var res *gorm.DB
if filters == nil {
r.db.Model(&User{}).Count(&total)

pg.TotalRows = total
pg.TotalPages = int(math.Ceil(float64(total) / float64(pg.Limit)))
orderQuery := fmt.Sprintf("%s %s", pg.SortColumn, pg.SortOrder)
res = r.db.Model(&User{}).Offset(pg.GetOffset()).Limit(pg.GetLimit()).Order(orderQuery).
Preload("Organization").Preload("Role").Find(&users)
res = r.db.Model(&User{}).Preload("Organization").Preload("Role").Find(&users)
} else {
combinedFilter := func(filters ...FilterFunc) FilterFunc {
return func(user *gorm.DB) *gorm.DB {
Expand All @@ -170,15 +159,38 @@ func (r *UserRepository) List(pg *pagination.Pagination, filters ...FilterFunc)
}
}
cFunc := combinedFilter(filters...)
cFunc(r.db.Model(&User{})).Count(&total)
res = cFunc(r.db.Model(&User{}).Preload("Organization").Preload("Role")).Find(&users)
}
if res.Error != nil {
log.Errorf("error is :%s(%T)", res.Error.Error(), res.Error)
return nil, res.Error
}
if res.RowsAffected == 0 {
return nil, httpErrors.NewNotFoundError(httpErrors.NotFound, "", "")
}

var out []domain.User
for _, user := range users {
out = append(out, r.reflect(user))
}

pg.TotalRows = total
pg.TotalPages = int(math.Ceil(float64(total) / float64(pg.Limit)))
orderQuery := fmt.Sprintf("%s %s", pg.SortColumn, pg.SortOrder)
res = cFunc(r.db.Model(&User{}).Offset(pg.GetOffset()).Limit(pg.GetLimit()).Order(orderQuery).
Preload("Organization").Preload("Role")).Find(&users)
return &out, nil
}

func (r *UserRepository) ListWithPagination(pg *pagination.Pagination, organizationId string) (*[]domain.User, error) {
var users []User

if pg == nil {
pg = pagination.NewDefaultPagination()
}

filterFunc := CombinedGormFilter("users", pg.GetFilters(), pg.CombinedFilter)
db := filterFunc(r.db.Model(&User{}).Where("organization_id = ?", organizationId))
db.Count(&pg.TotalRows)

pg.TotalPages = int(math.Ceil(float64(pg.TotalRows) / float64(pg.Limit)))
orderQuery := fmt.Sprintf("%s %s", pg.SortColumn, pg.SortOrder)
res := db.Preload("Organization").Preload("Role").Offset(pg.GetOffset()).Limit(pg.GetLimit()).Order(orderQuery).Find(&users)
if res.Error != nil {
log.Errorf("error is :%s(%T)", res.Error.Error(), res.Error)
return nil, res.Error
Expand All @@ -194,6 +206,7 @@ func (r *UserRepository) List(pg *pagination.Pagination, filters ...FilterFunc)

return &out, nil
}

func (r *UserRepository) Get(accountId string, organizationId string) (domain.User, error) {
user, err := r.getUserByAccountId(accountId, organizationId)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/usecase/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (u *AuthUsecase) Logout(accessToken string, organizationName string) error
return nil
}
func (u *AuthUsecase) FindId(code string, email string, userName string, organizationId string) (string, error) {
users, err := u.userRepository.List(nil, u.userRepository.OrganizationFilter(organizationId),
users, err := u.userRepository.List(u.userRepository.OrganizationFilter(organizationId),
u.userRepository.NameFilter(userName), u.userRepository.EmailFilter(email))
if err != nil && users == nil {
return "", httpErrors.NewBadRequestError(err, "A_INVALID_ID", "")
Expand Down Expand Up @@ -134,7 +134,7 @@ func (u *AuthUsecase) FindId(code string, email string, userName string, organiz
}

func (u *AuthUsecase) FindPassword(code string, accountId string, email string, userName string, organizationId string) error {
users, err := u.userRepository.List(nil, u.userRepository.OrganizationFilter(organizationId),
users, err := u.userRepository.List(u.userRepository.OrganizationFilter(organizationId),
u.userRepository.AccountIdFilter(accountId), u.userRepository.NameFilter(userName),
u.userRepository.EmailFilter(email))
if err != nil && users == nil {
Expand Down Expand Up @@ -198,10 +198,10 @@ func (u *AuthUsecase) VerifyIdentity(accountId string, email string, userName st
var err error

if accountId == "" {
users, err = u.userRepository.List(nil, u.userRepository.OrganizationFilter(organizationId),
users, err = u.userRepository.List(u.userRepository.OrganizationFilter(organizationId),
u.userRepository.NameFilter(userName), u.userRepository.EmailFilter(email))
} else {
users, err = u.userRepository.List(nil, u.userRepository.OrganizationFilter(organizationId),
users, err = u.userRepository.List(u.userRepository.OrganizationFilter(organizationId),
u.userRepository.AccountIdFilter(accountId), u.userRepository.NameFilter(userName),
u.userRepository.EmailFilter(email))
}
Expand Down
26 changes: 18 additions & 8 deletions internal/usecase/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type IUserUsecase interface {
DeleteAdmin(organizationId string) error
DeleteAll(ctx context.Context, organizationId string) error
Create(ctx context.Context, user *domain.User) (*domain.User, error)
List(ctx context.Context, organizationId string, pg *pagination.Pagination) (*[]domain.User, error)
List(ctx context.Context, organizationId string) (*[]domain.User, error)
ListWithPagination(ctx context.Context, organizationId string, pg *pagination.Pagination) (*[]domain.User, error)
Get(userId uuid.UUID) (*domain.User, error)
Update(ctx context.Context, userId uuid.UUID, user *domain.User) (*domain.User, error)
ResetPassword(userId uuid.UUID) error
Expand Down Expand Up @@ -321,13 +322,22 @@ func (u *UserUsecase) UpdatePasswordByAccountId(ctx context.Context, accountId s
return nil
}

func (u *UserUsecase) List(ctx context.Context, organizationId string, pg *pagination.Pagination) (*[]domain.User, error) {
users, err := u.userRepository.List(pg, u.userRepository.OrganizationFilter(organizationId))
func (u *UserUsecase) List(ctx context.Context, organizationId string) (users *[]domain.User, err error) {
users, err = u.userRepository.List(u.userRepository.OrganizationFilter(organizationId))
if err != nil {
return nil, err
}

return users, nil
return
}

func (u *UserUsecase) ListWithPagination(ctx context.Context, organizationId string, pg *pagination.Pagination) (users *[]domain.User, err error) {
users, err = u.userRepository.ListWithPagination(pg, organizationId)
if err != nil {
return nil, err
}

return
}

func (u *UserUsecase) Get(userId uuid.UUID) (*domain.User, error) {
Expand All @@ -343,7 +353,7 @@ func (u *UserUsecase) Get(userId uuid.UUID) (*domain.User, error) {
}

func (u *UserUsecase) GetByAccountId(ctx context.Context, accountId string, organizationId string) (*domain.User, error) {
users, err := u.userRepository.List(nil, u.userRepository.OrganizationFilter(organizationId),
users, err := u.userRepository.List(u.userRepository.OrganizationFilter(organizationId),
u.userRepository.AccountIdFilter(accountId))
if err != nil {
return nil, err
Expand All @@ -353,7 +363,7 @@ func (u *UserUsecase) GetByAccountId(ctx context.Context, accountId string, orga
}

func (u *UserUsecase) GetByEmail(ctx context.Context, email string, organizationId string) (*domain.User, error) {
users, err := u.userRepository.List(nil, u.userRepository.OrganizationFilter(organizationId),
users, err := u.userRepository.List(u.userRepository.OrganizationFilter(organizationId),
u.userRepository.EmailFilter(email))
if err != nil {
return nil, err
Expand Down Expand Up @@ -395,7 +405,7 @@ func (u *UserUsecase) UpdateByAccountId(ctx context.Context, accountId string, u
}
}

users, err := u.userRepository.List(nil, u.userRepository.OrganizationFilter(userInfo.GetOrganizationId()),
users, err := u.userRepository.List(u.userRepository.OrganizationFilter(userInfo.GetOrganizationId()),
u.userRepository.AccountIdFilter(accountId))
if err != nil {
if _, code := httpErrors.ErrorResponse(err); code == http.StatusNotFound {
Expand Down Expand Up @@ -551,7 +561,7 @@ func (u *UserUsecase) UpdateByAccountIdByAdmin(ctx context.Context, accountId st
}
}

users, err := u.userRepository.List(nil, u.userRepository.OrganizationFilter(userInfo.GetOrganizationId()),
users, err := u.userRepository.List(u.userRepository.OrganizationFilter(userInfo.GetOrganizationId()),
u.userRepository.AccountIdFilter(accountId))
if err != nil {
if _, code := httpErrors.ErrorResponse(err); code == http.StatusNotFound {
Expand Down

0 comments on commit 14e357e

Please sign in to comment.