48 lines
997 B
JavaScript
48 lines
997 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;
|
|
}
|
|
}
|
|
})(); |