default
This commit is contained in:
58
api/category.go
Normal file
58
api/category.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
db "main.go/database"
|
||||
|
||||
)
|
||||
|
||||
|
||||
// GetCategories 获取所有题目分类。
|
||||
func GetCategories(c *gin.Context) {
|
||||
var categories []string
|
||||
|
||||
if err := getAllCategories(&categories); err != nil {
|
||||
c.JSON(400, gin.H{"code": 400, "msg": "Get categories failure!"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"code": 200, "data": categories})
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
//getAllCategories 操作数据库所有题目分类
|
||||
func getAllCategories(categories *[]string) error {
|
||||
Link()
|
||||
DB := db.DBsnctf
|
||||
rows,err:= DB.Raw("select category from category").Rows()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var category string
|
||||
if err := rows.Scan(&category); err != nil {
|
||||
return err
|
||||
}
|
||||
*categories = append(*categories, category)
|
||||
}
|
||||
return rows.Err()
|
||||
|
||||
}
|
||||
|
||||
// CheckCategory 检查类别是否正确
|
||||
func CheckCategory(c string) bool {
|
||||
var categories []string
|
||||
if err := getAllCategories(&categories); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, category := range categories {
|
||||
if category == c {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Reference in New Issue
Block a user