-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfruits.go
111 lines (101 loc) · 3.04 KB
/
fruits.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
package main
import (
"encoding/json"
"net/http"
"strconv"
"github.com/gorilla/mux"
errs "github.com/pkg/errors"
)
// FruitController is used to group the controller actions. The DB object is
// used to access the repository layer (aka database)
type FruitController struct {
DB FruitRepository
}
// NewFruitController returns a new fruit controller
func NewFruitController(db *DB) FruitController {
return FruitController{
DB: db,
}
}
// List actions returns a json list of fruits in the database.
func (fc FruitController) List(w http.ResponseWriter, r *http.Request) {
fruitList, err := fc.DB.ListFruits()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(fruitList)
}
// Show action returns a json representation of fruit with 'id' in the database.
func (fc FruitController) Show(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
fruitID, err := strconv.Atoi(vars["id"])
if err != nil {
http.Error(w, "Failed to convert fruit ID from string to int", 500)
return
}
fruit, err := fc.DB.ShowFruit(fruitID)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(fruit)
}
// Create action creates a new fruit object.
func (fc FruitController) Create(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var fruitPayload Fruit
err := decoder.Decode(&fruitPayload)
if err != nil {
http.Error(w, errs.Wrapf(err, "failed to decode payload into fruit object").Error(), 500)
return
}
fruit, err := fc.DB.CreateFruit(fruitPayload)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(fruit)
}
// Update action updates the fruit object represented by the 'id'.
func (fc FruitController) Update(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
fruitID, err := strconv.Atoi(vars["id"])
if err != nil {
http.Error(w, errs.Wrapf(err, "Failed to convert fruit ID from string to int").Error(), 500)
return
}
decoder := json.NewDecoder(r.Body)
var fruitPayload Fruit
err = decoder.Decode(&fruitPayload)
if err != nil {
http.Error(w, errs.Wrapf(err, "Failed to decode payload into fruit object").Error(), 500)
return
}
fruit, err := fc.DB.UpdateFruit(fruitID, fruitPayload)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(fruit)
}
// Delete action soft deleted the fruit object represented by the 'id'.
func (fc FruitController) Delete(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
fruitID, err := strconv.Atoi(vars["id"])
if err != nil {
http.Error(w, errs.Wrapf(err, "Failed to convert fruit ID from string to int").Error(), 500)
return
}
fruit, err := fc.DB.DeleteFruit(fruitID)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(fruit)
}