-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapi.go
114 lines (96 loc) · 3.15 KB
/
api.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
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"net/http"
"strconv"
)
// curl --user user1:pass1 127.0.0.1:8000/api/products/list
func (a *App) getProducts(w http.ResponseWriter, r *http.Request) {
rows, err := a.DB.Query("SELECT * FROM products")
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
}
defer rows.Close()
var products []Product
for rows.Next() {
var p Product
err := rows.Scan(&p.Id, &p.Name, &p.Manufacturer)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
}
products = append(products, p)
}
_ = json.NewEncoder(w).Encode(products)
}
// curl --header "Content-Type: application/json" --request POST --data '{"name": "ABC", "manufacturer": "ACME"}' \
// --user user1:pass1 127.0.0.1:8000/api/products/new
func (a *App) createProduct(w http.ResponseWriter, r *http.Request) {
var p Product
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&p); err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid payload")
return
}
defer r.Body.Close()
_, err := a.DB.Query("INSERT INTO products (name, manufacturer) VALUES (?, ?)", p.Name, p.Manufacturer)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithMessage(w, http.StatusCreated, "New row added.")
}
// curl --user user1:pass1 127.0.0.1:8000/api/products/10
func (a *App) getProduct(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid product ID")
return
}
p := Product{Id: id}
row := a.DB.QueryRow("SELECT name, manufacturer FROM products WHERE id=?", p.Id)
if err := row.Scan(&p.Name, &p.Manufacturer); err != nil {
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
respondWithJSON(w, http.StatusOK, p)
}
// curl --request PUT --data '{"name": "ABC", "manufacturer": "ACME"}' --user user1:pass1 127.0.0.1:8000/api/products/11
func (a *App) updateProduct(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid product ID")
return
}
var p Product
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&p); err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid payload")
return
}
defer r.Body.Close()
p.Id = id
_, err = a.DB.Query("UPDATE products SET name=?, manufacturer=? WHERE id=?", p.Name, p.Manufacturer, p.Id)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJSON(w, http.StatusOK, p)
}
// curl --request DELETE --user user1:pass1 127.0.0.1:8000/api/products/10
func (a *App) deleteProduct(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid product ID")
return
}
_, err = a.DB.Query("DELETE FROM products WHERE id=?", id)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithMessage(w, http.StatusOK, "Deleted.")
}