Refactor mass data structure
Some checks failed
Build Backend (NixCN CMS) TeamCity build failed
Build Frontend (NixCN CMS) TeamCity build finished

Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
2026-01-01 13:29:59 +08:00
parent 8973d518a2
commit acd3c95c80
11 changed files with 518 additions and 279 deletions

View File

@@ -4,6 +4,5 @@ import "github.com/gin-gonic/gin"
func Handler(r *gin.RouterGroup) {
r.POST("/magic", RequestMagicLink)
r.GET("/magic/verify", VerifyMagicLink)
r.POST("/refresh", Refresh)
}

View File

@@ -81,8 +81,8 @@ func VerifyMagicLink(c *gin.Context) {
}
// Verify if user exists
user := new(data.User)
err := user.GetByEmail(email)
userData := new(data.User)
user, err := userData.GetByEmail(email)
if err != nil {
if err == gorm.ErrRecordNotFound {

1
service/auth/redirect.go Normal file
View File

@@ -0,0 +1 @@
package auth

1
service/auth/token.go Normal file
View File

@@ -0,0 +1 @@
package auth

View File

@@ -8,7 +8,7 @@ import (
)
func Checkin(c *gin.Context) {
data := new(data.User)
data := new(data.Checkin)
userId, ok := c.Get("user_id")
if !ok {
c.JSON(401, gin.H{
@@ -50,13 +50,29 @@ func Checkin(c *gin.Context) {
}
func CheckinSubmit(c *gin.Context) {
userId, ok := c.Get("user_id")
if !ok {
c.JSON(403, gin.H{
"status": "unauthorized",
})
}
userData := new(data.User)
userData.GetByUserId(userId.(uuid.UUID))
if userData.PermissionLevel <= 20 {
c.JSON(403, gin.H{
"status": "access denied",
})
return
}
var req struct {
ChekinCode string `json:"checkin_code"`
}
c.ShouldBindJSON(&req)
data := new(data.User)
userId, err := data.VerifyCheckinCode(req.ChekinCode)
checkinData := new(data.Checkin)
userId, err := checkinData.VerifyCheckinCode(req.ChekinCode)
if err != nil {
c.JSON(400, gin.H{
"status": "error verify checkin code",
@@ -64,13 +80,6 @@ func CheckinSubmit(c *gin.Context) {
return
}
data.GetByUserId(*userId)
if data.PermissionLevel <= 20 {
c.JSON(403, gin.H{
"status": "access denied",
})
}
c.JSON(200, gin.H{
"status": "success",
})

View File

@@ -2,14 +2,13 @@ package user
import (
"nixcn-cms/data"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func Info(c *gin.Context) {
data := new(data.User)
userData := new(data.User)
userId, ok := c.Get("user_id")
if !ok {
c.JSON(404, gin.H{
@@ -19,7 +18,7 @@ func Info(c *gin.Context) {
}
// Get user from database
err := data.GetByUserId(userId.(uuid.UUID))
user, err := userData.GetByUserId(userId.(uuid.UUID))
if err != nil {
c.JSON(404, gin.H{
"status": "user not found",
@@ -27,21 +26,13 @@ func Info(c *gin.Context) {
return
}
// Set time nil if time is zero
for k, v := range data.Checkin {
if t, ok := v.(time.Time); ok && t.IsZero() {
data.Checkin[k] = nil
}
}
c.JSON(200, gin.H{
"user_id": data.UserId,
"email": data.Email,
"type": data.Type,
"nickname": data.Nickname,
"subtitle": data.Subtitle,
"avatar": data.Avatar,
"checkin": data.Checkin,
"permission_level": data.PermissionLevel,
"user_id": user.UserId,
"email": user.Email,
"type": user.Type,
"nickname": user.Nickname,
"subtitle": user.Subtitle,
"avatar": user.Avatar,
"permission_level": user.PermissionLevel,
})
}

View File

@@ -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,
})