diff --git a/api/cart/index.js b/api/cart/index.js index 9abf2a635..a1f3b6341 100644 --- a/api/cart/index.js +++ b/api/cart/index.js @@ -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)); @@ -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; }()); diff --git a/public/basket.html b/public/basket.html index 91521add6..b18572082 100644 --- a/public/basket.html +++ b/public/basket.html @@ -334,26 +334,27 @@

Coupon code

}); } - 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({ @@ -379,7 +380,7 @@

Coupon code

\ ' + data.name + '\ \ - \ + \ \ \ $' + data.price.toFixed(2) + '\ diff --git a/public/js/client.js b/public/js/client.js index c35401434..57676ac5c 100644 --- a/public/js/client.js +++ b/public/js/client.js @@ -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({