2016-04-18 03:27:23 +00:00
|
|
|
part of angel_framework.http;
|
|
|
|
|
|
|
|
/// An in-memory [Service].
|
|
|
|
class MemoryService<T> extends Service {
|
|
|
|
Map <int, T> items = {};
|
|
|
|
|
2016-06-19 05:02:41 +00:00
|
|
|
_makeJson(int index, T t) {
|
2016-05-04 01:01:29 +00:00
|
|
|
if (T == null || T == Map)
|
2016-06-21 22:56:04 +00:00
|
|
|
return mergeMap([god.serializeObject(t), {'id': index}]);
|
2016-05-04 01:01:29 +00:00
|
|
|
else
|
|
|
|
return t;
|
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<List> index([Map params]) async {
|
|
|
|
return items.keys
|
|
|
|
.where((index) => items[index] != null)
|
2016-06-19 05:02:41 +00:00
|
|
|
.map((index) => _makeJson(index, items[index]))
|
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
|
|
|
.toList();
|
|
|
|
}
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-05-04 01:01:29 +00:00
|
|
|
Future read(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 found = items[desiredId];
|
|
|
|
if (found != null) {
|
2016-06-19 05:02:41 +00:00
|
|
|
return _makeJson(desiredId, found);
|
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
|
|
|
} else throw new AngelHttpException.NotFound();
|
|
|
|
} else throw new AngelHttpException.NotFound();
|
|
|
|
}
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-05-04 01:01:29 +00:00
|
|
|
Future create(data, [Map params]) async {
|
2016-06-21 04:19:43 +00:00
|
|
|
//try {
|
2016-06-21 22:56:04 +00:00
|
|
|
var created = (data is Map) ? god.deserializeDatum(data, outputType: T) : data;
|
2016-06-21 04:19:43 +00:00
|
|
|
items[items.length] = created;
|
2016-06-19 05:02:41 +00:00
|
|
|
return _makeJson(items.length - 1, created);
|
2016-06-21 04:19:43 +00:00
|
|
|
/*} catch (e) {
|
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
|
|
|
throw new AngelHttpException.BadRequest(message: 'Invalid data.');
|
2016-06-21 04:19:43 +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
|
|
|
}
|
|
|
|
|
2016-05-04 01:01:29 +00:00
|
|
|
Future 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 {
|
2016-06-21 22:56:04 +00:00
|
|
|
Map existing = god.serializeObject(items[desiredId]);
|
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
|
|
|
data = mergeMap([existing, data]);
|
2016-05-04 00:58:28 +00:00
|
|
|
items[desiredId] =
|
2016-06-21 22:56:04 +00:00
|
|
|
(data is Map) ? god.deserializeDatum(data, outputType: T) : data;
|
2016-06-19 05:02:41 +00:00
|
|
|
return _makeJson(desiredId, items[desiredId]);
|
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
|
|
|
} 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 01:01:29 +00:00
|
|
|
Future 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] =
|
2016-06-21 22:56:04 +00:00
|
|
|
(data is Map) ? god.deserializeDatum(data, outputType: T) : data;
|
2016-06-19 05:02:41 +00:00
|
|
|
return _makeJson(desiredId, items[desiredId]);
|
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
|
|
|
} 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 01:01:29 +00:00
|
|
|
Future 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;
|
2016-06-19 05:02:41 +00:00
|
|
|
return _makeJson(desiredId, item);
|
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
|
|
|
} else throw new AngelHttpException.NotFound();
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MemoryService() : super();
|
|
|
|
}
|