Files
cms-server/api/event/list.go
Asai Neko 3bbcd1caab
All checks were successful
Server Check Build (NixCN CMS) TeamCity build finished
Fix event list response the hole result struct
Signed-off-by: Asai Neko <sugar@sne.moe>
2026-03-27 17:56:36 +08:00

119 lines
3.9 KiB
Go

package event
import (
"errors"
"nixcn-cms/internal/exception"
"nixcn-cms/service/service_event"
"nixcn-cms/tracer"
"nixcn-cms/utils"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
// List retrieves a paginated, filterable list of events.
//
// @Summary List Events
// @Description Returns a paginated list of events. Supports filtering by type and sorting. Lv30 users are automatically scoped to events they own.
// @Tags Event
// @Accept json
// @Produce json
// @Security Bearer
// @Param limit query int false "Maximum number of events to return (default 20)"
// @Param offset query int true "Number of events to skip"
// @Param type query string false "Filter by event type: 'official' or 'party'"
// @Param sort_by query string false "Sort field: 'start_time' (default), 'end_time', 'name'"
// @Param sort_order query string false "Sort direction: 'asc' or 'desc' (default)"
// @Success 200 {object} utils.RespStatus{data=service_event.EventListResponse} "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 /event/list [get]
func (self *EventHandler) List(c *gin.Context) {
ctx, span := tracer.StartSpan(
c.Request.Context(),
"api_event",
"list",
)
defer span.End()
ctx = exception.ContextWithEndpoint(ctx, exception.EndpointEventList)
ctx = exception.ContextWithService(ctx, exception.ServiceEndpoint)
userIdOrig, ok := c.Get("user_id")
if !ok {
errorCode := exception.New(
exception.WithStatus(exception.StatusUser),
exception.WithType(exception.TypeCommon),
exception.WithOriginal(exception.CommonErrorMissingUserId),
exception.WithError(errors.New("Missing UserId")),
).Throw(ctx).String()
utils.HttpResponse(c, 403, errorCode)
return
}
userId, err := uuid.Parse(userIdOrig.(string))
if err != nil {
errorCode := exception.New(
exception.WithStatus(exception.StatusServer),
exception.WithType(exception.TypeCommon),
exception.WithOriginal(exception.CommonErrorUuidParseFailed),
exception.WithError(err),
).Throw(ctx).String()
utils.HttpResponse(c, 500, errorCode)
return
}
permissionLevelOrig, ok := c.Get("permission_level")
if !ok {
errorCode := exception.New(
exception.WithStatus(exception.StatusUser),
exception.WithType(exception.TypeCommon),
exception.WithOriginal(exception.CommonErrorPermissionDenied),
exception.WithError(errors.New("Missing PermissionLevel")),
).Throw(ctx).String()
utils.HttpResponse(c, 403, errorCode)
return
}
type ListQuery struct {
Limit *string `form:"limit"`
Offset *string `form:"offset"`
Type *string `form:"type"`
SortBy *string `form:"sort_by"`
SortOrder *string `form:"sort_order"`
}
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
}
result := self.svc.List(&service_event.EventListPayload{
Context: ctx,
UserId: userId,
Data: &service_event.EventListData{
Limit: query.Limit,
Offset: query.Offset,
Type: query.Type,
SortBy: query.SortBy,
SortOrder: query.SortOrder,
PermissionLevel: permissionLevelOrig.(uint),
},
})
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(), result.Data)
}