package user import ( "errors" "nixcn-cms/data" "time" "github.com/gin-gonic/gin" "github.com/google/uuid" "gorm.io/gorm" ) func Query(c *gin.Context) { userId, ok := c.Get("user_id") if !ok { c.JSON(400, gin.H{"status": "could not found user_id"}) return } eventIdOrig, ok := c.GetQuery("event_id") if !ok { c.JSON(400, gin.H{"status": "could not found event_id"}) return } eventId, err := uuid.Parse(eventIdOrig) if err != nil { c.JSON(400, gin.H{"status": "event_id is not valid"}) return } checkinData := new(data.Checkin) checkin, err := checkinData.GetCheckin(userId.(uuid.UUID), eventId) if err != nil { c.JSON(500, gin.H{"status": "database error"}) return } else if checkin == nil { c.JSON(404, gin.H{"status": "event checkin record not found"}) return } checkinTime := time.Now() checkinData.EventId = eventId checkinData.UserId = userId.(uuid.UUID) checkinData.CheckinAt = checkinTime err = checkinData.CreateCheckin() if err != nil { if errors.Is(err, gorm.ErrDuplicatedKey) { c.JSON(409, gin.H{ "status": "already checked in", }) return } c.JSON(500, gin.H{ "status": "database error", }) return } c.JSON(200, gin.H{ "checkin_time": checkinTime, }) }