Last active
August 29, 2015 14:02
-
-
Save aaronroberson/719ec9627bb75f8ac3f8 to your computer and use it in GitHub Desktop.
Geekwise Day 11 Assets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(angular) { | |
"use strict"; | |
var app = angular.module('MyStore'); | |
app.factory('CartService', function($cookieStore, ProductService) { | |
// Private items variable | |
var items = {}; | |
// Angular factories return service objects | |
var cart = { | |
getItems: function() { | |
// Initialize the itemsCookie variable | |
var itemsCookie; | |
// Check if cart is empty | |
if(!items.length) { | |
// Get the items cookie | |
itemsCookie = $cookieStore.get('items'); | |
// Check if the item cookie exists | |
if(itemsCookie) { | |
// Loop through the items in the cookie | |
angular.forEach(itemsCookie, function(item, key) { | |
// Get the product details from the ProductService using the guid | |
ProductService.getProduct(key).then(function(response){ | |
var product = response.data; | |
// Update the quantity to the quantity saved in the cookie | |
product.quantity = item; | |
// Add the product to the cart items object using the guid as the key | |
items[product.guid] = product; | |
}); | |
}); | |
} | |
} | |
// Returns items object | |
return items; | |
}, | |
addItem: function(item) { | |
// Checks if item already exists | |
// If it exists, updates the quantity | |
// If it doesn't exist, adds quantity property with value of 1 then | |
// pushes the item onto the items array | |
if(!items[item.guid]) { | |
item.quantity = 1; | |
items[item.guid] = item; | |
} else { | |
items[item.guid].quantity += 1; | |
//items[item.guid].quantity = items[item.guid].quantity + 1 | |
} | |
cart.updateItemsCookie(); | |
}, | |
removeItem: function(guid) { | |
// Removes an item from the items array | |
// Can use angular.forEach(array, function(item, index) to splice | |
delete items[guid]; | |
// var myArr = new Array(); | |
// delete myArr.splice // returns false | |
// delete myArr.length // return false | |
// var x = 10; | |
// delete x; // return false | |
// y = 10; | |
// delete y; // return true | |
cart.updateItemsCookie(); | |
}, | |
emptyCart: function() { | |
// Sets items array to empty array | |
items = {}; | |
// Remove the items cookie | |
$cookieStore.remove('items'); | |
}, | |
getItemCount: function() { | |
// returns number of items, including item quantity | |
var total = 0; | |
angular.forEach(items, function(item) { | |
total += item.quantity; | |
}); | |
return total; | |
}, | |
getCartSubtotal: function() { | |
// Return the item quantity times item price for each item in the array | |
var total = 0; | |
angular.forEach(items, function(item) { | |
var s = parseInt(item.quantity); | |
var q = isNaN(s) ? 0 : s; | |
var p = cart.getItemPrice(item); | |
total += q * p; | |
}); | |
return total; | |
}, | |
getCartTotal: function() { | |
// TODO Return the cartSubtotal plus shipping and handling | |
// TODO Return the item quantity times item price for each item in the array | |
return cart.getCartSubtotal(); | |
}, | |
updateItemsCookie: function() { | |
// Initialize an object to be saved as the cookie | |
var itemsCookie = {}; | |
// Loop through the items in the cart | |
angular.forEach(items, function(item, key) { | |
// Add each item to the items cookie, | |
// using the guid as the key and the quantity as the value | |
itemsCookie[key] = item.quantity; | |
}); | |
// Use the $cookieStore service to persist the itemsCookie object to the cookie named 'items' | |
$cookieStore.put('items', itemsCookie); | |
}, | |
getItemPrice: function(item) { | |
return parseFloat(item.isSpecial ? item.specialPrice : item.price); | |
} | |
}; | |
return cart; | |
}); | |
})(window.angular); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
emptyCart: function() { | |
// Sets items array to empty array | |
items = {}; | |
// Remove the items cookie | |
$cookieStore.remove('items'); | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getCartTotal: function() { | |
// TODO Return the cartSubtotal plus shipping and handling | |
// TODO Return the item quantity times item price for each item in the array | |
return cart.getCartSubtotal(); | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getItemPrice: function(item) { | |
return parseFloat(item.isSpecial ? item.specialPrice : item.price); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getItems: function() { | |
// Initialize the itemsCookie variable | |
var itemsCookie; | |
// Check if cart is empty | |
if(!items.length) { | |
// Get the items cookie | |
itemsCookie = $cookieStore.get('items'); | |
// Check if the item cookie exists | |
if(itemsCookie) { | |
// Loop through the items in the cookie | |
angular.forEach(itemsCookie, function(item, key) { | |
// Get the product details from the ProductService using the guid | |
ProductService.getProduct(key).then(function(response){ | |
var product = response.data; | |
// Update the quantity to the quantity saved in the cookie | |
product.quantity = item; | |
// Add the product to the cart items object using the guid as the key | |
items[product.guid] = product; | |
}); | |
}); | |
} | |
} | |
// Returns items object | |
return items; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment