-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (147 loc) · 3.57 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"strconv"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
)
type Todo struct {
gorm.Model
Text string
Status string
}
//initDB
func dbInit() {
db, err := gorm.Open("sqlite3", "test.sqlite3")
if err != nil {
panic("dbInit)")
}
db.AutoMigrate(&Todo{})
defer db.Close()
}
//InsertData
func dbInsert(text string, status string) {
db, err := gorm.Open("sqlite3", "test.sqlite3")
if err != nil {
panic("dbInsert)")
}
db.Create(&Todo{Text: text, Status: status})
defer db.Close()
}
//UpdateData
func dbUpdate(id int, text string, status string) {
db, err := gorm.Open("sqlite3", "test.sqlite3")
if err != nil {
panic("(dbUpdate)")
}
var todo Todo
db.First(&todo, id)
todo.Text = text
todo.Status = status
db.Save(&todo)
db.Close()
}
//DeleteDB
func dbDelete(id int) {
db, err := gorm.Open("sqlite3", "test.sqlite3")
if err != nil {
panic("(dbDelete)")
}
var todo Todo
db.First(&todo, id)
db.Delete(&todo)
db.Close()
}
//GetAllData
func dbGetAll() []Todo {
db, err := gorm.Open("sqlite3", "test.sqlite3")
if err != nil {
panic("(dbGetAll())")
}
var todos []Todo
db.Order("created_at desc").Find(&todos)
db.Close()
return todos
}
//GetData
func dbGetOne(id int) Todo {
db, err := gorm.Open("sqlite3", "test.sqlite3")
if err != nil {
panic("(dbGetOne())")
}
var todo Todo
db.First(&todo, id)
db.Close()
return todo
}
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*.html")
dbInit()
router.GET("/", func(ctx *gin.Context){
ctx.HTML(200, "index.html", gin.H{})
})
data := "hello from internal"
router.GET("/data", func(ctx *gin.Context){
ctx.HTML(200, "data.html", gin.H{"data": data})
})
router.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
router.GET("/todos", func(ctx *gin.Context) {
todos := dbGetAll()
ctx.HTML(200, "todos.html", gin.H{
"todos": todos,
})
})
router.POST("/todos/new", func(ctx *gin.Context) {
text := ctx.PostForm("text")
status := ctx.PostForm("status")
dbInsert(text, status)
ctx.Redirect(302, "/todos")
})
router.GET("/todos/detail/:id", func(ctx *gin.Context) {
n := ctx.Param("id")
id, err := strconv.Atoi(n)
if err != nil {
panic(err)
}
todo := dbGetOne(id)
ctx.HTML(200, "detail.html", gin.H{"todo": todo})
})
//Update
router.POST("/todos/update/:id", func(ctx *gin.Context) {
n := ctx.Param("id")
id, err := strconv.Atoi(n)
if err != nil {
panic("ERROR")
}
text := ctx.PostForm("text")
status := ctx.PostForm("status")
dbUpdate(id, text, status)
ctx.Redirect(302, "/todos")
})
//削除確認
router.GET("/todos/delete_check/:id", func(ctx *gin.Context) {
n := ctx.Param("id")
id, err := strconv.Atoi(n)
if err != nil {
panic("ERROR")
}
todo := dbGetOne(id)
ctx.HTML(200, "delete.html", gin.H{"todo": todo})
})
//Delete
router.POST("/todos/delete/:id", func(ctx *gin.Context) {
n := ctx.Param("id")
id, err := strconv.Atoi(n)
if err != nil {
panic("ERROR")
}
dbDelete(id)
ctx.Redirect(302, "/todos")
})
router.Run()
}