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

Commit

Permalink
Merge pull request #69 from richardnwinder/update-basket
Browse files Browse the repository at this point in the history
code added: press Update basket button updates item in cart database.
  • Loading branch information
nustiueudinastea authored Mar 7, 2017
2 parents dea194e + 862cfe1 commit 1231715
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 47 deletions.
74 changes: 48 additions & 26 deletions api/cart/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,6 @@
});
});

// Update item
app.put("/cart/:id", function (req, res, next) {
if (req.params.id == null) {
return next(new Error("Must pass id of item to update"), 400);
}

console.log("Updating item: " + req.params.id + " quantity: " + req.body.quantity);

var custId = helpers.getCustomerId(req, app.get("env"));
var options = {
uri: endpoints.cartsUrl + "/" + custId + "/items",
method: 'PATCH',
json: true,
body: {itemId: req.params.id, quantity: req.body.quantity}
};
console.log(options.uri)
request(options, function (error, response, body) {
if (error) {
return next(error);
}
console.log('Item updated with status: ' + response.statusCode);
helpers.respondStatus(res, response.statusCode);
});

});

// Add new item to cart
app.post("/cart", function (req, res, next) {
console.log("Attempting to add to cart: " + JSON.stringify(req.body));
Expand Down Expand Up @@ -132,5 +106,53 @@
});
});

// Update cart item
app.post("/cart/update", function (req, res, next) {
console.log("Attempting to update cart item: " + JSON.stringify(req.body));

if (req.body.id == null) {
next(new Error("Must pass id of item to update"), 400);
return;
}
if (req.body.quantity == null) {
next(new Error("Must pass quantity to update"), 400);
return;
}
var custId = helpers.getCustomerId(req, app.get("env"));

async.waterfall([
function (callback) {
request(endpoints.catalogueUrl + "/catalogue/" + req.body.id.toString(), function (error, response, body) {
console.log(body);
callback(error, JSON.parse(body));
});
},
function (item, callback) {
var options = {
uri: endpoints.cartsUrl + "/" + custId + "/items",
method: 'PATCH',
json: true,
body: {itemId: item.id, quantity: parseInt(req.body.quantity), unitPrice: item.price}
};
console.log("PATCH to carts: " + options.uri + " body: " + JSON.stringify(options.body));
request(options, function (error, response, body) {
if (error) {
callback(error)
return;
}
callback(null, response.statusCode);
});
}
], function (err, statusCode) {
if (err) {
return next(err);
}
if (statusCode != 202) {
return next(new Error("Unable to add to cart. Status code: " + statusCode))
}
helpers.respondStatus(res, statusCode);
});
});

module.exports = app;
}());
43 changes: 22 additions & 21 deletions public/basket.html
Original file line number Diff line number Diff line change
Expand Up @@ -334,26 +334,27 @@ <h4>Coupon code</h4>
});
}

function updateCart() {
console.log('Updating cart.');
$("tr.item").each(function (){
var id = $(this).find("input.id").val(),
quantity = $(this).find("input.form-control").val();
console.log("Item nr " + id + " has " + quantity);
$.ajax({
url: "cart/" + id,
type: "PUT",
data: JSON.stringify({"quantity": quantity}),
success: function (data, textStatus, jqXHR) {
console.log('Worked!');
location.reload();
},
error: function (jqXHR, textStatus, errorThrown) {
console.error('Could not update item: ' + id + ', due to: ' + textStatus + ' | ' + errorThrown);
}
});
});
}
// function updateCart()
// for each item row in cart table call updateToCart (client.js - updateToCart(itemId, quantity, callback))
function updateCart() {
console.log("Updating Cart");
var cartsize = document.getElementById("cart-list").rows.length;
console.log("cart-list size: " + cartsize);

var idx = 0;
next = function(){
if (idx< cartsize) {
var id = document.getElementById("cart-list").rows[idx].cells[2].id;
var quantity = document.getElementById("cart-list").rows[idx].cells[2].getElementsByTagName('input')[0].value;
idx++;
updateToCart(id, quantity, next);
}
else {
location.reload();
}
}
next();
}

$(document).ready(function () {
$.ajaxSetup({
Expand All @@ -379,7 +380,7 @@ <h4>Coupon code</h4>
<td>\
<a href="#">' + data.name + '</a>\
</td>\
<td>\
<td id="' + element.itemId + '">\
<input type="number" min="1" value="' + element.quantity + '" class="form-control">\
</td>\
<td>$' + data.price.toFixed(2) + '</td>\
Expand Down
20 changes: 20 additions & 0 deletions public/js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,26 @@ function addToCart(id) {
});
}

// function update To Cart(itemId, quantity, callback)
// cart/update request sent to frontend server (index.js - app.post("/cart/update" function...)
function updateToCart(id, quantity, next) {

console.log("Sending request to update cart: item: " + id + " quantity: " + quantity);
$.ajax({
url: "cart/update",
type: "POST",
data: JSON.stringify({"id": id, "quantity": quantity}),
success: function (data, textStatus, jqXHR) {
console.log('Item updated: ' + id + ', ' + textStatus);
next();
},
error: function (jqXHR, textStatus, errorThrown) {
console.error('Could not update item: ' + id + ', due to: ' + textStatus + ' | ' + errorThrown);
next();
}
});
}

function username(id, callback) {
console.log("Requesting user account information " + id);
$.ajax({
Expand Down

0 comments on commit 1231715

Please sign in to comment.