新增修改更新api
This commit is contained in:
@@ -6,25 +6,25 @@ import (
|
|||||||
"main.go/api"
|
"main.go/api"
|
||||||
db "main.go/database"
|
db "main.go/database"
|
||||||
. "main.go/type"
|
. "main.go/type"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CrDb 专门用作给数据替换。
|
// CrDb 专门用作给数据替换。
|
||||||
type CrDb struct {
|
type CrDb struct {
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" binding:"required"`
|
||||||
Score int `json:"score" binding:"required"`
|
Score int `json:"score" binding:"required"`
|
||||||
Flag string `json:"flag"` // 暂时一个题只能一个flag
|
Flag string `json:"flag"` // 暂时一个题只能一个flag
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Attachment string `json:"attachment"`
|
Attachment string `json:"attachment"`
|
||||||
Category string `json:"category" binding:"required"`
|
Category string `json:"category" binding:"required"`
|
||||||
Tags string `json:"tags"`
|
Tags string `json:"tags"`
|
||||||
Hints string `json:"hints"`
|
Hints string `json:"hints"`
|
||||||
Visible int `json:"visible"`
|
Visible int `json:"visible"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//NewChallenge 新建一个题目
|
//NewChallenge 新建一个题目
|
||||||
func NewChallenge(c *gin.Context){
|
func NewChallenge(c *gin.Context) {
|
||||||
var request ChallengeRequest
|
var request ChallengeRequest
|
||||||
if err := c.ShouldBindJSON(&request); err != nil {
|
if err := c.ShouldBindJSON(&request); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@@ -58,15 +58,15 @@ func addChallenge(c *Challenge) error {
|
|||||||
attachmentString := strings.Join(c.Attachment, ",")
|
attachmentString := strings.Join(c.Attachment, ",")
|
||||||
hintString := strings.Join(c.Hints, ",")
|
hintString := strings.Join(c.Hints, ",")
|
||||||
crdb := &CrDb{
|
crdb := &CrDb{
|
||||||
Name: c.Name,
|
Name: c.Name,
|
||||||
Score: c.Score,
|
Score: c.Score,
|
||||||
Flag: c.Flag,
|
Flag: c.Flag,
|
||||||
Description: c.Description,
|
Description: c.Description,
|
||||||
Attachment: attachmentString,
|
Attachment: attachmentString,
|
||||||
Category: c.Category,
|
Category: c.Category,
|
||||||
Tags: c.Tags,
|
Tags: c.Tags,
|
||||||
Hints: hintString,
|
Hints: hintString,
|
||||||
Visible: c.Visible,
|
Visible: c.Visible,
|
||||||
}
|
}
|
||||||
//插入数据
|
//插入数据
|
||||||
err := DB.Table("challenge").Create(crdb).Error
|
err := DB.Table("challenge").Create(crdb).Error
|
||||||
@@ -76,4 +76,49 @@ func addChallenge(c *Challenge) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
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 ChallengeRequest
|
||||||
|
if err := c.ShouldBindJSON(&request); err != nil {
|
||||||
|
c.JSON(400, gin.H{"code": 400, "msg": "Request format wrong!"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
challenge := &Challenge{
|
||||||
|
ID: cid,
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
err := DB.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!"})
|
||||||
|
|
||||||
|
}
|
||||||
|
@@ -1,9 +1,11 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
. "main.go/type"
|
|
||||||
db "main.go/database"
|
db "main.go/database"
|
||||||
|
"main.go/tools"
|
||||||
|
. "main.go/type"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetAllNotices 获取所有的公告
|
// GetAllNotices 获取所有的公告
|
||||||
@@ -17,7 +19,6 @@ func GetAllNotices(c *gin.Context) {
|
|||||||
c.JSON(200, gin.H{"code": 200, "data": notices})
|
c.JSON(200, gin.H{"code": 200, "data": notices})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func getAllNotices(notices *[]Notice) error {
|
func getAllNotices(notices *[]Notice) error {
|
||||||
Link()
|
Link()
|
||||||
DB := db.DBsnctf
|
DB := db.DBsnctf
|
||||||
@@ -37,3 +38,41 @@ func getAllNotices(notices *[]Notice) error {
|
|||||||
}
|
}
|
||||||
return rows.Err()
|
return rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddNewNotices 新增公告
|
||||||
|
func AddNewNotices(c *gin.Context) {
|
||||||
|
Link()
|
||||||
|
DB := db.DBsnctf
|
||||||
|
var request NoticeRequest
|
||||||
|
if err := c.ShouldBindJSON(&request); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
c.JSON(400, gin.H{"code": 400, "msg": "Request format wrong!"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
addtime := tools.Timestamp()
|
||||||
|
notice := &Notice{
|
||||||
|
Title: request.Title,
|
||||||
|
Content: request.Content,
|
||||||
|
CreatedAt: addtime,
|
||||||
|
}
|
||||||
|
err := DB.Table("notice").Create(¬ice).Error
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"code": 400, "msg": "error for database"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(200, gin.H{"code": 200, "msg": "success!"})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// DelNotice 删除公告
|
||||||
|
func DelNotice(c *gin.Context) {
|
||||||
|
Link()
|
||||||
|
DB := db.DBsnctf
|
||||||
|
noticeid := c.Param("id")
|
||||||
|
err := DB.Table("notice").Where("id = ?", noticeid).Delete(&Notice{}).Error
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"code": 400, "msg": "error for database"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(200, gin.H{"code": 200, "msg": "success!"})
|
||||||
|
}
|
||||||
|
@@ -56,7 +56,6 @@ func Initrouter() {
|
|||||||
personal.GET("/score/self", api.GetSelfScoreAndRank)
|
personal.GET("/score/self", api.GetSelfScoreAndRank)
|
||||||
// 获取所有用户信息
|
// 获取所有用户信息
|
||||||
personal.GET("/users", api.GetAllUserInfo)
|
personal.GET("/users", api.GetAllUserInfo)
|
||||||
|
|
||||||
// 校内排行api,暂时留空
|
// 校内排行api,暂时留空
|
||||||
}
|
}
|
||||||
// 管理员api,需要用户登陆且Role=1才能访问
|
// 管理员api,需要用户登陆且Role=1才能访问
|
||||||
@@ -65,7 +64,19 @@ func Initrouter() {
|
|||||||
{
|
{
|
||||||
// 创建新题目
|
// 创建新题目
|
||||||
manager.POST("/challenge", admin.NewChallenge)
|
manager.POST("/challenge", admin.NewChallenge)
|
||||||
|
// 删除题目 暂未测试
|
||||||
|
manager.DELETE("/challenge/:id", admin.DelChallenge)
|
||||||
|
// 更新题目信息 暂未测试
|
||||||
|
manager.PUT("/challenge/:id", admin.EditChallenge)
|
||||||
|
//创建新公告
|
||||||
|
manager.POST("/notice", api.AddNewNotices)
|
||||||
|
//删除公告 暂未测试
|
||||||
|
manager.DELETE("/notice/:id", api.DelNotice)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
router.Run(":9000")
|
err := router.Run(":9000")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -28,8 +28,6 @@ func Token() string {
|
|||||||
func Timestamp() int {
|
func Timestamp() int {
|
||||||
// time_zone := time.FixedZone("UTC", 0)
|
// time_zone := time.FixedZone("UTC", 0)
|
||||||
// t := time.Now().In(time_zone).Unix()
|
// t := time.Now().In(time_zone).Unix()
|
||||||
t := time.Now().Unix()
|
t := time.Now().UnixNano() / 1e6
|
||||||
return int(t)
|
return int(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user