- Replace hardcoded error messages with structured error codes using exception.Builder. - Introduce new common error constants in exception/common.go (CommonErrorInvalidInput, CommonErrorUserNotFound, etc.). - Update exception/specific.go with domain-specific errors and remove redundant ones. - Apply consistent error handling across auth, event, user services and middleware. Co-authored-by: Gemini <gemini@google.com> Signed-off-by: Noa Virellia <noa@requiem.garden>
194 lines
5.5 KiB
Go
194 lines
5.5 KiB
Go
package event
|
|
|
|
import (
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/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.ErrorStatusUser).
|
|
SetService(exception.EventService).
|
|
SetEndpoint(exception.EventCheckinEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorMissingUserId).
|
|
Build()
|
|
utils.HttpResponse(c, 403, errorCode)
|
|
return
|
|
}
|
|
userId, err := uuid.Parse(userIdOrig.(string))
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusServer).
|
|
SetService(exception.EventService).
|
|
SetEndpoint(exception.EventCheckinEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorUuidParseFailed).
|
|
Build()
|
|
utils.HttpResponse(c, 500, errorCode)
|
|
}
|
|
|
|
// Get event id from query
|
|
eventIdOrig, ok := c.GetQuery("event_id")
|
|
if !ok {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusClient).
|
|
SetService(exception.EventService).
|
|
SetEndpoint(exception.EventCheckinEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
Build()
|
|
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.ErrorStatusServer).
|
|
SetService(exception.EventService).
|
|
SetEndpoint(exception.EventCheckinEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorUuidParseFailed).
|
|
Build()
|
|
utils.HttpResponse(c, 500, errorCode)
|
|
return
|
|
}
|
|
data.UserId = userId
|
|
code, err := data.GenCheckinCode(eventId)
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusServer).
|
|
SetService(exception.EventService).
|
|
SetEndpoint(exception.EventCheckinEndpoint).
|
|
SetType(exception.ErrorTypeSpecific).
|
|
SetOriginal(exception.EventCheckinGenCodeFailed).
|
|
Build()
|
|
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(req.ChekinCode)
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusClient).
|
|
SetService(exception.EventService).
|
|
SetEndpoint(exception.EventCheckinSubmitEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
Build()
|
|
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.ErrorStatusUser).
|
|
SetService(exception.EventService).
|
|
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorMissingUserId).
|
|
Build()
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
userId, err := uuid.Parse(userIdOrig.(string))
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusServer).
|
|
SetService(exception.EventService).
|
|
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorUuidParseFailed).
|
|
Build()
|
|
utils.HttpResponse(c, 500, errorCode)
|
|
return
|
|
}
|
|
|
|
eventIdOrig, ok := c.GetQuery("event_id")
|
|
if !ok {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusClient).
|
|
SetService(exception.EventService).
|
|
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
Build()
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
eventId, err := uuid.Parse(eventIdOrig)
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusClient).
|
|
SetService(exception.EventService).
|
|
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
Build()
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
|
|
attendanceData := new(data.Attendance)
|
|
attendance, err := attendanceData.GetAttendance(userId, eventId)
|
|
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusServer).
|
|
SetService(exception.EventService).
|
|
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorDatabase).
|
|
Build()
|
|
utils.HttpResponse(c, 500, errorCode)
|
|
return
|
|
} else if attendance == nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusClient).
|
|
SetService(exception.EventService).
|
|
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
|
SetType(exception.ErrorTypeSpecific).
|
|
SetOriginal(exception.EventCheckinQueryRecordNotFound).
|
|
Build()
|
|
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}
|
|
|
|
utils.HttpResponse(c, 200, "", "success", checkInAtResp)
|
|
}
|