Files
SNCTF/api/admin/challenge.go
2022-07-20 22:29:16 +08:00

164 lines
4.5 KiB
Go

package admin
import (
"fmt"
"github.com/gin-gonic/gin"
"main.go/api"
db "main.go/database"
. "main.go/type"
"strconv"
"strings"
)
// CrDb 专门用作给数据替换。
type CrDb struct {
Name string `json:"name" binding:"required"`
Score int `json:"score" binding:"required"`
Flag string `json:"flag"` // 暂时一个题只能一个flag
Description string `json:"description"`
Attachment string `json:"attachment"`
Category string `json:"category" binding:"required"`
Tags string `json:"tags"`
Hints string `json:"hints"`
Visible int `json:"visible"`
}
//GetAllChallenges 获取所有题目
func GetAllChallenges(c *gin.Context) {
var challenges []Challenge
if err := getchallenge(&challenges); err != nil {
fmt.Println(err)
c.JSON(400, gin.H{"code": 400, "msg": "Get all challenges failure!"})
return
}
c.JSON(200, gin.H{"code": 200, "data": challenges})
}
//getchallenge 获取题目
func getchallenge(challenge *[]Challenge) error {
var attachmentString, hints string
api.Link()
DB := db.DBsnctf
command := "SELECT * FROM challenge;"
rows, err := DB.Raw(command).Rows()
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var b Challenge
err := rows.Scan(&b.ID, &b.Name, &b.Score, &b.Flag, &b.Description, &attachmentString, &b.Category, &b.Tags, &hints, &b.Visible)
if err != nil {
return err
}
b.Attachment = strings.Split(attachmentString, ",")
b.Hints = strings.Split(hints, ",")
*challenge = append(*challenge, b)
}
return nil
}
//NewChallenge 新建一个题目
func NewChallenge(c *gin.Context) {
var request ChallengeRequest
if err := c.ShouldBindJSON(&request); err != nil {
fmt.Println(err)
c.JSON(400, gin.H{"code": 400, "msg": "Request format wrong!"})
return
}
challenge := &Challenge{
Name: request.Name,
Score: request.Score,
Flag: request.Flag,
Description: request.Description,
Attachment: request.Attachment,
Category: request.Category,
Tags: request.Tags,
Hints: request.Hints,
Visible: request.Visible,
}
if err := addChallenge(challenge); err != nil {
c.JSON(400, gin.H{"code": 400, "msg": "Add challenge failure!"})
return
}
c.JSON(200, gin.H{"code": 200, "msg": "Add challenge success!"})
}
//addChallenge 添加题目内容
func addChallenge(c *Challenge) error {
api.Link()
DB := db.DBsnctf
// 使用逗号分隔字符串
attachmentString := strings.Join(c.Attachment, ",")
hintString := strings.Join(c.Hints, ",")
crdb := &CrDb{
Name: c.Name,
Score: c.Score,
Flag: c.Flag,
Description: c.Description,
Attachment: attachmentString,
Category: c.Category,
Tags: c.Tags,
Hints: hintString,
Visible: c.Visible,
}
//插入数据
err := DB.Table("challenge").Create(crdb).Error
//command := "INSERT INTO challenge (name,score,flag,description,attachment,category,tags,hints,visible) VALUES (?,?,?,?,?,?,?,?,?);"
//err := DB.Debug().Raw(command,c.Name, c.Score, c.Flag, c.Description, attachmentString, c.Category, c.Tags, hintString, c.Visible).Error
if err != nil {
return err
}
return nil
}
// DelChallenge 删除题目
func DelChallenge(c *gin.Context) {
api.Link()
DB := db.DBsnctf
id := c.Param("id")
err := DB.Table("challenge").Where("id = ?", id).Delete(&Challenge{}).Error
if err != nil {
c.JSON(400, gin.H{"code": 400, "msg": "Delete challenge failure!"})
return
}
c.JSON(200, gin.H{"code": 200, "msg": "Delete challenge success!"})
}
// EditChallenge 修改题目
func EditChallenge(c *gin.Context) {
api.Link()
DB := db.DBsnctf
id := c.Param("id")
cid, _ := strconv.Atoi(id)
var request ChallengeRequeststrings
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(400, gin.H{"code": 400, "msg": "Request format wrong!"})
return
}
visable, _ := strconv.Atoi(request.Visible)
attachmentString := strings.Join(request.Attachment, ",")
hintString := strings.Join(request.Hints, ",")
challenge := &CrDb{
Name: request.Name,
Score: request.Score,
Flag: request.Flag,
Description: request.Description,
Attachment: attachmentString,
Category: request.Category,
Tags: request.Tags,
Hints: hintString,
Visible: visable,
}
err := DB.Debug().Table("challenge").Where("id = ?", cid).Updates(challenge).Error
if err != nil {
c.JSON(400, gin.H{"code": 400, "msg": "Update challenge failure!"})
return
}
c.JSON(200, gin.H{"code": 200, "msg": "Update challenge success!"})
}