-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
187 lines (166 loc) · 4.05 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"database/sql"
"fmt"
"html/template"
"log"
"net/http"
"github.com/go-sql-driver/mysql"
)
var db *sql.DB
type Task struct {
Id int
Detail string
Pic string
Deadline string
Isdone bool
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func dbConnect() (db *sql.DB) {
cfg := mysql.Config{
User: "simpletasks",
Passwd: "simple123",
Net: "tcp",
Addr: "127.0.0.1:3306",
DBName: "simpletasks",
AllowNativePasswords: true,
}
var err error
db, err = sql.Open("mysql", cfg.FormatDSN())
if err != nil {
log.Fatal(err)
}
pingErr := db.Ping()
if pingErr != nil {
log.Fatal(pingErr)
}
return db
}
var tmpl, err = template.ParseGlob("views/*")
func handlerIndex(w http.ResponseWriter, r *http.Request) {
db := dbConnect()
rows, err := db.Query("select * from tasks")
checkErr(err)
task := Task{}
tasks := []Task{}
for rows.Next() {
var id int
var detail, pic, deadline string
var isdone bool
err = rows.Scan(&id, &detail, &pic, &deadline, &isdone)
checkErr(err)
task.Id = id
task.Detail = detail
task.Pic = pic
task.Deadline = deadline
task.Isdone = isdone
tasks = append(tasks, task)
}
err = tmpl.ExecuteTemplate(w, "index", tasks)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
defer db.Close()
}
func handlerNew(w http.ResponseWriter, r *http.Request) {
err = tmpl.ExecuteTemplate(w, "new", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func handlerInsert(w http.ResponseWriter, r *http.Request) {
db := dbConnect()
if r.Method == "POST" {
detail := r.FormValue("detail")
pic := r.FormValue("pic")
deadline := r.FormValue("deadline")
isdone := r.FormValue("is_done")
insForm, err := db.Prepare("INSERT INTO tasks(detail, pic, deadline, is_done) VALUES(?,?,?,?)")
if err != nil {
panic(err.Error())
}
insForm.Exec(detail, pic, deadline, isdone)
}
defer db.Close()
http.Redirect(w, r, "/", 301)
}
func handlerEdit(w http.ResponseWriter, r *http.Request) {
db := dbConnect()
nId := r.URL.Query().Get("id")
selDB, err := db.Query("SELECT * FROM tasks WHERE id=?", nId)
if err != nil {
panic(err.Error())
}
task := Task{}
for selDB.Next() {
var id int
var detail, pic, deadline string
var isdone bool
err = selDB.Scan(&id, &detail, &pic, &deadline, &isdone)
if err != nil {
panic(err.Error())
}
task.Id = id
task.Detail = detail
task.Pic = pic
task.Deadline = deadline
task.Isdone = isdone
}
tmpl.ExecuteTemplate(w, "edit", task)
defer db.Close()
}
func handlerUpdate(w http.ResponseWriter, r *http.Request) {
db := dbConnect()
if r.Method == "POST" {
detail := r.FormValue("detail")
pic := r.FormValue("pic")
deadline := r.FormValue("deadline")
isdone := r.FormValue("is_done")
id := r.FormValue("id")
fmt.Printf(isdone)
insForm, err := db.Prepare("UPDATE Tasks SET detail=?, pic=?, deadline=?, is_done=? WHERE id=?")
if err != nil {
panic(err.Error())
}
insForm.Exec(detail, pic, deadline, isdone, id)
}
defer db.Close()
http.Redirect(w, r, "/", 301)
}
func handlerDone(w http.ResponseWriter, r *http.Request) {
db := dbConnect()
if r.Method == "POST" {
isdone := 1
id := r.FormValue("id")
insForm, err := db.Prepare("UPDATE Tasks SET is_done=? WHERE id=?")
if err != nil {
panic(err.Error())
}
fmt.Println(isdone, id)
insForm.Exec(isdone, id)
}
defer db.Close()
http.Redirect(w, r, "/", 301)
}
func main() {
http.HandleFunc("/", handlerIndex)
http.HandleFunc("/index", handlerIndex)
http.HandleFunc("/new", handlerNew)
http.HandleFunc("/insert", handlerInsert)
http.HandleFunc("/edit", handlerEdit)
http.HandleFunc("/update", handlerUpdate)
http.HandleFunc("/done", handlerDone)
http.Handle("/static/",
http.StripPrefix("/static/",
http.FileServer(http.Dir("assets"))))
var address = "localhost:8080"
fmt.Printf("server started at %s\n", address)
err := http.ListenAndServe(address, nil)
if err != nil {
fmt.Println(err.Error())
}
}