Skip to content

Commit 7cf29dc

Browse files
author
Rodolfo Martínez
committed
Fixed code
1 parent ea8f56f commit 7cf29dc

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

pkg/routes/middleware.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,39 @@ package routes
33
import (
44
"encoding/json"
55
"io/ioutil"
6+
"log"
67
"net/http"
78
)
89

10+
type Item struct {
11+
Name string `json:"name"`
12+
Quantity int `json:"quantity"`
13+
Price int `json:"price"`
14+
Id float32 `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+
924
func add(w http.ResponseWriter, r *http.Request) {
1025
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
1126

1227
if r.Method == "POST" {
1328
w.WriteHeader(http.StatusOK)
1429
bodyBytes, _ := ioutil.ReadAll(r.Body)
15-
if err := json.NewEncoder(w).Encode(bodyBytes); err != nil {
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 {
1639
panic(err)
1740
}
1841
} else {
@@ -31,3 +54,11 @@ func delete(w http.ResponseWriter, r *http.Request) {
3154
// ..
3255

3356
}
57+
58+
func newTotal(total, newElement int) int {
59+
return total + newElement
60+
}
61+
62+
func productTotalPrice(productPrice, quantity int) int {
63+
return productPrice * quantity
64+
}

web/app/js/app.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ function fetch() {
77

88
}
99

10+
function addID() {
11+
return Math.random();
12+
}
13+
1014
function add(name, price, quantity) {
1115
axios.post('/add', {
12-
id: queryName,
16+
id: addID(),
1317
name: name,
1418
quantity: parseInt(price),
1519
price: parseInt(quantity)
@@ -210,7 +214,7 @@ function paragraphAdd() {
210214
}
211215

212216
function setQuery(id) {
213-
var name = id.split("-");
217+
var name = id.split("-");
214218
queryName = name[0]
215219
}
216220

@@ -222,9 +226,9 @@ function setValueModal(id){
222226
var nameRef = name+"-name"
223227
var quantRef = quantity+"-quantity"
224228
var priceRef = price+"-price"
225-
var nameItem = document.getElementById(nameRef).textContent;
226-
var quantityItem = document.getElementById(quantRef).textContent;
227-
var priceItem = document.getElementById(priceRef).textContent;
229+
var nameItem = document.getElementById(nameRef).textContent;
230+
var quantityItem = document.getElementById(quantRef).textContent;
231+
var priceItem = document.getElementById(priceRef).textContent;
228232
$('#editName').val(nameItem);
229233
$('#editQuantity').val(quantityItem);
230234
$('#editPrice').val(priceItem);

0 commit comments

Comments
 (0)