141 lines
3.1 KiB
Go
141 lines
3.1 KiB
Go
package service_user
|
|
|
|
import (
|
|
"context"
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/internal/exception"
|
|
"nixcn-cms/service/shared"
|
|
"strconv"
|
|
)
|
|
|
|
type UserListPayload struct {
|
|
Context context.Context
|
|
Limit *string
|
|
Offset *string
|
|
}
|
|
|
|
type UserListResult struct {
|
|
Common shared.CommonResult
|
|
Data *[]data.UserIndexDoc `json:"user_list"`
|
|
}
|
|
|
|
func (self *UserServiceImpl) ListUsers(payload *UserListPayload) (result *UserListResult) {
|
|
var limit string = *payload.Limit
|
|
if payload.Limit == nil || *payload.Limit == "" {
|
|
limit = "20"
|
|
} else {
|
|
limit = *payload.Limit
|
|
}
|
|
|
|
var offset string = *payload.Offset
|
|
if payload.Offset == nil || *payload.Offset == "" {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceList).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
SetError(nil).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserListResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
} else {
|
|
offset = *payload.Offset
|
|
}
|
|
|
|
// Parse string to int64
|
|
limitNum, err := strconv.Atoi(limit)
|
|
if err != nil {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceList).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserListResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
offsetNum, err := strconv.Atoi(offset)
|
|
if err != nil {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceList).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserListResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Get user list from search engine
|
|
userList, err := new(data.User).
|
|
FastListUsers(payload.Context, &limitNum, &offsetNum)
|
|
if err != nil {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceList).
|
|
SetType(exception.TypeSpecific).
|
|
SetOriginal(exception.UserListDatabaseFailed).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserListResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
}
|
|
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceList).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonSuccess).
|
|
SetError(nil).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserListResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 200,
|
|
Exception: exception,
|
|
},
|
|
Data: userList,
|
|
}
|
|
|
|
return
|
|
|
|
}
|