This repository has been archived by the owner on Mar 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathservices.dart
321 lines (268 loc) · 10.4 KB
/
services.dart
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import 'dart:async';
import 'dart:io';
import 'dart:convert';
import 'package:html_components/common/reflection.dart' as reflection;
class Player {
String name;
int number;
Player(String this.name, int this.number);
String get imagePath => "${name.toLowerCase()}.jpg";
String toString() => "Player[name=$name, number=$number, imagePath=$imagePath]";
Map<String, Object> toJson() {
return {'name': name, 'number': number, 'imagePath': imagePath};
}
}
List<Player> players = [
new Player('Messi', 10),
new Player('Bojan', 9),
new Player('Iniesta', 8),
new Player('Villa', 7),
new Player('Xavi', 6),
new Player('Puyol', 5),
new Player('Afellay', 20),
new Player('Abidal', 22),
new Player('Alves', 2),
new Player('Pique', 3),
new Player('Keita', 15),
new Player('Adriano', 21),
new Player('Valdes', 1)
];
class Car {
String model;
String manufacturer;
int year;
bool warranty;
Car(String this.model, String this.manufacturer, int this.year, bool this.warranty);
String get imagePath => "${manufacturer}.jpg";
String toString() => model;
Map<String, Object> toJson() {
return {'model': model, 'manufacturer': manufacturer, 'year': year, 'warranty': warranty, 'imagePath': imagePath};
}
}
class TreeNode {
String data;
bool isParent;
TreeNode(this.data, [this.isParent = true]);
Map<String, Object> toJson() => {'data': data, 'isParent': isParent};
}
Map<String, List<String>> stringTreeNodes = {
null: [new TreeNode('Node 0'), new TreeNode('Node 1'), new TreeNode('Node 2', false)],
'Node 0': [new TreeNode('Node 0.0'), new TreeNode('Node 0.1')],
'Node 1': [new TreeNode('Node 1.0'), new TreeNode('Node 1.1', false)],
'Node 2': [],
'Node 0.0': [new TreeNode('Node 0.0.0', false), new TreeNode('Node 0.0.1', false)],
'Node 0.1': [new TreeNode('Node 0.1.0', false)],
'Node 1.0': [new TreeNode('Node 1.0.0', false)],
'Node 1.1': [],
'Node 0.0.0': [],
'Node 0.0.1': [],
'Node 0.1.0': [],
'Node 1.0.0': []
};
List<Car> cars = [
new Car("107f6514", "Honda", 2012, true),
new Car("4c182d41", "Audi", 1965, false),
new Car("b32ff478", "Chrysler", 1990, true),
new Car("9a657f4d", "Ferrari", 2005, false),
new Car("e79e04ac", "Opel", 2002, false),
new Car("c26b4bdd", "Mercedes", 1968, true),
new Car("0f7c6bf8", "Mercedes", 2005, false),
new Car("2e18d596", "Honda", 1988, false),
new Car("55f8ae33", "Opel", 1985, false),
new Car("ead885f7", "Volkswagen", 2005, false),
new Car("b4d60a09", "BMW", 1995, false),
new Car("bf503dea", "Opel", 2006, false),
new Car("09edd664", "Volvo", 1976, false),
new Car("004dfda3", "Chrysler", 1999, false),
new Car("48dccaa5", "Mercedes", 1983, true),
new Car("a2d920ab", "Audi", 1971, true),
new Car("e392f591", "Mercedes", 2005, false),
new Car("9585cac1", "Mercedes", 2008, true),
new Car("13e9e1bf", "Honda", 1990, true),
new Car("ef4e526a", "Ferrari", 1972, false),
new Car("439f21a7", "BMW", 1996, false),
new Car("19924a3b", "Ferrari", 1990, true),
new Car("c371ca0d", "Opel", 1996, false),
new Car("3a5f1d0b", "BMW", 1973, true),
new Car("7b966c83", "Volkswagen", 2008, false),
new Car("76ca3f21", "Opel", 1983, true),
new Car("0865717f", "Honda", 1972, false),
new Car("ae7a37f5", "Ferrari", 1976, false),
new Car("dfe81f5a", "Opel", 1991, true),
new Car("8e001e99", "Volvo", 1976, false),
new Car("edfecf6d", "Opel", 1975, false),
new Car("1968e984", "Honda", 1989, false),
new Car("cae24651", "BMW", 1974, false),
new Car("b9fe01bb", "Renault", 2001, true),
new Car("b45964f6", "Renault", 2005, true),
new Car("039040ee", "Volkswagen", 1976, true),
new Car("395a1c95", "Chrysler", 2003, false),
new Car("71d3e760", "Chrysler", 2003, true),
new Car("ff12cf33", "BMW", 1983, true),
new Car("e6590710", "Ferrari", 2009, true),
new Car("27da1304", "Honda", 1987, true),
new Car("bff9afd9", "Volvo", 1976, true),
new Car("a77b396f", "Mercedes", 1971, false),
new Car("e51d6ab0", "Ferrari", 1961, true),
new Car("dd9dc756", "Volkswagen", 2003, false),
new Car("b46bd077", "Chrysler", 1963, false),
new Car("92aa8974", "Renault", 1991, true),
new Car("ba8d99b6", "Volvo", 2003, true),
new Car("193808e9", "Volvo", 1967, true),
new Car("5ab07802", "Chrysler", 1975, true)
];
class Document {
String name;
String size;
String type;
Document(String this.name, String this.size, String this.type);
Map<String, Object> toJson() => {'name': name, 'size': size, 'type': type};
String toString() => name;
}
class DocumentTreeNode {
Document data;
bool isParent;
DocumentTreeNode(this.data, [this.isParent = true]);
Map<String, Object> toJson() => {'data': data, 'isParent': isParent};
}
Map<String, List<DocumentTreeNode>> documentTreeNodes = {
null: [new DocumentTreeNode(new Document("Documents", "80 KB", "Folder")), new DocumentTreeNode(new Document("Pictures", "171 KB", "Folder")), new DocumentTreeNode(new Document("Movies", "79 GB", "Folder"))],
'Documents': [new DocumentTreeNode(new Document("Work", "40 KB", "Folder")), new DocumentTreeNode(new Document("HTML Components", "3 MB", "Folder"))],
'Pictures': [new DocumentTreeNode(new Document("barcelona.jpg", "30 KB", "JPEG Image"), false), new DocumentTreeNode(new Document("logo.jpg", "45 KB", "JPEG Image"), false), new DocumentTreeNode(new Document("honda.png", "96 KB", "PNG Image"), false)],
'Movies': [new DocumentTreeNode(new Document("Al Pacino", "39 GB", "Folder")), new DocumentTreeNode(new Document("Robert De Niro", "40 GB", "Folder"))],
'Work': [new DocumentTreeNode(new Document("Expenses.doc", "30 KB", "Word Document"), false), new DocumentTreeNode(new Document("Resume.doc", "10 KB", "Word Document"), false)],
'HTML Components': [new DocumentTreeNode(new Document("Documentation.pdf", "3 MB", "PDF Document"), false)],
'barcelona.jpg': [],
'logo.jpg': [],
'honda.png': [],
'Al Pacino': [new DocumentTreeNode(new Document("Scarface", "15 GB", "Movie File"), false), new DocumentTreeNode(new Document("Carlitos' Way", "24 GB", "Movie File"), false)],
'Robert De Niro': [new DocumentTreeNode(new Document("Goodfellas", "23 GB", "Movie File"), false), new DocumentTreeNode(new Document("Untouchables", "17 GB", "Movie File"), false)],
'Expenses.doc': [],
'Resume.doc': [],
'Documentation.pdf': [],
'Scarface': [],
'Carlitos\' Way': [],
'Goodfellas': [],
'Untouchables': []
};
void main() {
HttpServer.bind("localhost", 9090).then((HttpServer server) {
server.listen((HttpRequest request) {
HttpResponse response = request.response;
response.headers
..add('Access-Control-Allow-Origin', '*')
..add('Access-Control-Allow-Methods', 'POST, GET, PUT, OPTIONS')
..add('Access-Control-Max-Age', '1000')
..add('Access-Control-Allow-Headers', '*');
_serve(request).then((_) {
request.response.close();
});
});
});
}
Future _serve(HttpRequest request) {
Completer completer = new Completer();
String url = request.uri.toString();
HttpResponse response = request.response;
if (url.startsWith('/autocomplete/string')) {
_autocompleteString(response, url.substring(url.lastIndexOf('?') + 1));
completer.complete();
}
else if (url.startsWith('/autocomplete/object')) {
_autocompleteObject(response, url.substring(url.lastIndexOf('?') + 1));
completer.complete();
}
else if (url.startsWith('/carousel/cars')) {
_carouselCars(response, url.substring(url.lastIndexOf('/') + 1));
completer.complete();
}
else if (url.startsWith('/datagrid/cars')) {
_datagridCars(response, url.substring(url.lastIndexOf('?') + 1));
completer.complete();
}
else if (url.startsWith('/tree/string')) {
List<int> dataBody = new List<int>();
request.listen(dataBody.addAll, onDone: () {
String data = new String.fromCharCodes(dataBody);
_treeString(response, data);
completer.complete();
});
}
else if (url.startsWith('/datatable/cars')) {
_datagridCars(response, url.substring(url.lastIndexOf('?') +1));
completer.complete();
}
else if (url.startsWith('')) {
List<int> dataBody = new List<int>();
request.listen(dataBody.addAll, onDone: () {
String data = new String.fromCharCodes(dataBody);
_treetableDocument(response, data);
completer.complete();
});
}
else {
response.statusCode = HttpStatus.NOT_FOUND;
completer.complete();
}
return completer.future;
}
void _autocompleteString(HttpResponse response, String queryParams) {
String q = queryParams.split('=')[1];
List<Player> result =
players
.where((Player player) => player.name.toLowerCase().startsWith(q.toLowerCase()))
.map((Player player) => player.name)
.toList(growable: false);
String json = JSON.encode(result);
response.write(json);
}
void _autocompleteObject(HttpResponse response, String queryParams) {
String q = queryParams.split('&')[0].split('=')[1];
String property = queryParams.split('&')[1].split('=')[1];
List<Player> result =
players
.where((Player player) => reflection.getPropertyValue(player, property).toString().toLowerCase().startsWith(q.toLowerCase()))
.toList(growable: false);
String json = JSON.encode(result);
response.write(json);
}
void _carouselCars(HttpResponse response, String param) {
int count = int.parse(param);
List<Car> result =
cars
.take(count)
.toList(growable: false);
String json = JSON.encode(result);
response.write(json);
}
void _datagridCars(HttpResponse response, String queryParams) {
int page = int.parse(queryParams.split('&')[0].split('=')[1]);
int rows = int.parse(queryParams.split('&')[1].split('=')[1]);
List<Car> result =
cars
.skip((page - 1) * rows)
.take(rows)
.toList(growable: false);
Map<String, Object> responseObject = {
'totalCount': cars.length,
'result': result
};
String json = JSON.encode(responseObject);
response.write(json);
}
_treeString(HttpResponse response, String data) {
if (data == 'null') {
response.write(JSON.encode(stringTreeNodes[null]));
return;
}
Map<String, Object> parent = JSON.decode(data);
response.write(JSON.encode(stringTreeNodes[parent['data']]));
}
void _treetableDocument(HttpResponse response, String data) {
if (data == 'null') {
response.write(JSON.encode(documentTreeNodes[null]));
return;
}
String parentName = JSON.decode(data)['data']['name'];
response.write(JSON.encode(documentTreeNodes[parentName]));
}