fix bug
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
//GetAllChallenges 获取全部题目
|
//GetAllChallenges 获取全部题目
|
||||||
|
|
||||||
func GetAllChallenges(c * gin.Context){
|
func GetAllChallenges(c *gin.Context) {
|
||||||
var challenges []ChallengeResponse
|
var challenges []ChallengeResponse
|
||||||
|
|
||||||
if err := getAllChallenges(c, &challenges); err != nil {
|
if err := getAllChallenges(c, &challenges); err != nil {
|
||||||
@@ -25,33 +25,75 @@ func GetAllChallenges(c * gin.Context){
|
|||||||
|
|
||||||
//GetChallengesByCategory 获取某个分类下的题目
|
//GetChallengesByCategory 获取某个分类下的题目
|
||||||
|
|
||||||
func GetChallengesByCategory(c *gin.Context){
|
func GetChallengesByCategory(c *gin.Context) {
|
||||||
category := c.Param("category")
|
category := c.Param("category")
|
||||||
if matched := CheckCategory(category); !matched {
|
if matched := CheckCategory(category); !matched {
|
||||||
c.JSON(400, gin.H{"code": 400, "msg": "Wrong category!"})
|
c.JSON(400, gin.H{"code": 400, "msg": "Wrong category!"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var challenges []ChallengeResponse
|
var challenges []ChallengeResponse
|
||||||
if err := getAllChallenges(c, &challenges); err != nil {
|
if err := getCategory(c, &challenges); err != nil {
|
||||||
c.JSON(400, gin.H{"code": 400, "msg": "Get all challenges failure!"})
|
c.JSON(400, gin.H{"code": 400, "msg": "Get all challenges failure!"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(200, gin.H{"code": 200, "data": challenges})
|
c.JSON(200, gin.H{"code": 200, "data": challenges})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//getCategory 操作数据库获取某个分类下的题目
|
||||||
|
func getCategory(c *gin.Context, challenges *[]ChallengeResponse) error {
|
||||||
|
var attachmentString, hints string
|
||||||
|
Link()
|
||||||
|
DB := db.DBsnctf
|
||||||
|
category := c.Param("category")
|
||||||
|
rows, err := DB.Raw("SELECT id, name, score, description, attachment, category, tags, hints FROM challenge WHERE visible=1 AND category=?;", category).Rows()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var challenge ChallengeResponse
|
||||||
|
err = rows.Scan(&challenge.ID, &challenge.Name, &challenge.Score, &challenge.Description, &attachmentString, &challenge.Category, &challenge.Tags, &hints)
|
||||||
|
fmt.Println(err)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// 解析为切片
|
||||||
|
challenge.Attachment = strings.Split(attachmentString, ",")
|
||||||
|
challenge.Hints = strings.Split(hints, ",")
|
||||||
|
|
||||||
|
solverCount, err := getSolverCount(challenge.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
challenge.SolverCount = solverCount
|
||||||
|
session, err := Store.Get(c.Request, "SNCTFSESSID")
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(200, gin.H{"code": 400, "msg": "Get SNCTFSESSID error"})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
user, ok := session.Values["user"].(User)
|
||||||
|
if !ok {
|
||||||
|
c.JSON(200, gin.H{"code": 400, "msg": "No session"})
|
||||||
|
return errors.New("no session")
|
||||||
|
}
|
||||||
|
challenge.IsSolved = hasAlreadySolved(user.ID, challenge.ID)
|
||||||
|
*challenges = append(*challenges, challenge)
|
||||||
|
}
|
||||||
|
return rows.Err()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// getAllChallenges 操作数据库获取所有题目。
|
// getAllChallenges 操作数据库获取所有题目。
|
||||||
func getAllChallenges(c *gin.Context, challenges *[]ChallengeResponse) error {
|
func getAllChallenges(c *gin.Context, challenges *[]ChallengeResponse) error {
|
||||||
var attachmentString, hints string
|
var attachmentString, hints string
|
||||||
Link()
|
Link()
|
||||||
DB := db.DBsnctf
|
DB := db.DBsnctf
|
||||||
rows,err := DB.Raw("SELECT id, name, score, description, attachment, category, tags, hints FROM challenge WHERE visible=1;").Rows()
|
rows, err := DB.Raw("SELECT id, name, score, description, attachment, category, tags, hints FROM challenge WHERE visible=1;").Rows()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
for rows.Next(){
|
for rows.Next() {
|
||||||
var challenge ChallengeResponse
|
var challenge ChallengeResponse
|
||||||
err = rows.Scan(&challenge.ID, &challenge.Name, &challenge.Score, &challenge.Description, &attachmentString, &challenge.Category, &challenge.Tags, &hints)
|
err = rows.Scan(&challenge.ID, &challenge.Name, &challenge.Score, &challenge.Description, &attachmentString, &challenge.Category, &challenge.Tags, &hints)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
Reference in New Issue
Block a user