Skip to content

Commit e1dc105

Browse files
committed
Is searched for id in editing and deleting function
1 parent 773a2fc commit e1dc105

File tree

4 files changed

+23
-29
lines changed

4 files changed

+23
-29
lines changed

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func main() {
1313
mux := routes.Init()
1414

1515
srv := http.Server{
16-
Addr: ":8081",
16+
Addr: ":8080",
1717
Handler: mux,
1818
ReadTimeout: 5 * time.Second,
1919
WriteTimeout: 10 * time.Second,

pkg/routes/api.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,6 @@ import (
44
"net/http"
55
)
66

7-
type Item struct {
8-
Name string `json:"name"`
9-
Quantity int `json:"quantity"`
10-
Price int `json:"price"`
11-
}
12-
13-
type EditItem struct {
14-
Name string `json:"name"`
15-
Quantity int `json:"quantity"`
16-
Price int `json:"price"`
17-
Query string `json:"query"`
18-
}
19-
20-
type ItemListPrice struct {
21-
Items []Item `json:"item"`
22-
Total int `json:"total"`
23-
}
24-
25-
var itemsBunch ItemListPrice
26-
277
func Api(mux *http.ServeMux) *http.ServeMux {
288
mux.HandleFunc("/edit", edit)
299
mux.HandleFunc("/add", add)

pkg/routes/middleware.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ import (
77
"net/http"
88
)
99

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+
1024
func add(w http.ResponseWriter, r *http.Request) {
1125
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
1226

@@ -35,22 +49,22 @@ func edit(w http.ResponseWriter, r *http.Request) {
3549
if r.Method == "POST" {
3650
w.WriteHeader(http.StatusOK)
3751
bodyBytes, _ := ioutil.ReadAll(r.Body)
38-
var elementEdit EditItem
52+
var elementEdit Item
3953
err := json.Unmarshal(bodyBytes, &elementEdit)
4054
if err != nil {
4155
log.Println("error:", err)
4256
}
4357
itemsBunch.Total = 0
4458
for index, element := range itemsBunch.Items {
45-
if element.Name == elementEdit.Query {
59+
if element.Name == elementEdit.Id {
4660
itemsBunch.Items[index].Name = elementEdit.Name
4761
itemsBunch.Items[index].Price = elementEdit.Price
4862
itemsBunch.Items[index].Quantity = elementEdit.Quantity
4963
}
5064
totalProduct := productTotalPrice(itemsBunch.Items[index].Price, itemsBunch.Items[index].Quantity)
5165
itemsBunch.Total = newTotal(itemsBunch.Total, totalProduct)
5266
}
53-
elementEdit.Query = ""
67+
elementEdit.Id = ""
5468
if err := json.NewEncoder(w).Encode(itemsBunch); err != nil {
5569
panic(err)
5670
}
@@ -65,14 +79,14 @@ func delete(w http.ResponseWriter, r *http.Request) {
6579
if r.Method == "POST" {
6680
w.WriteHeader(http.StatusOK)
6781
bodyBytes, _ := ioutil.ReadAll(r.Body)
68-
var element EditItem
82+
var element Item
6983
err := json.Unmarshal(bodyBytes, &element)
7084
if err != nil {
7185
log.Println("error:", err)
7286
}
7387
var indexDelete int
7488
for i, item := range itemsBunch.Items {
75-
if item.Name == element.Query {
89+
if item.Name == element.Id {
7690
indexDelete = i
7791
}
7892
}

web/app/js/app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function fetch() {
99

1010
function add(name, price, quantity) {
1111
axios.post('/add', {
12-
query: queryName,
12+
id: queryName,
1313
name: name,
1414
quantity: parseInt(price),
1515
price: parseInt(quantity)
@@ -34,7 +34,7 @@ function edit(name, price, quantity) {
3434
name: name,
3535
quantity: parseInt(price),
3636
price: parseInt(quantity),
37-
query: queryName
37+
id: queryName
3838
})
3939
.then(function (response) {
4040
data = response.data
@@ -53,7 +53,7 @@ function edit(name, price, quantity) {
5353

5454
function deleteItem() {
5555
axios.post('/delete', {
56-
query: queryName
56+
id: queryName
5757
})
5858
.then(function (response) {
5959
data = response.data

0 commit comments

Comments
 (0)