forked from golang-bristol/beer-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
92 lines (75 loc) · 2.66 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/golang-bristol/beer-model"
"github.com/julienschmidt/httprouter"
)
// GetBeers returns the cellar
func GetBeers(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
cellar := db.FindBeers()
json.NewEncoder(w).Encode(cellar)
}
// GetBeer returns a beer from the cellar
func GetBeer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ID, err := strconv.Atoi(ps.ByName("id"))
if err != nil {
http.Error(w, fmt.Sprintf("%s is not a valid Beer ID, it must be a number.", ps.ByName("id")), http.StatusBadRequest)
return
}
cellar, _ := db.FindBeer(model.Beer{ID: ID})
if len(cellar) == 1 {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(cellar[0])
return
}
http.Error(w, "The beer you requested does not exist.", http.StatusNotFound)
}
// GetBeerReviews returns all reviews for a beer
func GetBeerReviews(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ID, err := strconv.Atoi(ps.ByName("id"))
if err != nil {
http.Error(w, fmt.Sprintf("%s is not a valid Beer ID, it must be a number.", ps.ByName("id")), http.StatusBadRequest)
return
}
// TODO: Consider checking if a beer matching the ID actually exists, and
// 404 if that is not the case.
results, _ := db.FindReview(model.Review{BeerID: ID})
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(results)
}
// AddBeer adds a new beer to the cellar
func AddBeer(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
decoder := json.NewDecoder(r.Body)
var newBeer model.Beer
err := decoder.Decode(&newBeer)
if err != nil {
http.Error(w, "Bad beer - this will be a HTTP status code soon!", http.StatusBadRequest)
return
}
db.SaveBeer(newBeer)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode("New beer added.")
}
// AddBeerReview adds a new review for a beer
func AddBeerReview(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ID, err := strconv.Atoi(ps.ByName("id"))
if err != nil {
http.Error(w, fmt.Sprintf("%s is not a valid Beer ID, it must be a number.", ps.ByName("id")), http.StatusBadRequest)
return
}
var newReview model.Review
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&newReview); err != nil {
http.Error(w, "Failed to parse review", http.StatusBadRequest)
}
newReview.BeerID = ID
if err := db.SaveReview(newReview); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode("New beer review added.")
}