diff --git a/internal/delivery/http/user.go b/internal/delivery/http/user.go index 135393ed..8443cec0 100644 --- a/internal/delivery/http/user.go +++ b/internal/delivery/http/user.go @@ -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{}) diff --git a/internal/pagination/pagination.go b/internal/pagination/pagination.go index 01f8f513..40ed71ff 100644 --- a/internal/pagination/pagination.go +++ b/internal/pagination/pagination.go @@ -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{ diff --git a/internal/repository/user.go b/internal/repository/user.go index 5188c0d7..5403d81f 100644 --- a/internal/repository/user.go +++ b/internal/repository/user.go @@ -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, @@ -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 { @@ -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 @@ -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 { diff --git a/internal/usecase/auth.go b/internal/usecase/auth.go index 668f665b..02b43103 100644 --- a/internal/usecase/auth.go +++ b/internal/usecase/auth.go @@ -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", "") @@ -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 { @@ -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)) } diff --git a/internal/usecase/user.go b/internal/usecase/user.go index 41147aa2..a65b6649 100644 --- a/internal/usecase/user.go +++ b/internal/usecase/user.go @@ -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 @@ -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) { @@ -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 @@ -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 @@ -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 { @@ -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 {