-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
121 lines (106 loc) · 3.39 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
115
116
117
118
119
120
121
package main
import (
"encoding/json"
"errors"
"github.com/gorilla/mux"
"net/http"
)
type Application struct {
router *mux.Router
db Database
}
func (a *Application) initRoutes() {
a.router.HandleFunc("/live", a.health).Methods("GET")
a.router.HandleFunc("/ready", a.health).Methods("GET")
a.router.HandleFunc("/todo", a.createTodoItem).Methods("POST")
a.router.HandleFunc("/todos", a.getAllToDoItems).Methods("GET")
a.router.HandleFunc("/todo/{id}", a.getToDoItem).Methods("GET")
a.router.HandleFunc("/todo/{id}", a.updateToDoItem).Methods("PUT")
a.router.HandleFunc("/todo/{id}", a.deleteToDoItem).Methods("DELETE")
}
func (a *Application) getToDoItem(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
item, err := a.db.getItem(vars["id"])
var e *ErrorItemNotFound
if errors.As(err, &e) {
respondWithError(w, http.StatusNotFound, "item not found")
return
} else if err != nil {
respondWithError(w, http.StatusInternalServerError, "Database error occurred")
return
}
respondWithJSON(w, http.StatusOK, item)
}
func (a *Application) getAllToDoItems(w http.ResponseWriter, r *http.Request) {
items, err := a.db.allItems()
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Database error occurred")
return
}
respondWithJSON(w, http.StatusOK, items)
}
func (a *Application) deleteToDoItem(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
err := a.db.deleteItem(vars["id"])
var e *ErrorItemNotFound
if errors.As(err, &e) {
respondWithError(w, http.StatusNotFound, "item not found")
return
} else if err != nil {
respondWithError(w, http.StatusInternalServerError, "Database error occurred")
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"result": "success"})
}
func (a *Application) updateToDoItem(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var td Item
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&td); err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request payload")
return
}
defer r.Body.Close()
updatedItem, err := a.db.updateItem(vars["id"], td)
var e *ErrorItemNotFound
if errors.As(err, &e) {
respondWithError(w, http.StatusNotFound, "item not found")
return
} else if err != nil {
respondWithError(w, http.StatusInternalServerError, "Database error occurred")
return
}
respondWithJSON(w, http.StatusOK, updatedItem)
}
func (a *Application) createTodoItem(w http.ResponseWriter, r *http.Request) {
var td Item
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&td); err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid request payload")
return
}
defer r.Body.Close()
td, err := a.db.createItem(td)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Database error occurred")
return
}
respondWithJSON(w, http.StatusCreated, td)
}
func (a *Application) health(w http.ResponseWriter, r *http.Request) {
err := a.db.ping()
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Error occurred.")
}
w.WriteHeader(http.StatusNoContent)
return
}
func respondWithError(w http.ResponseWriter, code int, message string) {
respondWithJSON(w, code, map[string]string{"error": message})
}
func 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)
}