Skip to content

Commit

Permalink
Altered cart to prevent from exceeding the maximum quantity to solve s…
Browse files Browse the repository at this point in the history
  • Loading branch information
amillen committed Oct 27, 2015
1 parent 19e7ea1 commit 1ee67a2
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 23 deletions.
28 changes: 21 additions & 7 deletions dist/ngCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ angular.module('ngCart', ['ngCart.directives'])
};
};

this.addItem = function (id, name, price, quantity, data) {
this.addItem = function (id, name, price, quantity, quantityMax, data) {

var inCart = this.getItemById(id);

if (typeof inCart === 'object'){
//Update quantity of an item if it's already in the cart
inCart.setQuantity(quantity, false);
inCart.setQuantity(quantity, false, quantityMax);
} else {
var newItem = new ngCartItem(id, name, price, quantity, data);
var newItem = new ngCartItem(id, name, price, quantity, quantityMax, data);
this.$cart.items.push(newItem);
$rootScope.$broadcast('ngCart:itemAdded', newItem);
}
Expand Down Expand Up @@ -186,7 +186,7 @@ angular.module('ngCart', ['ngCart.directives'])
_self.$cart.tax = storedCart.tax;

angular.forEach(storedCart.items, function (item) {
_self.$cart.items.push(new ngCartItem(item._id, item._name, item._price, item._quantity, item._data));
_self.$cart.items.push(new ngCartItem(item._id, item._name, item._price, item._quantity, item._maxQuantity, item._data));
});
this.$save();
};
Expand All @@ -199,11 +199,12 @@ angular.module('ngCart', ['ngCart.directives'])

.factory('ngCartItem', ['$rootScope', '$log', function ($rootScope, $log) {

var item = function (id, name, price, quantity, data) {
var item = function (id, name, price, quantity, maxQuantity, data) {
this.setId(id);
this.setName(name);
this.setPrice(price);
this.setQuantity(quantity);
this.setMaxQuantity(maxQuantity);
this.setData(data);
};

Expand Down Expand Up @@ -247,13 +248,15 @@ angular.module('ngCart', ['ngCart.directives'])
};


item.prototype.setQuantity = function(quantity, relative){
item.prototype.setQuantity = function(quantity, relative, quantityMax){


var quantityInt = parseInt(quantity);
if (quantityInt % 1 === 0){
if (relative === true){
this._quantity += quantityInt;
if (quantityMax >= (this._quantity + quantityInt)) {
this._quantity += quantityInt;
}
} else {
this._quantity = quantityInt;
}
Expand All @@ -271,6 +274,16 @@ angular.module('ngCart', ['ngCart.directives'])
return this._quantity;
};

item.prototype.setMaxQuantity = function (maxQuantity) {
if (maxQuantity) this._maxQuantity = maxQuantity;
else {
$log.error('A max quantity must be provided');
}
};

item.prototype.getMaxQuantity = function () {
return this._maxQuantity;
};
item.prototype.setData = function(data){
if (data) this._data = data;
};
Expand All @@ -291,6 +304,7 @@ angular.module('ngCart', ['ngCart.directives'])
name: this.getName(),
price: this.getPrice(),
quantity: this.getQuantity(),
maxQuantity: this.getMaxQuantity(),
data: this.getData(),
total: this.getTotal()
}
Expand Down
2 changes: 1 addition & 1 deletion dist/ngCart.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1ee67a2

Please sign in to comment.