Skip to content

Commit ea8f56f

Browse files
committed
Simple app for development
1 parent e1dc105 commit ea8f56f

File tree

1 file changed

+4
-88
lines changed

1 file changed

+4
-88
lines changed

pkg/routes/middleware.go

Lines changed: 4 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,16 @@ package routes
33
import (
44
"encoding/json"
55
"io/ioutil"
6-
"log"
76
"net/http"
87
)
98

10-
type Item struct {
11-
Name string `json:"name"`
12-
Quantity int `json:"quantity"`
13-
Price int `json:"price"`
14-
Id string `json:"id"`
15-
}
16-
17-
type ItemListPrice struct {
18-
Items []Item `json:"item"`
19-
Total int `json:"total"`
20-
}
21-
22-
var itemsBunch ItemListPrice
23-
249
func add(w http.ResponseWriter, r *http.Request) {
2510
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
2611

2712
if r.Method == "POST" {
2813
w.WriteHeader(http.StatusOK)
2914
bodyBytes, _ := ioutil.ReadAll(r.Body)
30-
var element Item
31-
err := json.Unmarshal(bodyBytes, &element)
32-
if err != nil {
33-
log.Println("error:", err)
34-
}
35-
itemsBunch.Items = append(itemsBunch.Items, element)
36-
totalProduct := productTotalPrice(element.Price, element.Quantity)
37-
itemsBunch.Total = newTotal(itemsBunch.Total, totalProduct)
38-
if err := json.NewEncoder(w).Encode(itemsBunch); err != nil {
15+
if err := json.NewEncoder(w).Encode(bodyBytes); err != nil {
3916
panic(err)
4017
}
4118
} else {
@@ -45,73 +22,12 @@ func add(w http.ResponseWriter, r *http.Request) {
4522
}
4623

4724
func edit(w http.ResponseWriter, r *http.Request) {
48-
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
49-
if r.Method == "POST" {
50-
w.WriteHeader(http.StatusOK)
51-
bodyBytes, _ := ioutil.ReadAll(r.Body)
52-
var elementEdit Item
53-
err := json.Unmarshal(bodyBytes, &elementEdit)
54-
if err != nil {
55-
log.Println("error:", err)
56-
}
57-
itemsBunch.Total = 0
58-
for index, element := range itemsBunch.Items {
59-
if element.Name == elementEdit.Id {
60-
itemsBunch.Items[index].Name = elementEdit.Name
61-
itemsBunch.Items[index].Price = elementEdit.Price
62-
itemsBunch.Items[index].Quantity = elementEdit.Quantity
63-
}
64-
totalProduct := productTotalPrice(itemsBunch.Items[index].Price, itemsBunch.Items[index].Quantity)
65-
itemsBunch.Total = newTotal(itemsBunch.Total, totalProduct)
66-
}
67-
elementEdit.Id = ""
68-
if err := json.NewEncoder(w).Encode(itemsBunch); err != nil {
69-
panic(err)
70-
}
71-
} else {
72-
w.WriteHeader(http.StatusBadRequest)
73-
}
25+
26+
// ..
7427
}
7528

7629
func delete(w http.ResponseWriter, r *http.Request) {
77-
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
7830

79-
if r.Method == "POST" {
80-
w.WriteHeader(http.StatusOK)
81-
bodyBytes, _ := ioutil.ReadAll(r.Body)
82-
var element Item
83-
err := json.Unmarshal(bodyBytes, &element)
84-
if err != nil {
85-
log.Println("error:", err)
86-
}
87-
var indexDelete int
88-
for i, item := range itemsBunch.Items {
89-
if item.Name == element.Id {
90-
indexDelete = i
91-
}
92-
}
93-
var emptyItem Item
94-
copy(itemsBunch.Items[indexDelete:], itemsBunch.Items[indexDelete+1:])
95-
itemsBunch.Items[len(itemsBunch.Items)-1] = emptyItem
96-
itemsBunch.Items = itemsBunch.Items[:len(itemsBunch.Items)-1]
97-
itemsBunch.Total = 0
98-
for index := range itemsBunch.Items {
99-
totalProduct := productTotalPrice(itemsBunch.Items[index].Price, itemsBunch.Items[index].Quantity)
100-
itemsBunch.Total = newTotal(itemsBunch.Total, totalProduct)
101-
}
102-
if err := json.NewEncoder(w).Encode(itemsBunch); err != nil {
103-
panic(err)
104-
}
105-
} else {
106-
w.WriteHeader(http.StatusBadRequest)
107-
}
108-
109-
}
110-
111-
func newTotal(total, newElement int) int {
112-
return total + newElement
113-
}
31+
// ..
11432

115-
func productTotalPrice(productPrice, quantity int) int {
116-
return productPrice * quantity
11733
}

0 commit comments

Comments
 (0)