161 lines
4.1 KiB
Go
161 lines
4.1 KiB
Go
package service_event
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/internal/exception"
|
|
"nixcn-cms/service/shared"
|
|
"nixcn-cms/tracer"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type EventCreateData struct {
|
|
Type string `json:"type" validate:"required"`
|
|
EnableKYC bool `json:"enable_kyc"`
|
|
Name string `json:"name" validate:"required"`
|
|
Subtitle string `json:"subtitle"`
|
|
Description string `json:"description"`
|
|
AttendanceGuide string `json:"attendance_guide"`
|
|
StartTime time.Time `json:"start_time"`
|
|
EndTime time.Time `json:"end_time"`
|
|
Thumbnail string `json:"thumbnail"`
|
|
Quota int64 `json:"quota"`
|
|
Limit int64 `json:"limit"`
|
|
UserId string `json:"user_id" swaggerignore:"true"`
|
|
PermissionLevel uint `json:"permission_level" swaggerignore:"true"`
|
|
}
|
|
|
|
type EventCreatePayload struct {
|
|
Context context.Context
|
|
Data *EventCreateData
|
|
}
|
|
|
|
type EventCreateResponse struct {
|
|
EventId string `json:"event_id" validate:"required"`
|
|
}
|
|
|
|
type EventCreateResult struct {
|
|
Common shared.CommonResult
|
|
Data *EventCreateResponse
|
|
}
|
|
|
|
func (self *EventServiceImpl) Create(payload *EventCreatePayload) (result *EventCreateResult) {
|
|
ctx, span := tracer.StartSpan(
|
|
payload.Context,
|
|
"service_event",
|
|
"create",
|
|
)
|
|
defer span.End()
|
|
|
|
ctx = exception.ContextWithService(ctx, exception.ServiceEventCreate)
|
|
|
|
if payload.Data.Type != "party" && payload.Data.Type != "official" {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusUser),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorInvalidInput),
|
|
exception.WithError(errors.New("type must be 'party' or 'official'")),
|
|
).Throw(ctx)
|
|
|
|
result = &EventCreateResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: exc,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
if payload.Data.PermissionLevel == 30 && payload.Data.Type != "party" {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusUser),
|
|
exception.WithType(exception.TypeSpecific),
|
|
exception.WithOriginal(exception.EventCreateTypeNotAllowed),
|
|
exception.WithError(errors.New("Lv30 users may only create events with type 'party'")),
|
|
).Throw(ctx)
|
|
|
|
result = &EventCreateResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 403,
|
|
Exception: exc,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
ownerId, err := uuid.Parse(payload.Data.UserId)
|
|
if err != nil {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusServer),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorUuidParseFailed),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &EventCreateResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exc,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
eventData := data.NewEvent(
|
|
data.WithEventType(payload.Data.Type),
|
|
data.WithEnableKYC(payload.Data.EnableKYC),
|
|
data.WithEventName(payload.Data.Name),
|
|
data.WithEventSubtitle(payload.Data.Subtitle),
|
|
data.WithEventDescription(payload.Data.Description),
|
|
data.WithAttendanceGuide(payload.Data.AttendanceGuide),
|
|
data.WithEventStartTime(payload.Data.StartTime),
|
|
data.WithEventEndTime(payload.Data.EndTime),
|
|
data.WithThumbnail(payload.Data.Thumbnail),
|
|
data.WithQuota(payload.Data.Quota),
|
|
data.WithLimit(payload.Data.Limit),
|
|
data.WithOwner(ownerId),
|
|
)
|
|
|
|
if err := eventData.Create(ctx); err != nil {
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusServer),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorDatabase),
|
|
exception.WithError(err),
|
|
).Throw(ctx)
|
|
|
|
result = &EventCreateResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exc,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
exc := exception.New(
|
|
exception.WithStatus(exception.StatusSuccess),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonSuccess),
|
|
).Throw(ctx)
|
|
|
|
result = &EventCreateResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 200,
|
|
Exception: exc,
|
|
},
|
|
Data: &EventCreateResponse{
|
|
EventId: eventData.EventId.String(),
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|