192 lines
4.7 KiB
Go
192 lines
4.7 KiB
Go
package service_event
|
|
|
|
import (
|
|
"context"
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/internal/exception"
|
|
"nixcn-cms/service/shared"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type CheckinData struct {
|
|
EventId uuid.UUID `json:"event_id"`
|
|
}
|
|
|
|
type CheckinPayload struct {
|
|
Context context.Context
|
|
UserId uuid.UUID
|
|
Data *CheckinData
|
|
}
|
|
|
|
type CheckinResult struct {
|
|
Common shared.CommonResult
|
|
Data *struct {
|
|
CheckinCode *string `json:"checkin_code"`
|
|
}
|
|
}
|
|
|
|
func (self *EventServiceImpl) Checkin(payload *CheckinPayload) (result *CheckinResult) {
|
|
attendance := &data.Attendance{UserId: payload.UserId}
|
|
code, err := attendance.GenCheckinCode(payload.Context, payload.Data.EventId)
|
|
if err != nil {
|
|
result = &CheckinResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceEvent).
|
|
SetEndpoint(exception.EndpointEventServiceCheckin).
|
|
SetType(exception.TypeSpecific).
|
|
SetOriginal(exception.EventCheckinGenCodeFailed).
|
|
SetError(err).
|
|
Throw(payload.Context),
|
|
},
|
|
}
|
|
return
|
|
}
|
|
|
|
result = &CheckinResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 200,
|
|
Exception: new(exception.Builder).
|
|
SetStatus(exception.StatusSuccess).
|
|
SetService(exception.ServiceEvent).
|
|
SetEndpoint(exception.EndpointEventServiceCheckin).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonSuccess).
|
|
Throw(payload.Context),
|
|
},
|
|
Data: &struct {
|
|
CheckinCode *string `json:"checkin_code"`
|
|
}{code},
|
|
}
|
|
return
|
|
}
|
|
|
|
type CheckinSubmitData struct {
|
|
CheckinCode string `json:"checkin_code"`
|
|
}
|
|
|
|
type CheckinSubmitPayload struct {
|
|
Context context.Context
|
|
Data *CheckinSubmitData
|
|
}
|
|
|
|
type CheckinSubmitResult struct {
|
|
Common shared.CommonResult
|
|
}
|
|
|
|
func (self *EventServiceImpl) CheckinSubmit(payload *CheckinSubmitPayload) (result *CheckinSubmitResult) {
|
|
attendanceData := new(data.Attendance)
|
|
err := attendanceData.VerifyCheckinCode(payload.Context, payload.Data.CheckinCode)
|
|
if err != nil {
|
|
result = &CheckinSubmitResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceEvent).
|
|
SetEndpoint(exception.EndpointEventServiceCheckinSubmit).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
SetError(err).
|
|
Throw(payload.Context),
|
|
},
|
|
}
|
|
return
|
|
}
|
|
|
|
result = &CheckinSubmitResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 200,
|
|
Exception: new(exception.Builder).
|
|
SetStatus(exception.StatusSuccess).
|
|
SetService(exception.ServiceEvent).
|
|
SetEndpoint(exception.EndpointEventServiceCheckinSubmit).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonSuccess).
|
|
Throw(payload.Context),
|
|
},
|
|
}
|
|
return
|
|
}
|
|
|
|
type CheckinQueryData struct {
|
|
EventId uuid.UUID `json:"event_id"`
|
|
}
|
|
|
|
type CheckinQueryPayload struct {
|
|
Context context.Context
|
|
UserId uuid.UUID
|
|
Data *CheckinQueryData
|
|
}
|
|
|
|
type CheckinQueryResult struct {
|
|
Common shared.CommonResult
|
|
Data *struct {
|
|
CheckinAt *time.Time `json:"checkin_at"`
|
|
}
|
|
}
|
|
|
|
func (self *EventServiceImpl) CheckinQuery(payload *CheckinQueryPayload) (result *CheckinQueryResult) {
|
|
attendanceData := new(data.Attendance)
|
|
attendance, err := attendanceData.GetAttendance(payload.Context, payload.UserId, payload.Data.EventId)
|
|
|
|
if err != nil {
|
|
result = &CheckinQueryResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceEvent).
|
|
SetEndpoint(exception.EndpointEventServiceCheckinQuery).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorDatabase).
|
|
SetError(err).
|
|
Throw(payload.Context),
|
|
},
|
|
}
|
|
return
|
|
}
|
|
|
|
if attendance == nil {
|
|
result = &CheckinQueryResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 404,
|
|
Exception: new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceEvent).
|
|
SetEndpoint(exception.EndpointEventServiceCheckinQuery).
|
|
SetType(exception.TypeSpecific).
|
|
SetOriginal(exception.EventCheckinQueryRecordNotFound).
|
|
Throw(payload.Context),
|
|
},
|
|
}
|
|
return
|
|
}
|
|
|
|
var checkinAt *time.Time
|
|
if !attendance.CheckinAt.IsZero() {
|
|
checkinAt = &attendance.CheckinAt
|
|
}
|
|
|
|
result = &CheckinQueryResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 200,
|
|
Exception: new(exception.Builder).
|
|
SetStatus(exception.StatusSuccess).
|
|
SetService(exception.ServiceEvent).
|
|
SetEndpoint(exception.EndpointEventServiceCheckinQuery).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonSuccess).
|
|
Throw(payload.Context),
|
|
},
|
|
Data: &struct {
|
|
CheckinAt *time.Time `json:"checkin_at"`
|
|
}{checkinAt},
|
|
}
|
|
return
|
|
}
|