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

Commit dea194e

Browse files
authored
Merge pull request #68 from microservices-demo/update-basket
Update basket functionality
2 parents 141b7dc + 0d04fea commit dea194e

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

api/cart/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,32 @@
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+
6490
// Add new item to cart
6591
app.post("/cart", function (req, res, next) {
6692
console.log("Attempting to add to cart: " + JSON.stringify(req.body));

public/basket.html

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,11 @@ <h1>Shopping cart</h1>
9999
shopping</a>
100100
</div>
101101
<div class="pull-right">
102-
<a class="btn btn-default" onclick="alert('Stub. Todo.')"><i
102+
<a class="btn btn-default" onclick="updateCart()"><i
103103
class="fa fa-refresh"></i> Update basket
104104
</a>
105105
<button id="orderButton" type="submit" class="btn btn-primary" disabled>
106-
Proceed to checkout <i
107-
class="fa fa-chevron-right"></i>
106+
Proceed to checkout <i class="fa fa-chevron-right"></i>
108107
</button>
109108
</div>
110109
</div>
@@ -335,6 +334,27 @@ <h4>Coupon code</h4>
335334
});
336335
}
337336

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+
}
357+
338358
$(document).ready(function () {
339359
$.ajaxSetup({
340360
contentType: "application/json; charset=utf-8"
@@ -349,7 +369,8 @@ <h4>Coupon code</h4>
349369
if (element != null && element.itemId != null && element.itemId != "null") {
350370
$.getJSON('/catalogue/' + element.itemId, function (data) {
351371
console.log(JSON.stringify(data));
352-
$('#cart-list').append(' <tr>\
372+
$('#cart-list').append(' <tr class="item">\
373+
<input class="id" type="hidden" value="' + data.id + '">\
353374
<td>\
354375
<a href="#">\
355376
<img src="' + data.imageUrl[0] + '" alt="' + data.namex + '">\
@@ -359,7 +380,7 @@ <h4>Coupon code</h4>
359380
<a href="#">' + data.name + '</a>\
360381
</td>\
361382
<td>\
362-
<input type="number" min="0" value="' + element.quantity + '" class="form-control">\
383+
<input type="number" min="1" value="' + element.quantity + '" class="form-control">\
363384
</td>\
364385
<td>$' + data.price.toFixed(2) + '</td>\
365386
<td>$0.00</td>\

0 commit comments

Comments
 (0)