Files
nixcn-cms/service/user/checkin.go
2025-12-27 03:45:31 +08:00

78 lines
1.3 KiB
Go

package user
import (
"nixcn-cms/data"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func Checkin(c *gin.Context) {
data := new(data.User)
userId, ok := c.Get("user_id")
if !ok {
c.JSON(401, gin.H{
"status": "unauthorized",
})
return
}
// Get event id from query
eventIdOrig, ok := c.GetQuery("event_id")
if !ok {
c.JSON(400, gin.H{
"status": "undefinded event id",
})
return
}
// Parse event id to uuid
eventId, err := uuid.Parse(eventIdOrig)
if err != nil {
c.JSON(500, gin.H{
"status": "error parsing string to uuid",
})
return
}
data.UserId = userId.(uuid.UUID)
code, err := data.GenCheckinCode(eventId)
if err != nil {
c.JSON(500, gin.H{
"status": "error generating code",
})
return
}
c.JSON(200, gin.H{
"checkin_code": code,
})
}
func CheckinSubmit(c *gin.Context) {
var req struct {
ChekinCode string `json:"checkin_code"`
}
c.ShouldBindJSON(&req)
data := new(data.User)
userId, err := data.VerifyCheckinCode(req.ChekinCode)
if err != nil {
c.JSON(400, gin.H{
"status": "error verify checkin code",
})
return
}
data.GetByUserId(*userId)
if data.PermissionLevel <= 20 {
c.JSON(403, gin.H{
"status": "access denied",
})
}
c.JSON(200, gin.H{
"status": "success",
})
}