Files
2026-03-27 20:16:04 +08:00

144 lines
3.4 KiB
Go

package service_user
import (
"context"
"errors"
"nixcn-cms/data"
"nixcn-cms/internal/exception"
"nixcn-cms/service/shared"
"nixcn-cms/tracer"
"strconv"
)
type UserListData struct {
Limit *string
Offset *string `validate:"required"`
SortBy *string
SortOrder *string
PermissionLevel *uint
}
type UserListPayload struct {
Context context.Context
Data *UserListData
}
type UserListResponse struct {
data.UserAdminDoc
}
type UserListResult struct {
Common shared.CommonResult
Total int64
Data *[]UserListResponse `json:"user_list"`
}
func (self *UserServiceImpl) List(payload *UserListPayload) (result *UserListResult) {
ctx, span := tracer.StartSpan(
payload.Context,
"service_user",
"list",
)
defer span.End()
ctx = exception.ContextWithService(ctx, exception.ServiceUserList)
limit := 20
if payload.Data.Limit != nil && *payload.Data.Limit != "" {
v, err := strconv.Atoi(*payload.Data.Limit)
if err != nil {
exc := exception.New(
exception.WithStatus(exception.StatusUser),
exception.WithType(exception.TypeCommon),
exception.WithOriginal(exception.CommonErrorInvalidInput),
exception.WithError(err),
).Throw(ctx)
result = &UserListResult{
Common: shared.CommonResult{HttpCode: 400, Exception: exc},
}
return
}
limit = v
}
var offset string
if payload.Data.Offset == nil || *payload.Data.Offset == "" {
exc := exception.New(
exception.WithStatus(exception.StatusUser),
exception.WithType(exception.TypeCommon),
exception.WithOriginal(exception.CommonErrorInvalidInput),
exception.WithError(errors.New("offset is required")),
).Throw(ctx)
result = &UserListResult{
Common: shared.CommonResult{HttpCode: 400, Exception: exc},
}
return
} else {
offset = *payload.Data.Offset
}
// Parse string to int64
offsetNum, err := strconv.Atoi(offset)
if err != nil {
exc := exception.New(
exception.WithStatus(exception.StatusUser),
exception.WithType(exception.TypeCommon),
exception.WithOriginal(exception.CommonErrorInvalidInput),
exception.WithError(err),
).Throw(ctx)
result = &UserListResult{
Common: shared.CommonResult{HttpCode: 400, Exception: exc},
}
return
}
opts := data.UserListOptions{
Limit: limit,
Offset: offsetNum,
PermissionLevel: payload.Data.PermissionLevel,
}
if payload.Data.SortBy != nil {
opts.SortBy = *payload.Data.SortBy
}
if payload.Data.SortOrder != nil {
opts.SortOrder = *payload.Data.SortOrder
}
userList, total, err := new(data.User).ListUsersFiltered(ctx, opts)
if err != nil {
exc := exception.New(
exception.WithStatus(exception.StatusServer),
exception.WithType(exception.TypeSpecific),
exception.WithOriginal(exception.UserListDatabaseFailed),
exception.WithError(err),
).Throw(ctx)
result = &UserListResult{
Common: shared.CommonResult{HttpCode: 500, Exception: exc},
}
return
}
response := make([]UserListResponse, 0, len(*userList))
for _, doc := range *userList {
response = append(response, UserListResponse{UserAdminDoc: doc})
}
exc := exception.New(
exception.WithStatus(exception.StatusSuccess),
exception.WithType(exception.TypeCommon),
exception.WithOriginal(exception.CommonSuccess),
).Throw(ctx)
result = &UserListResult{
Common: shared.CommonResult{HttpCode: 200, Exception: exc},
Total: total,
Data: &response,
}
return
}