-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (125 loc) · 3.49 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
package main
import (
"database/sql"
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
)
type product struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
func main() {
//connect to database
db, err := sql.Open("postgres", "user=postgres password=postgres dbname=postgres sslmode=disable")
if err != nil {
log.Fatal(err)
}
defer db.Close()
//create the table if it doesn't exist
_, err = db.Exec("CREATE TABLE IF NOT EXISTS products (id SERIAL PRIMARY KEY, name TEXT, email TEXT)")
if err != nil {
log.Fatal(err)
}
//create router
router := mux.NewRouter()
router.HandleFunc("/products", getproducts(db)).Methods("GET")
router.HandleFunc("/products/{id}", getproduct(db)).Methods("GET")
router.HandleFunc("/products", createproduct(db)).Methods("POST")
router.HandleFunc("/products/{id}", updateproduct(db)).Methods("PUT")
router.HandleFunc("/products/{id}", deleteproduct(db)).Methods("DELETE")
//start server
log.Fatal(http.ListenAndServe(":8000", jsonContentTypeMiddleware(router)))
}
func jsonContentTypeMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
next.ServeHTTP(w, r)
})
}
// get all products
func getproducts(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
rows, err := db.Query("SELECT * FROM products")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
products := []product{}
for rows.Next() {
var u product
if err := rows.Scan(&u.ID, &u.Name, &u.Email); err != nil {
log.Fatal(err)
}
products = append(products, u)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
json.NewEncoder(w).Encode(products)
}
}
// get product by id
func getproduct(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
var u product
err := db.QueryRow("SELECT * FROM products WHERE id = $1", id).Scan(&u.ID, &u.Name, &u.Email)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(u)
}
}
// create product
func createproduct(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var u product
json.NewDecoder(r.Body).Decode(&u)
err := db.QueryRow("INSERT INTO products (name, email) VALUES ($1, $2) RETURNING id", u.Name, u.Email).Scan(&u.ID)
if err != nil {
log.Fatal(err)
}
json.NewEncoder(w).Encode(u)
}
}
// update product
func updateproduct(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var u product
json.NewDecoder(r.Body).Decode(&u)
vars := mux.Vars(r)
id := vars["id"]
_, err := db.Exec("UPDATE products SET name = $1, email = $2 WHERE id = $3", u.Name, u.Email, id)
if err != nil {
log.Fatal(err)
}
json.NewEncoder(w).Encode(u)
}
}
// delete product
func deleteproduct(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
var u product
err := db.QueryRow("SELECT * FROM products WHERE id = $1", id).Scan(&u.ID, &u.Name, &u.Email)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
} else {
_, err := db.Exec("DELETE FROM products WHERE id = $1", id)
if err != nil {
//todo : fix error handling
w.WriteHeader(http.StatusNotFound)
return
}
json.NewEncoder(w).Encode("product deleted")
}
}
}