Files
cms-server/service/service_event/delete.go
2026-03-27 20:16:04 +08:00

92 lines
2.1 KiB
Go

package service_event
import (
"context"
"errors"
"nixcn-cms/data"
"nixcn-cms/internal/exception"
"nixcn-cms/service/shared"
"nixcn-cms/tracer"
"github.com/google/uuid"
"gorm.io/gorm"
)
type EventDeleteData struct {
EventId string `json:"event_id" validate:"required"`
}
type EventDeletePayload struct {
Context context.Context
Data *EventDeleteData
}
type EventDeleteResult struct {
Common shared.CommonResult
}
func (self *EventServiceImpl) Delete(payload *EventDeletePayload) (result *EventDeleteResult) {
ctx, span := tracer.StartSpan(
payload.Context,
"service_event",
"delete",
)
defer span.End()
ctx = exception.ContextWithService(ctx, exception.ServiceEventDelete)
eventId, err := uuid.Parse(payload.Data.EventId)
if err != nil {
exc := exception.New(
exception.WithStatus(exception.StatusUser),
exception.WithType(exception.TypeCommon),
exception.WithOriginal(exception.CommonErrorUuidParseFailed),
exception.WithError(err),
).Throw(ctx)
result = &EventDeleteResult{
Common: shared.CommonResult{HttpCode: 400, Exception: exc},
}
return
}
if err := new(data.Event).DeleteEventById(ctx, eventId); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
exc := exception.New(
exception.WithStatus(exception.StatusUser),
exception.WithType(exception.TypeSpecific),
exception.WithOriginal(exception.EventDeleteNotFound),
exception.WithError(err),
).Throw(ctx)
result = &EventDeleteResult{
Common: shared.CommonResult{HttpCode: 404, Exception: exc},
}
return
}
exc := exception.New(
exception.WithStatus(exception.StatusServer),
exception.WithType(exception.TypeCommon),
exception.WithOriginal(exception.CommonErrorDatabase),
exception.WithError(err),
).Throw(ctx)
result = &EventDeleteResult{
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 = &EventDeleteResult{
Common: shared.CommonResult{HttpCode: 200, Exception: exc},
}
return
}