-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
142 lines (117 loc) · 4.47 KB
/
app.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"lana/flagship-store/services"
"lana/flagship-store/services/commands"
"lana/flagship-store/services/errors"
"lana/flagship-store/services/responses"
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
type App struct {
Router *mux.Router
CreateCheckoutService services.CreateCheckout
AddProductToCheckoutService services.AddProductToCheckout
RetrieveCheckoutAmountService services.RetrieveCheckoutAmount
DeleteCheckoutService services.DeleteCheckout
}
func (app *App) Initialize(createCheckoutService services.CreateCheckout, addProductToCheckoutService services.AddProductToCheckout, deleteCheckoutService services.DeleteCheckout, retrieveCheckoutAmountService services.RetrieveCheckoutAmount) {
app.CreateCheckoutService = createCheckoutService
app.AddProductToCheckoutService = addProductToCheckoutService
app.RetrieveCheckoutAmountService = retrieveCheckoutAmountService
app.DeleteCheckoutService = deleteCheckoutService
app.Router = mux.NewRouter().StrictSlash(true)
app.initializeRoutes()
}
func (app *App) Run(addr string) {
fmt.Println("My first Golang application")
log.Fatal(http.ListenAndServe(addr, app.Router))
}
func (app *App) initializeRoutes() {
app.Router.HandleFunc("/checkouts", app.createCheckout).Methods("POST")
app.Router.HandleFunc("/checkouts/{id}", app.addProductToCheckout).Methods("PATCH")
app.Router.HandleFunc("/checkouts/{id}", app.deleteCheckout).Methods("DELETE")
app.Router.HandleFunc("/checkouts/{id}/amount", app.retrieveCheckoutAmount).Methods("GET")
}
func (app *App) createCheckout(response http.ResponseWriter, request *http.Request) {
body, _ := ioutil.ReadAll(request.Body)
var productCommand commands.Product
json.Unmarshal(body, &productCommand)
checkout, err := app.CreateCheckoutService.Do(productCommand)
if _, ok := err.(*errors.ProductNotFoundError); ok {
response.WriteHeader(http.StatusNotFound)
productNotFound := responses.ProductNotFound{
Message: "Product " + productCommand.Code + " not found",
}
json.NewEncoder(response).Encode(productNotFound)
return
}
response.WriteHeader(http.StatusCreated)
json.NewEncoder(response).Encode(checkout)
}
func (app *App) addProductToCheckout(response http.ResponseWriter, request *http.Request) {
body, _ := ioutil.ReadAll(request.Body)
var addProductCommand commands.AddProduct
json.Unmarshal(body, &addProductCommand)
vars := mux.Vars(request)
id := vars["id"]
_, err := app.AddProductToCheckoutService.Do(addProductCommand, id)
if _, isThisError := err.(*errors.CheckoutNotFoundError); isThisError {
response.WriteHeader(http.StatusNotFound)
checkoutNotFound := responses.CheckoutNotFound{
Message: "Checkout " + id + " not found",
}
json.NewEncoder(response).Encode(checkoutNotFound)
return
}
if _, isThisError := err.(*errors.ProductNotFoundError); isThisError {
response.WriteHeader(http.StatusUnprocessableEntity)
productNotFound := responses.ProductNotFound{
Message: "Product " + addProductCommand.Code + " not found",
}
json.NewEncoder(response).Encode(productNotFound)
return
}
response.WriteHeader(http.StatusNoContent)
}
func (app *App) retrieveCheckoutAmount(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
id := vars["id"]
amount, err := app.RetrieveCheckoutAmountService.Do(id)
if _, ok := err.(*errors.CheckoutNotFoundError); ok {
response.WriteHeader(http.StatusNotFound)
checkoutNotFound := responses.CheckoutNotFound{
Message: "Checkout " + id + " not found",
}
json.NewEncoder(response).Encode(checkoutNotFound)
return
}
responseCheckout := responses.Checkout{
Amount: formatCheckoutAmount(amount),
}
response.WriteHeader(http.StatusOK)
json.NewEncoder(response).Encode(responseCheckout)
}
func formatCheckoutAmount(amount int) string {
amount_with_decimals := float64(amount) / 100
amount_with_fixed_decimals := strconv.FormatFloat(amount_with_decimals, 'f', 2, 64)
return amount_with_fixed_decimals + "€"
}
func (app *App) deleteCheckout(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
id := vars["id"]
_, err := app.DeleteCheckoutService.Do(id)
if _, ok := err.(*errors.CheckoutNotFoundError); ok {
response.WriteHeader(http.StatusNotFound)
checkoutNotFound := responses.CheckoutNotFound{
Message: "Checkout " + id + " not found",
}
json.NewEncoder(response).Encode(checkoutNotFound)
return
}
response.WriteHeader(http.StatusNoContent)
}