-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.ts
148 lines (140 loc) · 5.9 KB
/
model.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
export class Product {
constructor(
public id: number,
public title: string,
public price: number,
public rating: number,
public description: string,
public categories: string[]) {}
}
export class Review {
constructor(
public id: number,
public productId: number,
public timestamp: string,
public user: string,
public rating: number,
public comment: string) {}
}
export function getProducts(params = <any>{}): Product[] {
let result = products;
if (params.title) {
result = result.filter(
p => p.title.toLowerCase().indexOf(params.title.toLowerCase()) !== -1);
}
if (parseInt(params.price) && result.length > 0) {
result = result.filter(
p => p.price <= parseInt(params.price));
}
if (params.category && result.length > 0) {
result = result.filter(
p => p.categories.indexOf(params.category.toLowerCase()) !== -1);
}
return result;
}
export function getProductById(productId: number): Product {
return products.find(p => p.id === productId);
}
export function getReviewsByProductId(productId: number): Review[] {
return reviews.filter(r => r.productId === productId);
}
var products = [
{
"id": 0,
"title": 'First Product',
"price": 24.99,
"rating": 4.3,
"description": 'This is a short description. Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
"categories": ['electronics', 'hardware']
},
{
"id": 1,
"title": "Second Product",
"price": 64.99,
"rating": 3.5,
"description": "This is a short description. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"categories": ["books"]
},
{
"id": 2,
"title": "Third Product",
"price": 74.99,
"rating": 4.2,
"description": "This is a short description. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"categories": ["electronics"]
},
{
"id": 3,
"title": "Fourth Product",
"price": 84.99,
"rating": 3.9,
"description": "This is a short description. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"categories": ["hardware"]
},
{
"id": 4,
"title": "Fifth Product",
"price": 94.99,
"rating": 5,
"description": "This is a short description. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"categories": ["electronics", "hardware"]
},
{
"id": 5,
"title": "Sixth Product",
"price": 54.99,
"rating": 4.6,
"description": "This is a short description. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"categories": ["books"]
}
];
var reviews = [
{
"id": 0,
"productId": 0,
"timestamp": "2014-05-20T02:17:00+00:00",
"user": "User 1",
"rating": 5,
"comment": "Aenean vestibulum velit id placerat posuere. Praesent placerat mi ut massa tempor, sed rutrum metus rutrum. Fusce lacinia blandit ligula eu cursus. Proin in lobortis mi. Praesent pellentesque auctor dictum. Nunc volutpat id nibh quis malesuada. Curabitur tincidunt luctus leo, quis condimentum mi aliquet eu. Vivamus eros metus, convallis eget rutrum nec, ultrices quis mauris. Praesent non lectus nec dui venenatis pretium."
},
{
"id": 1,
"productId": 0,
"timestamp": "2014-05-20T02:53:00+00:00",
"user": "User 2",
"rating": 3,
"comment": "Aenean vestibulum velit id placerat posuere. Praesent placerat mi ut massa tempor, sed rutrum metus rutrum. Fusce lacinia blandit ligula eu cursus. Proin in lobortis mi. Praesent pellentesque auctor dictum. Nunc volutpat id nibh quis malesuada. Curabitur tincidunt luctus leo, quis condimentum mi aliquet eu. Vivamus eros metus, convallis eget rutrum nec, ultrices quis mauris. Praesent non lectus nec dui venenatis pretium."
},
{
"id": 2,
"productId": 0,
"timestamp": "2014-05-20T05:26:00+00:00",
"user": "User 3",
"rating": 4,
"comment": "Aenean vestibulum velit id placerat posuere. Praesent placerat mi ut massa tempor, sed rutrum metus rutrum. Fusce lacinia blandit ligula eu cursus. Proin in lobortis mi. Praesent pellentesque auctor dictum. Nunc volutpat id nibh quis malesuada. Curabitur tincidunt luctus leo, quis condimentum mi aliquet eu. Vivamus eros metus, convallis eget rutrum nec, ultrices quis mauris. Praesent non lectus nec dui venenatis pretium."
},
{
"id": 3,
"productId": 0,
"timestamp": "2014-05-20T07:20:00+00:00",
"user": "User 4",
"rating": 4,
"comment": "Aenean vestibulum velit id placerat posuere. Praesent placerat mi ut massa tempor, sed rutrum metus rutrum. Fusce lacinia blandit ligula eu cursus. Proin in lobortis mi. Praesent pellentesque auctor dictum. Nunc volutpat id nibh quis malesuada. Curabitur tincidunt luctus leo, quis condimentum mi aliquet eu. Vivamus eros metus, convallis eget rutrum nec, ultrices quis mauris. Praesent non lectus nec dui venenatis pretium."
},
{
"id": 4,
"productId": 0,
"timestamp": "2014-05-20T11:35:00+00:00",
"user": "User 5",
"rating": 5,
"comment": "Aenean vestibulum velit id placerat posuere. Praesent placerat mi ut massa tempor, sed rutrum metus rutrum. Fusce lacinia blandit ligula eu cursus. Proin in lobortis mi. Praesent pellentesque auctor dictum. Nunc volutpat id nibh quis malesuada. Curabitur tincidunt luctus leo, quis condimentum mi aliquet eu. Vivamus eros metus, convallis eget rutrum nec, ultrices quis mauris. Praesent non lectus nec dui venenatis pretium."
},
{
"id": 5,
"productId": 0,
"timestamp": "2014-05-20T11:42:00+00:00",
"user": "User 6",
"rating": 5,
"comment": "Aenean vestibulum velit id placerat posuere. Praesent placerat mi ut massa tempor, sed rutrum metus rutrum. Fusce lacinia blandit ligula eu cursus. Proin in lobortis mi. Praesent pellentesque auctor dictum. Nunc volutpat id nibh quis malesuada. Curabitur tincidunt luctus leo, quis condimentum mi aliquet eu. Vivamus eros metus, convallis eget rutrum nec, ultrices quis mauris. Praesent non lectus nec dui venenatis pretium."
}
];