Refactor checkin table to attendance table
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
@@ -1,13 +1,10 @@
|
||||
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) {
|
||||
@@ -28,35 +25,20 @@ func Query(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
checkinData := new(data.Checkin)
|
||||
checkin, err := checkinData.GetCheckin(userId.(uuid.UUID), eventId)
|
||||
attendanceData := new(data.Attendance)
|
||||
attendance, err := attendanceData.GetAttendance(userId.(uuid.UUID), eventId)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"status": "database error"})
|
||||
return
|
||||
} else if checkin == nil {
|
||||
} else if attendance == 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",
|
||||
})
|
||||
} else if attendance.CheckinAt.IsZero() {
|
||||
c.JSON(200, gin.H{"checkin_at": nil})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"checkin_time": checkinTime,
|
||||
"checkin_at": attendance.CheckinAt,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user