@@ -3,39 +3,16 @@ package routes
3
3
import (
4
4
"encoding/json"
5
5
"io/ioutil"
6
- "log"
7
6
"net/http"
8
7
)
9
8
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
-
24
9
func add (w http.ResponseWriter , r * http.Request ) {
25
10
w .Header ().Set ("Content-Type" , "application/json; charset=UTF-8" )
26
11
27
12
if r .Method == "POST" {
28
13
w .WriteHeader (http .StatusOK )
29
14
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 {
39
16
panic (err )
40
17
}
41
18
} else {
@@ -45,73 +22,12 @@ func add(w http.ResponseWriter, r *http.Request) {
45
22
}
46
23
47
24
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
+ // ..
74
27
}
75
28
76
29
func delete (w http.ResponseWriter , r * http.Request ) {
77
- w .Header ().Set ("Content-Type" , "application/json; charset=UTF-8" )
78
30
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
+ // ..
114
32
115
- func productTotalPrice (productPrice , quantity int ) int {
116
- return productPrice * quantity
117
33
}
0 commit comments