All checks were successful
Server Check Build (NixCN CMS) TeamCity build finished
Signed-off-by: Asai Neko <sugar@sne.moe>
100 lines
3.3 KiB
Go
100 lines
3.3 KiB
Go
package user
|
|
|
|
import (
|
|
"nixcn-cms/internal/exception"
|
|
"nixcn-cms/service/service_user"
|
|
"nixcn-cms/tracer"
|
|
"nixcn-cms/utils"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// List retrieves a paginated, filterable list of users. Lv40+ only.
|
|
//
|
|
// @Summary List Users (Admin)
|
|
// @Description Returns a paginated list of users with permission_level included. Supports filtering by permission_level and sorting.
|
|
// @Tags User
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param limit query string false "Maximum number of users to return (default 20)"
|
|
// @Param offset query string true "Number of users to skip"
|
|
// @Param sort_by query string false "Sort field: 'id' (default) | 'permission_level'"
|
|
// @Param sort_order query string false "Sort direction: 'asc' (default) | 'desc'"
|
|
// @Param permission_level query int false "Filter by exact permission level"
|
|
// @Success 200 {object} utils.RespStatus{data=[]service_user.UserListResponse} "Successful paginated list retrieval"
|
|
// @Failure 400 {object} utils.RespStatus{data=nil} "Invalid Input"
|
|
// @Failure 401 {object} utils.RespStatus{data=nil} "Unauthorized"
|
|
// @Failure 500 {object} utils.RespStatus{data=nil} "Internal Server Error"
|
|
// @Router /user/list [get]
|
|
func (self *UserHandler) List(c *gin.Context) {
|
|
ctx, span := tracer.StartSpan(
|
|
c.Request.Context(),
|
|
"api_user",
|
|
"list",
|
|
)
|
|
defer span.End()
|
|
|
|
ctx = exception.ContextWithEndpoint(ctx, exception.EndpointUserList)
|
|
ctx = exception.ContextWithService(ctx, exception.ServiceEndpoint)
|
|
|
|
type ListQuery struct {
|
|
Limit *string `form:"limit"`
|
|
Offset *string `form:"offset"`
|
|
SortBy *string `form:"sort_by"`
|
|
SortOrder *string `form:"sort_order"`
|
|
PermissionLevel *string `form:"permission_level"`
|
|
}
|
|
|
|
var query ListQuery
|
|
if err := c.ShouldBindQuery(&query); err != nil {
|
|
errorCode := exception.New(
|
|
exception.WithStatus(exception.StatusClient),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorInvalidInput),
|
|
exception.WithError(err),
|
|
).Throw(ctx).String()
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
|
|
var permLevel *uint
|
|
if query.PermissionLevel != nil && *query.PermissionLevel != "" {
|
|
v, err := strconv.ParseUint(*query.PermissionLevel, 10, 64)
|
|
if err != nil {
|
|
errorCode := exception.New(
|
|
exception.WithStatus(exception.StatusUser),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorInvalidInput),
|
|
exception.WithError(err),
|
|
).Throw(ctx).String()
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
u := uint(v)
|
|
permLevel = &u
|
|
}
|
|
|
|
result := self.svc.List(&service_user.UserListPayload{
|
|
Context: ctx,
|
|
Data: &service_user.UserListData{
|
|
Limit: query.Limit,
|
|
Offset: query.Offset,
|
|
SortBy: query.SortBy,
|
|
SortOrder: query.SortOrder,
|
|
PermissionLevel: permLevel,
|
|
},
|
|
})
|
|
|
|
if result.Common.Exception.Original != exception.CommonSuccess {
|
|
utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String())
|
|
return
|
|
}
|
|
|
|
utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String(), gin.H{
|
|
"total": result.Total,
|
|
"items": result.Data,
|
|
})
|
|
}
|