-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.go
118 lines (108 loc) · 3.17 KB
/
product.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
package handler
import (
"encoding/json"
"net/http"
"github.com/LeonardoBatistaCarias/fc2-hexagonal-architecture/adapters/dto"
"github.com/LeonardoBatistaCarias/fc2-hexagonal-architecture/application"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
)
func MakeProductHandlers(r *mux.Router, n *negroni.Negroni, service application.ProductServiceInterface) {
r.Handle("/product/{id}", n.With(
negroni.Wrap(getProduct(service)),
)).Methods("GET", "OPTIONS")
r.Handle("/product", n.With(
negroni.Wrap(createProduct(service)),
)).Methods("POST", "OPTIONS")
r.Handle("/product/{id}/enable", n.With(
negroni.Wrap(enableProduct(service)),
)).Methods("GET", "OPTIONS")
r.Handle("/product/{id}/disable", n.With(
negroni.Wrap(disableProduct(service)),
)).Methods("GET", "OPTIONS")
}
func getProduct(service application.ProductServiceInterface) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r)
id := vars["id"]
product, err := service.Get(id)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
err = json.NewEncoder(w).Encode(product)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
})
}
func createProduct(service application.ProductServiceInterface) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var productDto dto.Product
err := json.NewDecoder(r.Body).Decode(&productDto)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write(jsonError(err.Error()))
return
}
product, err := service.Create(productDto.Name, productDto.Price)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write(jsonError(err.Error()))
return
}
err = json.NewEncoder(w).Encode(product)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write(jsonError(err.Error()))
return
}
})
}
func enableProduct(service application.ProductServiceInterface) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r)
id := vars["id"]
product, err := service.Get(id)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
result, err := service.Enable(product)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(result)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
})
}
func disableProduct(service application.ProductServiceInterface) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r)
id := vars["id"]
product, err := service.Get(id)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
result, err := service.Disable(product)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(result)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
})
}