Files
cms-server/api/event/create.go
Asai Neko c540235c6d
All checks were successful
Server Check Build (NixCN CMS) TeamCity build finished
Full restruct data layer without alibaba style SetXxx to Functional
Options

Signed-off-by: Asai Neko <sugar@sne.moe>
2026-03-26 16:32:51 +08:00

92 lines
3.0 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"
)
// Create handles the request to create a new event.
//
// @Summary Create an Event
// @Description Allows a Lv30+ user to create a new event. Users at exactly Lv30 may only create events with type 'party'. Sets type and enable_kyc, which are immutable after creation.
// @Tags Event
// @Accept json
// @Produce json
// @Security Bearer
// @Param request body service_event.EventCreateData true "Event Creation Details"
// @Success 200 {object} utils.RespStatus{data=service_event.EventCreateResponse} "Successfully created the event"
// @Failure 400 {object} utils.RespStatus{data=nil} "Invalid Input"
// @Failure 401 {object} utils.RespStatus{data=nil} "Missing User ID / Unauthorized"
// @Failure 403 {object} utils.RespStatus{data=nil} "Permission Denied / Type Not Allowed for this level"
// @Failure 500 {object} utils.RespStatus{data=nil} "Internal Server Error / Database Error"
// @Router /event/create [post]
func (self *EventHandler) Create(c *gin.Context) {
ctx, span := tracer.StartSpan(
c.Request.Context(),
"api_event",
"create",
)
defer span.End()
ctx = exception.ContextWithEndpoint(ctx, exception.EndpointEventCreate)
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
}
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
}
var createData service_event.EventCreateData
if err := c.ShouldBindJSON(&createData); 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
}
createData.UserId = userIdOrig.(string)
createData.PermissionLevel = permissionLevelOrig.(uint)
payload := &service_event.EventCreatePayload{
Context: ctx,
Data: &createData,
}
result := self.svc.Create(payload)
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)
}