Add agenda service and submit api
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
11
service/service_agenda/service.go
Normal file
11
service/service_agenda/service.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package service_agenda
|
||||
|
||||
type AgendaService interface {
|
||||
Submit(*SubmitPayload) *SubmitResult
|
||||
}
|
||||
|
||||
type AgendaServiceImpl struct{}
|
||||
|
||||
func NewAgendaService() AgendaService {
|
||||
return &AgendaServiceImpl{}
|
||||
}
|
||||
131
service/service_agenda/submit.go
Normal file
131
service/service_agenda/submit.go
Normal file
@@ -0,0 +1,131 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user