-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCaptchaLog.go
78 lines (69 loc) · 1.9 KB
/
CaptchaLog.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
package main
import (
"fmt"
"time"
"github.com/gin-gonic/gin"
)
// CaptchaLog :
type CaptchaLog struct {
Type string `gorm:"column:type;primary_key" form:"type;primary_key" json:"type;primary_key"`
Value string `gorm:"column:value;primary_key" form:"value;primary_key" json:"value;primary_key"`
Count uint16 `gorm:"column:count" form:"count" json:"count"`
UpdatedAt *time.Time `gorm:"column:updated_at" form:"updated_at" json:"updated_at"`
}
// TableName :
func (*CaptchaLog) TableName() string {
return "captcha_log"
}
// handler create
func initRoutersCaptchaLog(r *gin.Engine, captchalog string) {
route := r.Group("captchalog")
route.GET("/", getCaptchaLogs)
route.GET("/:id", getCaptchaLog)
route.POST("/", createCaptchaLog)
route.PUT("/:id", updateCaptchaLog)
route.DELETE("/:id", deleteCaptchaLog)
}
func getCaptchaLogs(c *gin.Context) {
var captchaLogs []CaptchaLog
if err := g.Find(&captchaLogs).Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
} else {
c.JSON(200, captchaLogs)
}
}
func getCaptchaLog(c *gin.Context) {
id := c.Params.ByName("id")
var captchaLog CaptchaLog
if err := g.Where("id = ?", id).First(&captchaLog).Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
} else {
c.JSON(200, captchaLog)
}
}
func createCaptchaLog(c *gin.Context) {
var captchaLog CaptchaLog
c.BindJSON(&captchaLog)
g.Create(&captchaLog)
c.JSON(200, captchaLog)
}
func updateCaptchaLog(c *gin.Context) {
var captchaLog CaptchaLog
id := c.Params.ByName("id")
if err := g.Where("id = ?", id).First(&captchaLog).Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
}
c.BindJSON(&captchaLog)
g.Save(&captchaLog)
c.JSON(200, captchaLog)
}
func deleteCaptchaLog(c *gin.Context) {
id := c.Params.ByName("id")
var captchaLog CaptchaLog
d := g.Where("id = ?", id).Delete(&captchaLog)
fmt.Println(d)
c.JSON(200, gin.H{"id #" + id: "deleted"})
}