146 lines
3.5 KiB
Go
146 lines
3.5 KiB
Go
package service_event
|
|
|
|
import (
|
|
"context"
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/internal/exception"
|
|
"nixcn-cms/service/shared"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type EventInfoData struct {
|
|
EventId uuid.UUID `json:"event_id"`
|
|
}
|
|
|
|
type EventInfoPayload struct {
|
|
Context context.Context
|
|
UserId uuid.UUID
|
|
Data *EventInfoData
|
|
}
|
|
|
|
type EventInfoResult struct {
|
|
Common shared.CommonResult
|
|
Data *data.EventIndexDoc
|
|
}
|
|
|
|
func (self *EventServiceImpl) GetEventInfo(payload *EventInfoPayload) (result *EventInfoResult) {
|
|
event, err := new(data.Event).GetEventById(payload.Context, payload.Data.EventId)
|
|
if err != nil {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceEvent).
|
|
SetEndpoint(exception.EndpointEventServiceInfo).
|
|
SetType(exception.TypeSpecific).
|
|
SetOriginal(exception.EventInfoNotFound).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &EventInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 404,
|
|
Exception: exception,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
joinCount, err := new(data.Attendance).CountUsersByEventID(payload.Context, payload.Data.EventId)
|
|
if err != nil {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceEvent).
|
|
SetEndpoint(exception.EndpointEventServiceInfo).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorDatabase).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &EventInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exception,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
checkinCount, err := new(data.Attendance).CountCheckedInUsersByEventID(payload.Context, payload.Data.EventId)
|
|
if err != nil {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceEvent).
|
|
SetEndpoint(exception.EndpointEventServiceInfo).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorDatabase).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &EventInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exception,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
var isJoined bool
|
|
joinedInfo, err := new(data.Attendance).GetAttendance(payload.Context, payload.UserId, payload.Data.EventId)
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceEvent).
|
|
SetEndpoint(exception.EndpointEventServiceInfo).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorDatabase).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &EventInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exception,
|
|
},
|
|
}
|
|
|
|
return
|
|
} else if err == gorm.ErrRecordNotFound {
|
|
isJoined = false
|
|
} else if joinedInfo.AttendanceId != uuid.Nil {
|
|
isJoined = true
|
|
}
|
|
|
|
result = &EventInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 200,
|
|
Exception: new(exception.Builder).
|
|
SetStatus(exception.StatusSuccess).
|
|
SetService(exception.ServiceEvent).
|
|
SetEndpoint(exception.EndpointEventServiceInfo).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonSuccess).
|
|
Throw(payload.Context),
|
|
},
|
|
Data: &data.EventIndexDoc{
|
|
EventId: event.EventId.String(),
|
|
Name: event.Name,
|
|
Type: event.Type,
|
|
Description: event.Description,
|
|
StartTime: event.StartTime,
|
|
EndTime: event.EndTime,
|
|
Thumbnail: event.Thumbnail,
|
|
EnableKYC: event.EnableKYC,
|
|
IsJoined: isJoined,
|
|
JoinCount: joinCount,
|
|
CheckinCount: checkinCount,
|
|
},
|
|
}
|
|
|
|
return
|
|
}
|