@@ -1,11 +1,13 @@
|
||||
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) {
|
||||
@@ -14,29 +16,46 @@ func Query(c *gin.Context) {
|
||||
c.JSON(400, gin.H{"status": "could not found user_id"})
|
||||
return
|
||||
}
|
||||
eventId, ok := c.GetQuery("event_id")
|
||||
|
||||
eventIdOrig, ok := c.GetQuery("event_id")
|
||||
if !ok {
|
||||
c.JSON(400, gin.H{"status": "could not found event_id"})
|
||||
return
|
||||
}
|
||||
|
||||
data := new(data.User)
|
||||
err := data.GetByUserId(userId.(uuid.UUID))
|
||||
eventId, err := uuid.Parse(eventIdOrig)
|
||||
if err != nil {
|
||||
c.JSON(404, gin.H{"status": "cannot found user"})
|
||||
return
|
||||
}
|
||||
if data.Checkin[eventId] == nil {
|
||||
c.JSON(404, gin.H{"status": "cannot found user checked in"})
|
||||
c.JSON(400, gin.H{"status": "event_id is not valid"})
|
||||
return
|
||||
}
|
||||
|
||||
var checkinTime *time.Time
|
||||
if data.Checkin[eventId].(*time.Time).IsZero() {
|
||||
checkinTime = nil
|
||||
} else {
|
||||
checkinTime = data.Checkin[eventId].(*time.Time)
|
||||
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,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user