2016-04-18 03:27:23 +00:00
|
|
|
part of angel_framework.http;
|
|
|
|
|
|
|
|
/// An in-memory [Service].
|
|
|
|
class MemoryService<T> extends Service {
|
|
|
|
God god = new God();
|
|
|
|
Map <int, T> items = {};
|
|
|
|
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
Map makeJson(int index, T t) {
|
|
|
|
return mergeMap([god.serializeToMap(t), {'id': index}]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List> index([Map params]) async {
|
|
|
|
return items.keys
|
|
|
|
.where((index) => items[index] != null)
|
|
|
|
.map((index) => makeJson(index, items[index]))
|
|
|
|
.toList();
|
|
|
|
}
|
2016-04-18 03:27:23 +00:00
|
|
|
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
Future<Object> read(id, [Map params]) async {
|
|
|
|
int desiredId = int.parse(id.toString());
|
|
|
|
if (items.containsKey(desiredId)) {
|
|
|
|
T found = items[desiredId];
|
|
|
|
if (found != null) {
|
|
|
|
return makeJson(desiredId, found);
|
|
|
|
} else throw new AngelHttpException.NotFound();
|
|
|
|
} else throw new AngelHttpException.NotFound();
|
|
|
|
}
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-05-04 00:58:28 +00:00
|
|
|
Future<Object> create(data, [Map params]) async {
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
try {
|
2016-05-04 00:58:28 +00:00
|
|
|
items[items.length] =
|
|
|
|
(data is Map) ? god.deserializeFromMap(data, T) : data;
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
T created = items[items.length - 1];
|
|
|
|
return makeJson(items.length - 1, created);
|
|
|
|
} catch (e) {
|
|
|
|
throw new AngelHttpException.BadRequest(message: 'Invalid data.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 00:58:28 +00:00
|
|
|
Future<Object> modify(id, data, [Map params]) async {
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
int desiredId = int.parse(id.toString());
|
|
|
|
if (items.containsKey(desiredId)) {
|
|
|
|
try {
|
|
|
|
Map existing = god.serializeToMap(items[desiredId]);
|
|
|
|
data = mergeMap([existing, data]);
|
2016-05-04 00:58:28 +00:00
|
|
|
items[desiredId] =
|
|
|
|
(data is Map) ? god.deserializeFromMap(data, T) : data;
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
return makeJson(desiredId, items[desiredId]);
|
|
|
|
} catch (e) {
|
|
|
|
throw new AngelHttpException.BadRequest(message: 'Invalid data.');
|
|
|
|
}
|
|
|
|
} else throw new AngelHttpException.NotFound();
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 00:58:28 +00:00
|
|
|
Future<Object> update(id, data, [Map params]) async {
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
int desiredId = int.parse(id.toString());
|
|
|
|
if (items.containsKey(desiredId)) {
|
|
|
|
try {
|
2016-05-04 00:58:28 +00:00
|
|
|
items[desiredId] =
|
|
|
|
(data is Map) ? god.deserializeFromMap(data, T) : data;
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
return makeJson(desiredId, items[desiredId]);
|
|
|
|
} catch (e) {
|
|
|
|
throw new AngelHttpException.BadRequest(message: 'Invalid data.');
|
|
|
|
}
|
|
|
|
} else throw new AngelHttpException.NotFound();
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Object> remove(id, [Map params]) async {
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
int desiredId = int.parse(id.toString());
|
|
|
|
if (items.containsKey(desiredId)) {
|
|
|
|
T item = items[desiredId];
|
|
|
|
items[desiredId] = null;
|
|
|
|
return makeJson(desiredId, item);
|
|
|
|
} else throw new AngelHttpException.NotFound();
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MemoryService() : super();
|
|
|
|
}
|