-
Notifications
You must be signed in to change notification settings - Fork 16
/
tiku.go
106 lines (99 loc) · 2.07 KB
/
tiku.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package controller
import (
"github.com/gin-gonic/gin"
"github.com/itihey/tikuAdapter/internal/dao"
"github.com/itihey/tikuAdapter/internal/entity"
"strconv"
)
// Page 分页
type Page struct {
PageNo int `json:"pageNo" form:"pageNo"`
PageSize int `json:"pageSize" form:"pageSize"`
Total int64 `json:"total" form:"total"`
Items []*entity.Tiku `json:"items" form:"items"`
}
// GetQuestions 获取题库
func GetQuestions(c *gin.Context) {
var page Page
err := c.ShouldBindQuery(&page)
if err != nil {
c.JSON(400, gin.H{
"message": "参数错误",
})
return
}
items, total, err := dao.Tiku.Order(dao.Tiku.ID.Desc()).FindByPage(page.PageNo*page.PageSize, page.PageSize)
if err != nil {
c.JSON(500, gin.H{
"message": "服务器错误",
})
return
}
page.Total = total
page.Items = items
c.JSON(200, page)
}
// UpdateQuestions 更新题库
func UpdateQuestions(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(400, gin.H{
"message": "参数错误",
})
return
}
var tiku *entity.Tiku
err = c.ShouldBindJSON(&tiku)
if err != nil {
c.JSON(400, gin.H{
"message": "参数错误",
})
return
}
updates, err := dao.Tiku.Where(dao.Tiku.ID.Eq(int32(id))).Updates(tiku)
if err != nil {
c.JSON(500, gin.H{
"message": "服务器错误",
})
return
}
c.JSON(200, gin.H{
"data": updates,
})
}
// DeleteQuestion 删除题目
func DeleteQuestion(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(400, gin.H{
"message": "参数错误",
})
return
}
dao.Tiku.Where(dao.Tiku.ID.Eq(int32(id))).Delete()
c.JSON(200, gin.H{
"message": "删除成功",
})
}
// CreateQuestion 创建题目
func CreateQuestion(c *gin.Context) {
var tiku *entity.Tiku
err := c.ShouldBindJSON(&tiku)
if err != nil {
c.JSON(400, gin.H{
"message": "参数错误",
})
return
}
tiku.Source = 1
err = dao.Tiku.Create(tiku)
if err != nil {
c.JSON(500, gin.H{
"message": "服务器错误",
})
return
}
c.JSON(200, gin.H{
"message": "创建成功",
})
}