Files
InventoryTraker-Box/InventoryTraker.Web/js/inventory/inventorySvc.js
T
2016-08-22 11:03:00 -04:00

48 lines
998 B
JavaScript

(function() {
window.app.factory('inventorySvc', inventorySvc);
inventorySvc.$inject = ['$http'];
function inventorySvc($http) {
var inventories = [];
loadInventories();
var svc = {
add: add,
update: update,
inventories: inventories,
getInventory: getInventory,
};
return svc;
function loadInventories() {
$http.post('/Inventory/All')
.success(function(data) {
inventories.addRange(data);
});
}
function add(inventory) {
return $http.post('/Inventory/Add', inventory)
.success(function(inventory) {
inventories.unshift(inventory);
});
}
function update(existingInventory, updatedInventory) {
return $http.post('/Inventory/Update', updatedInventory)
.success(function(inventory) {
angular.extend(existingInventory, inventory);
});
}
function getInventory(id) {
for (var i = 0; i < inventories.length; i++) {
if (inventories[i].Id == id) return inventories[i];
}
return null;
}
}
})();