/
handlers.go
153 lines (126 loc) · 3.06 KB
/
handlers.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
_ "github.com/lib/pq"
"github.com/russross/blackfriday"
)
var db *sql.DB
func init() {
var err error
db, err = sql.Open("postgres", "postgres://JavierC@localhost/todoapp?sslmode=disable")
if err != nil {
log.Fatal(err)
}
if err = db.Ping(); err != nil {
log.Fatal(err)
}
}
func Index(w http.ResponseWriter, r *http.Request) {
input, err := ioutil.ReadFile("README.md")
if err != nil {
log.Printf("Error Reading File")
panic(err)
}
output := blackfriday.MarkdownCommon(input)
w.Write(output)
}
func TodoIndex(w http.ResponseWriter, r *http.Request) {
rows, err := db.Query("SELECT * FROM todos")
if err != nil {
log.Printf("Error opening DB")
http.Error(w, http.StatusText(500), 500)
return
}
defer rows.Close()
todos := make([]*Todo, 0)
for rows.Next() {
todo := new(Todo)
err := rows.Scan(&todo.Id, &todo.Name, &todo.Completed)
if err != nil {
log.Printf("Error scanning DB")
http.Error(w, http.StatusText(500), 500)
return
}
todos = append(todos, todo)
}
if err = rows.Err(); err != nil {
log.Printf("Error closing DB")
http.Error(w, http.StatusText(500), 500)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(todos); err != nil {
log.Printf("Error printing todos")
panic(err)
}
}
func TodoCreate(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, http.StatusText(405), 405)
return
}
name := r.FormValue("name")
completedString := r.FormValue("completed")
if name == "" || completedString == "" {
http.Error(w, http.StatusText(400), 400)
return
}
var completed bool
if completedString == "true" {
completed = true
} else {
completed = false
}
id, err := strconv.ParseInt(r.FormValue("id"), 0, 32)
if err != nil {
http.Error(w, http.StatusText(400), 400)
return
}
result, err := db.Exec("INSERT INTO todos VALUES($1, $2, $3)", id, name, completed)
if err != nil {
http.Error(w, http.StatusText(500), 500)
return
}
rowsAffected, err := result.RowsAffected()
if err != nil {
http.Error(w, http.StatusText(500), 500)
return
}
fmt.Fprintf(w, "Todo %d created successfully (%d row affected)\n", id, rowsAffected)
}
func TodoShow(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
log.Printf("NOT GET")
http.Error(w, http.StatusText(405), 405)
return
}
id := r.FormValue("id")
if id == "" {
log.Printf("Bad ID")
http.Error(w, http.StatusText(400), 400)
return
}
row := db.QueryRow("SELECT * FROM todos WHERE Id = $1", id)
todo := new(Todo)
err := row.Scan(&todo.Id, &todo.Name, &todo.Completed)
if err == sql.ErrNoRows {
http.NotFound(w, r)
return
} else if err != nil {
http.Error(w, http.StatusText(500), 500)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(todo); err != nil {
log.Printf("Error printing todos")
panic(err)
}
}