Skip to content
This repository was archived by the owner on Dec 5, 2023. It is now read-only.

Commit 862cfe1

Browse files
code added: press Update basket button updates item in cart database.
1 parent dea194e commit 862cfe1

File tree

3 files changed

+90
-47
lines changed

3 files changed

+90
-47
lines changed

api/cart/index.js

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,6 @@
6161
});
6262
});
6363

64-
// Update item
65-
app.put("/cart/:id", function (req, res, next) {
66-
if (req.params.id == null) {
67-
return next(new Error("Must pass id of item to update"), 400);
68-
}
69-
70-
console.log("Updating item: " + req.params.id + " quantity: " + req.body.quantity);
71-
72-
var custId = helpers.getCustomerId(req, app.get("env"));
73-
var options = {
74-
uri: endpoints.cartsUrl + "/" + custId + "/items",
75-
method: 'PATCH',
76-
json: true,
77-
body: {itemId: req.params.id, quantity: req.body.quantity}
78-
};
79-
console.log(options.uri)
80-
request(options, function (error, response, body) {
81-
if (error) {
82-
return next(error);
83-
}
84-
console.log('Item updated with status: ' + response.statusCode);
85-
helpers.respondStatus(res, response.statusCode);
86-
});
87-
88-
});
89-
9064
// Add new item to cart
9165
app.post("/cart", function (req, res, next) {
9266
console.log("Attempting to add to cart: " + JSON.stringify(req.body));
@@ -132,5 +106,53 @@
132106
});
133107
});
134108

109+
// Update cart item
110+
app.post("/cart/update", function (req, res, next) {
111+
console.log("Attempting to update cart item: " + JSON.stringify(req.body));
112+
113+
if (req.body.id == null) {
114+
next(new Error("Must pass id of item to update"), 400);
115+
return;
116+
}
117+
if (req.body.quantity == null) {
118+
next(new Error("Must pass quantity to update"), 400);
119+
return;
120+
}
121+
var custId = helpers.getCustomerId(req, app.get("env"));
122+
123+
async.waterfall([
124+
function (callback) {
125+
request(endpoints.catalogueUrl + "/catalogue/" + req.body.id.toString(), function (error, response, body) {
126+
console.log(body);
127+
callback(error, JSON.parse(body));
128+
});
129+
},
130+
function (item, callback) {
131+
var options = {
132+
uri: endpoints.cartsUrl + "/" + custId + "/items",
133+
method: 'PATCH',
134+
json: true,
135+
body: {itemId: item.id, quantity: parseInt(req.body.quantity), unitPrice: item.price}
136+
};
137+
console.log("PATCH to carts: " + options.uri + " body: " + JSON.stringify(options.body));
138+
request(options, function (error, response, body) {
139+
if (error) {
140+
callback(error)
141+
return;
142+
}
143+
callback(null, response.statusCode);
144+
});
145+
}
146+
], function (err, statusCode) {
147+
if (err) {
148+
return next(err);
149+
}
150+
if (statusCode != 202) {
151+
return next(new Error("Unable to add to cart. Status code: " + statusCode))
152+
}
153+
helpers.respondStatus(res, statusCode);
154+
});
155+
});
156+
135157
module.exports = app;
136158
}());

public/basket.html

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -334,26 +334,27 @@ <h4>Coupon code</h4>
334334
});
335335
}
336336

337-
function updateCart() {
338-
console.log('Updating cart.');
339-
$("tr.item").each(function (){
340-
var id = $(this).find("input.id").val(),
341-
quantity = $(this).find("input.form-control").val();
342-
console.log("Item nr " + id + " has " + quantity);
343-
$.ajax({
344-
url: "cart/" + id,
345-
type: "PUT",
346-
data: JSON.stringify({"quantity": quantity}),
347-
success: function (data, textStatus, jqXHR) {
348-
console.log('Worked!');
349-
location.reload();
350-
},
351-
error: function (jqXHR, textStatus, errorThrown) {
352-
console.error('Could not update item: ' + id + ', due to: ' + textStatus + ' | ' + errorThrown);
353-
}
354-
});
355-
});
356-
}
337+
// function updateCart()
338+
// for each item row in cart table call updateToCart (client.js - updateToCart(itemId, quantity, callback))
339+
function updateCart() {
340+
console.log("Updating Cart");
341+
var cartsize = document.getElementById("cart-list").rows.length;
342+
console.log("cart-list size: " + cartsize);
343+
344+
var idx = 0;
345+
next = function(){
346+
if (idx< cartsize) {
347+
var id = document.getElementById("cart-list").rows[idx].cells[2].id;
348+
var quantity = document.getElementById("cart-list").rows[idx].cells[2].getElementsByTagName('input')[0].value;
349+
idx++;
350+
updateToCart(id, quantity, next);
351+
}
352+
else {
353+
location.reload();
354+
}
355+
}
356+
next();
357+
}
357358

358359
$(document).ready(function () {
359360
$.ajaxSetup({
@@ -379,7 +380,7 @@ <h4>Coupon code</h4>
379380
<td>\
380381
<a href="#">' + data.name + '</a>\
381382
</td>\
382-
<td>\
383+
<td id="' + element.itemId + '">\
383384
<input type="number" min="1" value="' + element.quantity + '" class="form-control">\
384385
</td>\
385386
<td>$' + data.price.toFixed(2) + '</td>\

public/js/client.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,26 @@ function addToCart(id) {
146146
});
147147
}
148148

149+
// function update To Cart(itemId, quantity, callback)
150+
// cart/update request sent to frontend server (index.js - app.post("/cart/update" function...)
151+
function updateToCart(id, quantity, next) {
152+
153+
console.log("Sending request to update cart: item: " + id + " quantity: " + quantity);
154+
$.ajax({
155+
url: "cart/update",
156+
type: "POST",
157+
data: JSON.stringify({"id": id, "quantity": quantity}),
158+
success: function (data, textStatus, jqXHR) {
159+
console.log('Item updated: ' + id + ', ' + textStatus);
160+
next();
161+
},
162+
error: function (jqXHR, textStatus, errorThrown) {
163+
console.error('Could not update item: ' + id + ', due to: ' + textStatus + ' | ' + errorThrown);
164+
next();
165+
}
166+
});
167+
}
168+
149169
function username(id, callback) {
150170
console.log("Requesting user account information " + id);
151171
$.ajax({

0 commit comments

Comments
 (0)