-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
128 lines (113 loc) · 2.65 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
)
/**
* Todo struct
*/
type Todo struct {
ID uint `json:"id"`
App uint `json:"app"`
Cate uint `json:"cate"`
Done bool `json:"done"`
Content string `json:"content"`
}
var db *gorm.DB
var err error
func main() {
db, _ = gorm.Open("sqlite3", "./todo.db")
defer db.Close()
r := gin.Default()
db.AutoMigrate(&Todo{})
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"POST", "GET", "DELETE"},
AllowHeaders: []string{"Content-Type"},
ExposeHeaders: []string{"Content-Length"},
}))
r.POST("/todo/create", createTodo)
r.POST("/todo/update", updateTodo)
r.GET("/todo/:id", getTodo)
r.GET("/todos", getAllTodos)
r.GET("/todos/:app", getAllTodosByApp)
r.GET("/todos/:app/:cate", getAllTodosByApp)
r.DELETE("/todo/:id", delTodo)
r.Run(":8888")
}
func createTodo(c *gin.Context) {
c.Header("Content-Type", "application/json")
var todo Todo
c.BindJSON(&todo)
db.Create(&todo)
c.JSON(200, todo)
}
func updateTodo(c *gin.Context) {
c.Header("Content-Type", "application/json")
var todo Todo
var todoBind Todo
if c.ShouldBind(&todoBind) == nil {
id := todoBind.ID
if err := db.Where("id = ?", id).First(&todo).Error; err != nil {
c.AbortWithStatus(404)
} else {
todo.Done = todoBind.Done
if todoBind.Content != "" {
todo.Content = todoBind.Content
}
db.Save(&todo)
c.JSON(200, todo)
}
} else {
c.AbortWithStatus(404)
}
}
func getTodo(c *gin.Context) {
c.Header("Content-Type", "application/json")
id := c.Params.ByName("id")
var todo Todo
if err := db.Where("id = ?", id).First(&todo).Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
} else {
c.JSON(200, todo)
}
}
func delTodo(c *gin.Context) {
c.Header("Content-Type", "application/json")
id := c.Params.ByName("id")
var todo Todo
db.Where("id = ?", id).Delete(&todo)
c.JSON(200, gin.H{id: "deleted"})
}
func getAllTodos(c *gin.Context) {
c.Header("Content-Type", "application/json")
var todos []Todo
if err := db.Find(&todos).Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
} else {
c.JSON(200, todos)
}
}
func getAllTodosByApp(c *gin.Context) {
c.Header("Content-Type", "application/json")
app := c.Params.ByName("app")
cate := c.Params.ByName("cate")
var todos []Todo
var tmp *gorm.DB
if cate == "" {
tmp = db.Where("app = ?", app)
} else {
tmp = db.Where("app = ? AND cate = ?", app, cate)
}
if err := tmp.Find(&todos).Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
} else {
c.JSON(200, todos)
}
}