-
Notifications
You must be signed in to change notification settings - Fork 104
/
products.service.js
56 lines (49 loc) · 1.71 KB
/
products.service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
angular.module('meanshopApp')
.factory('Product', function () {
var lastId = 5;
var exampleProducts = [
{_id: 1, title: 'Product 1', price: 123.45, quantity: 10, description: 'Lorem ipsum dolor sit amet'},
{_id: 2, title: 'Product 2', price: 123.45, quantity: 10, description: 'Lorem ipsum dolor sit amet'},
{_id: 3, title: 'Product 3', price: 123.45, quantity: 10, description: 'Lorem ipsum dolor sit amet'},
{_id: 4, title: 'Product 4', price: 123.45, quantity: 10, description: 'Lorem ipsum dolor sit amet'},
{_id: 5, title: 'Product 5', price: 123.45, quantity: 10, description: 'Lorem ipsum dolor sit amet'}
];
return {
query: function(){
return exampleProducts;
},
get: function(params){
var result = {};
angular.forEach(exampleProducts, function (product) {
if(product._id == params.id){
this.product = product;
return;
}
}, result);
return result.product;
},
delete: function(params){
angular.forEach(exampleProducts, function (product, index) {
if(product._id == params._id){
console.log(product, index);
exampleProducts.splice(index, 1);
return;
}
});
},
create: function(product){
product._id = ++lastId;
exampleProducts.push(product);
},
update: function(product){
var item = this.get(product);
if(!item) { return false; }
item.title = product.title;
item.price = product.price;
item.quantity = product.quantity;
item.description = product.description;
return true;
}
};
});