132 lines
2.9 KiB
Go
132 lines
2.9 KiB
Go
package service_agenda
|
|
|
|
import (
|
|
"context"
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/internal/exception"
|
|
"nixcn-cms/service/shared"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type SubmitData struct {
|
|
EventId uuid.UUID `json:"event_id"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
}
|
|
|
|
type SubmitPayload struct {
|
|
Context context.Context
|
|
UserId uuid.UUID `json:"user_id"`
|
|
Data *SubmitData
|
|
}
|
|
|
|
type SubmitResponse struct {
|
|
AgendaId uuid.UUID `json:"agenda_id"`
|
|
}
|
|
|
|
type SubmitResult struct {
|
|
Common shared.CommonResult
|
|
Data *SubmitResponse
|
|
}
|
|
|
|
func (self *AgendaServiceImpl) Submit(payload *SubmitPayload) (result *SubmitResult) {
|
|
var err error
|
|
|
|
attendanceData, err := new(data.Attendance).
|
|
GetAttendance(payload.Context, payload.UserId, payload.Data.EventId)
|
|
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceAgenda).
|
|
SetEndpoint(exception.EndpointAgendaServiceSubmit).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &SubmitResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 403,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceAgenda).
|
|
SetEndpoint(exception.EndpointAgendaServiceSubmit).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorDatabase).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &SubmitResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
agendaModel := new(data.Agenda).
|
|
SetAttendanceId(attendanceData.AttendanceId).
|
|
SetName(payload.Data.Name).
|
|
SetDescription(payload.Data.Description).
|
|
SetIsApproved(false)
|
|
|
|
err = agendaModel.Create(payload.Context)
|
|
if err != nil {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceAgenda).
|
|
SetEndpoint(exception.EndpointAgendaServiceSubmit).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorDatabase).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &SubmitResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
successException := new(exception.Builder).
|
|
SetStatus(exception.StatusSuccess).
|
|
SetService(exception.ServiceAgenda).
|
|
SetEndpoint(exception.EndpointAgendaServiceSubmit).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonSuccess).
|
|
SetError(nil).
|
|
Throw(payload.Context)
|
|
|
|
resultData := &SubmitResponse{
|
|
AgendaId: agendaModel.AgendaId,
|
|
}
|
|
|
|
result = &SubmitResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 200,
|
|
Exception: successException,
|
|
},
|
|
Data: resultData,
|
|
}
|
|
|
|
return
|
|
}
|