package event import ( "nixcn-cms/data" "nixcn-cms/internal/exception" "nixcn-cms/utils" "time" "github.com/gin-gonic/gin" "github.com/google/uuid" ) func Checkin(c *gin.Context) { data := new(data.Attendance) userIdOrig, ok := c.Get("user_id") if !ok { errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceEvent). SetEndpoint(exception.EndpointEventServiceCheckin). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorMissingUserId). Build(c) utils.HttpResponse(c, 403, errorCode) return } userId, err := uuid.Parse(userIdOrig.(string)) if err != nil { errorCode := new(exception.Builder). SetStatus(exception.StatusServer). SetService(exception.ServiceEvent). SetEndpoint(exception.EndpointEventServiceCheckin). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorUuidParseFailed). SetError(err). Build(c) utils.HttpResponse(c, 500, errorCode) } // Get event id from query eventIdOrig, ok := c.GetQuery("event_id") if !ok { errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceEvent). SetEndpoint(exception.EndpointEventServiceCheckin). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorInvalidInput). Build(c) utils.HttpResponse(c, 400, errorCode) return } // Parse event id to uuid eventId, err := uuid.Parse(eventIdOrig) if err != nil { errorCode := new(exception.Builder). SetStatus(exception.StatusServer). SetService(exception.ServiceEvent). SetEndpoint(exception.EndpointEventServiceCheckin). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorUuidParseFailed). SetError(err). Build(c) utils.HttpResponse(c, 500, errorCode) return } data.UserId = userId code, err := data.GenCheckinCode(c, eventId) if err != nil { errorCode := new(exception.Builder). SetStatus(exception.StatusServer). SetService(exception.ServiceEvent). SetEndpoint(exception.EndpointEventServiceCheckin). SetType(exception.TypeSpecific). SetOriginal(exception.EventCheckinGenCodeFailed). SetError(err). Build(c) utils.HttpResponse(c, 500, errorCode) return } checkinCodeResp := struct { CheckinCode *string `json:"checkin_code"` }{code} utils.HttpResponse(c, 200, "", "success", checkinCodeResp) } func CheckinSubmit(c *gin.Context) { var req struct { ChekinCode string `json:"checkin_code"` } c.ShouldBindJSON(&req) attendanceData := new(data.Attendance) err := attendanceData.VerifyCheckinCode(c, req.ChekinCode) if err != nil { errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceEvent). SetEndpoint(exception.EndpointEventServiceCheckinSubmit). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorInvalidInput). SetError(err). Build(c) utils.HttpResponse(c, 400, errorCode) return } utils.HttpResponse(c, 200, "", "success") } func CheckinQuery(c *gin.Context) { userIdOrig, ok := c.Get("user_id") if !ok { errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceEvent). SetEndpoint(exception.EndpointEventServiceCheckinQuery). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorMissingUserId). Build(c) utils.HttpResponse(c, 400, errorCode) return } userId, err := uuid.Parse(userIdOrig.(string)) if err != nil { errorCode := new(exception.Builder). SetStatus(exception.StatusServer). SetService(exception.ServiceEvent). SetEndpoint(exception.EndpointEventServiceCheckinQuery). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorUuidParseFailed). SetError(err). Build(c) utils.HttpResponse(c, 500, errorCode) return } eventIdOrig, ok := c.GetQuery("event_id") if !ok { errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceEvent). SetEndpoint(exception.EndpointEventServiceCheckinQuery). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorInvalidInput). Build(c) utils.HttpResponse(c, 400, errorCode) return } eventId, err := uuid.Parse(eventIdOrig) if err != nil { errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceEvent). SetEndpoint(exception.EndpointEventServiceCheckinQuery). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorInvalidInput). SetError(err). Build(c) utils.HttpResponse(c, 400, errorCode) return } attendanceData := new(data.Attendance) attendance, err := attendanceData.GetAttendance(c, userId, eventId) if err != nil { errorCode := new(exception.Builder). SetStatus(exception.StatusServer). SetService(exception.ServiceEvent). SetEndpoint(exception.EndpointEventServiceCheckinQuery). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorDatabase). SetError(err). Build(c) utils.HttpResponse(c, 500, errorCode) return } else if attendance == nil { errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceEvent). SetEndpoint(exception.EndpointEventServiceCheckinQuery). SetType(exception.TypeSpecific). SetOriginal(exception.EventCheckinQueryRecordNotFound). Build(c) utils.HttpResponse(c, 404, errorCode) return } else if attendance.CheckinAt.IsZero() { utils.HttpResponse(c, 200, "", "success", gin.H{"checkin_at": nil}) return } checkInAtResp := struct { CheckinAt time.Time `json:"checkin_at"` }{attendance.CheckinAt} errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceEvent). SetEndpoint(exception.EndpointEventServiceCheckinQuery). SetType(exception.TypeCommon). SetOriginal(exception.CommonSuccess). Build(c) utils.HttpResponse(c, 200, errorCode, checkInAtResp) }