-
Notifications
You must be signed in to change notification settings - Fork 7
/
simple-todo.go
131 lines (114 loc) · 2.95 KB
/
simple-todo.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"github.com/bmizerany/pat"
_ "github.com/mattn/go-sqlite3"
)
type Todo struct {
ID int64 `json:"id"`
Name string `json:"name"`
}
type Todos []Todo
var mainDB *sql.DB
func main() {
db, errOpenDB := sql.Open("sqlite3", "todo.db")
checkErr(errOpenDB)
mainDB = db
r := pat.New()
r.Del("/todos/:id", http.HandlerFunc(deleteByID))
r.Get("/todos/:id", http.HandlerFunc(getByID))
r.Put("/todos/:id", http.HandlerFunc(updateByID))
r.Get("/todos", http.HandlerFunc(getAll))
r.Post("/todos", http.HandlerFunc(insert))
http.Handle("/", r)
log.Print(" Running on 12345")
err := http.ListenAndServe(":12345", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func getAll(w http.ResponseWriter, r *http.Request) {
rows, err := mainDB.Query("SELECT * FROM todos")
checkErr(err)
var todos Todos
for rows.Next() {
var todo Todo
err = rows.Scan(&todo.ID, &todo.Name)
checkErr(err)
todos = append(todos, todo)
}
jsonB, errMarshal := json.Marshal(todos)
checkErr(errMarshal)
fmt.Fprintf(w, "%s", string(jsonB))
}
func getByID(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get(":id")
stmt, err := mainDB.Prepare(" SELECT * FROM todos where id = ?")
checkErr(err)
rows, errQuery := stmt.Query(id)
checkErr(errQuery)
var todo Todo
for rows.Next() {
err = rows.Scan(&todo.ID, &todo.Name)
checkErr(err)
}
jsonB, errMarshal := json.Marshal(todo)
checkErr(errMarshal)
fmt.Fprintf(w, "%s", string(jsonB))
}
func insert(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
var todo Todo
todo.Name = name
stmt, err := mainDB.Prepare("INSERT INTO todos(name) values (?)")
checkErr(err)
result, errExec := stmt.Exec(todo.Name)
checkErr(errExec)
newID, errLast := result.LastInsertId()
checkErr(errLast)
todo.ID = newID
jsonB, errMarshal := json.Marshal(todo)
checkErr(errMarshal)
fmt.Fprintf(w, "%s", string(jsonB))
}
func updateByID(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
id := r.URL.Query().Get(":id")
var todo Todo
ID, _ := strconv.ParseInt(id, 10, 0)
todo.ID = ID
todo.Name = name
stmt, err := mainDB.Prepare("UPDATE todos SET name = ? WHERE id = ?")
checkErr(err)
result, errExec := stmt.Exec(todo.Name, todo.ID)
checkErr(errExec)
rowAffected, errLast := result.RowsAffected()
checkErr(errLast)
if rowAffected > 0 {
jsonB, errMarshal := json.Marshal(todo)
checkErr(errMarshal)
fmt.Fprintf(w, "%s", string(jsonB))
} else {
fmt.Fprintf(w, "{row_affected=%d}", rowAffected)
}
}
func deleteByID(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get(":id")
stmt, err := mainDB.Prepare("DELETE FROM todos WHERE id = ?")
checkErr(err)
result, errExec := stmt.Exec(id)
checkErr(errExec)
rowAffected, errRow := result.RowsAffected()
checkErr(errRow)
fmt.Fprintf(w, "{row_affected=%d}", rowAffected)
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}