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
|
||||
}
|
||||
132
service/service_event/get_joined_event.go
Normal file
132
service/service_event/get_joined_event.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package service_event
|
||||
|
||||
import (
|
||||
"context"
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/service/shared"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type JoinedEventListData struct {
|
||||
Limit *string `json:"limit"`
|
||||
Offset *string `json:"offset"`
|
||||
}
|
||||
|
||||
type JoinedEventListPayload struct {
|
||||
Context context.Context
|
||||
UserId uuid.UUID
|
||||
Data *JoinedEventListData
|
||||
}
|
||||
|
||||
type JoinedEventListResult struct {
|
||||
Common shared.CommonResult
|
||||
Data *[]data.EventIndexDoc `json:"event_list"`
|
||||
}
|
||||
|
||||
func (self *EventServiceImpl) GetJoinedEvent(payload *JoinedEventListPayload) (result *JoinedEventListResult) {
|
||||
var limit string
|
||||
if payload.Data.Limit == nil || *payload.Data.Limit == "" {
|
||||
limit = "20"
|
||||
} else {
|
||||
limit = *payload.Data.Limit
|
||||
}
|
||||
|
||||
var offset string
|
||||
if payload.Data.Offset == nil || *payload.Data.Offset == "" {
|
||||
exc := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceEvent).
|
||||
SetEndpoint(exception.EndpointEventServiceList).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
SetError(nil).
|
||||
Throw(payload.Context)
|
||||
|
||||
return &JoinedEventListResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 400,
|
||||
Exception: exc,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
offset = *payload.Data.Offset
|
||||
}
|
||||
|
||||
limitNum, err := strconv.Atoi(limit)
|
||||
if err != nil {
|
||||
exc := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceEvent).
|
||||
SetEndpoint(exception.EndpointEventServiceList).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
return &JoinedEventListResult{
|
||||
Common: shared.CommonResult{HttpCode: 400, Exception: exc},
|
||||
}
|
||||
}
|
||||
|
||||
offsetNum, err := strconv.Atoi(offset)
|
||||
if err != nil {
|
||||
exc := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceEvent).
|
||||
SetEndpoint(exception.EndpointEventServiceList).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
return &JoinedEventListResult{
|
||||
Common: shared.CommonResult{HttpCode: 400, Exception: exc},
|
||||
}
|
||||
}
|
||||
|
||||
eventList, err := new(data.Event).
|
||||
GetEventsByUserId(payload.Context, payload.UserId, int64(limitNum), int64(offsetNum))
|
||||
|
||||
if err != nil {
|
||||
exc := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceEvent).
|
||||
SetEndpoint(exception.EndpointEventServiceList).
|
||||
SetType(exception.TypeSpecific).
|
||||
SetOriginal(exception.EventListDatabaseFailed).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
return &JoinedEventListResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 500,
|
||||
Exception: exc,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if eventList != nil {
|
||||
for i := range *eventList {
|
||||
(*eventList)[i].IsJoined = true
|
||||
}
|
||||
}
|
||||
|
||||
successExc := new(exception.Builder).
|
||||
SetStatus(exception.StatusSuccess).
|
||||
SetService(exception.ServiceEvent).
|
||||
SetEndpoint(exception.EndpointEventServiceList).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonSuccess).
|
||||
Throw(payload.Context)
|
||||
|
||||
return &JoinedEventListResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 200,
|
||||
Exception: successExc,
|
||||
},
|
||||
Data: eventList,
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ type EventService interface {
|
||||
ListEvents(*EventListPayload) *EventListResult
|
||||
JoinEvent(*EventJoinPayload) *EventJoinResult
|
||||
AttendanceList(*AttendanceListPayload) *AttendanceListResult
|
||||
GetJoinedEvent(*JoinedEventListPayload) *JoinedEventListResult
|
||||
}
|
||||
|
||||
type EventServiceImpl struct{}
|
||||
|
||||
Reference in New Issue
Block a user