-
Notifications
You must be signed in to change notification settings - Fork 5
/
handlers.go
149 lines (122 loc) · 3.65 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
package main
import (
"net/http"
"fmt"
"github.com/gorilla/mux"
"strconv"
"database/sql"
"encoding/json"
)
// send a payload of JSON content
func (a *App) respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
// send a JSON error message
func (a *App) respondWithError(w http.ResponseWriter, code int, message string) {
a.respondWithJSON(w, code, map[string]string{"error": message})
a.Logger.Printf("App error: code %d, message %s", code, message)
}
// provide health status check
func (a *App) healthStatus(w http.ResponseWriter, r *http.Request) {
dbStatus := "OK"
err := a.DB.Ping()
if err != nil {
dbStatus = fmt.Sprintf("DB access error: %s", err)
}
healthStatus := struct{DbStatus string `json:"dbStatus"`}{dbStatus}
a.respondWithJSON(w, http.StatusOK, healthStatus)
}
// handle get product request
func (a *App) getProduct(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
msg := fmt.Sprintf("Invalid product ID. Error: %s", err.Error())
a.respondWithError(w, http.StatusBadRequest, msg)
return
}
p := product{ID: id}
if err := p.getProduct(a.DB); err != nil {
switch err {
case sql.ErrNoRows:
msg := fmt.Sprintf("Product not found. Error: %s", err.Error())
a.respondWithError(w, http.StatusNotFound, msg)
default:
a.respondWithError(w, http.StatusInternalServerError, err.Error())
}
return
}
a.respondWithJSON(w, http.StatusOK, p)
}
func (a *App) getProducts(w http.ResponseWriter, r *http.Request) {
count, _ := strconv.Atoi(r.FormValue("count"))
start, _ := strconv.Atoi(r.FormValue("start"))
if count > 10 || count < 1 {
count = 10
}
if start < 0 {
start = 0
}
products, err := getProducts(a.DB, start, count)
if err != nil {
a.respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
a.respondWithJSON(w, http.StatusOK, products)
}
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 {
msg := fmt.Sprintf("Invalid request payload. Error: %s", err.Error())
a.respondWithError(w, http.StatusBadRequest, msg)
return
}
defer r.Body.Close()
if err := p.createProduct(a.DB); err != nil {
a.respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
a.respondWithJSON(w, http.StatusCreated, p)
}
func (a *App) updateProduct(w http.ResponseWriter, r *http.Request) {
var p product
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
msg := fmt.Sprintf("Invalid product ID. Error: %s", err.Error())
a.respondWithError(w, http.StatusBadRequest, msg)
return
}
p.ID = id
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&p); err != nil {
msg := fmt.Sprintf("Invalid request payload. Error: %s", err.Error())
a.respondWithError(w, http.StatusBadRequest, msg)
return
}
defer r.Body.Close()
if err := p.updateProduct(a.DB); err != nil {
a.respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
a.respondWithJSON(w, http.StatusOK, p)
}
func (a *App) deleteProduct(w http.ResponseWriter, r *http.Request) {
var p product
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
msg := fmt.Sprintf("Invalid product ID. Error: %s", err.Error())
a.respondWithError(w, http.StatusBadRequest, msg)
return
}
p.ID = id
if err := p.deleteProduct(a.DB); err != nil {
a.respondWithError(w, http.StatusInternalServerError, err.Error())
}
a.respondWithJSON(w, http.StatusOK, p)
}