2016-09-15 19:53:01 +00:00
|
|
|
library angel_framework.http.memory_service;
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:mirrors';
|
|
|
|
import 'package:json_god/json_god.dart' as god;
|
|
|
|
import 'package:merge_map/merge_map.dart';
|
|
|
|
import '../defs.dart';
|
|
|
|
import 'angel_http_exception.dart';
|
|
|
|
import 'service.dart';
|
2016-04-18 03:27:23 +00:00
|
|
|
|
|
|
|
/// An in-memory [Service].
|
|
|
|
class MemoryService<T> extends Service {
|
2016-06-24 20:56:34 +00:00
|
|
|
Map <int, MemoryModel> items = {};
|
2016-06-24 19:41:41 +00:00
|
|
|
|
|
|
|
MemoryService() :super() {
|
|
|
|
if (!reflectType(T).isAssignableTo(reflectType(MemoryModel))) {
|
|
|
|
throw new Exception(
|
|
|
|
"MemoryServices only support classes that inherit from MemoryModel.");
|
|
|
|
}
|
|
|
|
}
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-06-24 19:41:41 +00:00
|
|
|
_makeJson(int index, MemoryModel t) {
|
|
|
|
return t..id = 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
|
|
|
}
|
|
|
|
|
|
|
|
Future<List> index([Map params]) async {
|
2016-06-24 20:56:34 +00:00
|
|
|
return items.keys
|
|
|
|
.where((index) => items[index] != null)
|
|
|
|
.map((index) => _makeJson(index, items[index]))
|
|
|
|
.toList();
|
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-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());
|
2016-06-24 20:56:34 +00:00
|
|
|
if (items.containsKey(desiredId)) {
|
2016-06-24 19:41:41 +00:00
|
|
|
MemoryModel found = 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
|
|
|
if (found != null) {
|
2016-06-19 05:02:41 +00:00
|
|
|
return _makeJson(desiredId, found);
|
2016-12-31 02:22:54 +00:00
|
|
|
} else throw new AngelHttpException.notFound();
|
|
|
|
} else throw new AngelHttpException.notFound();
|
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-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-24 19:41:41 +00:00
|
|
|
MemoryModel created = (data is MemoryModel) ? data : god.deserializeDatum(
|
|
|
|
data, outputType: T);
|
|
|
|
|
|
|
|
created.id = items.length;
|
2016-06-24 20:56:34 +00:00
|
|
|
items[created.id] = created;
|
2016-06-24 19:41:41 +00:00
|
|
|
return 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());
|
2016-06-24 20:56:34 +00:00
|
|
|
if (items.containsKey(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
|
|
|
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) {
|
2016-12-31 02:22:54 +00:00
|
|
|
throw new AngelHttpException.badRequest(message: 'Invalid 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
|
|
|
}
|
2016-12-31 02:22:54 +00:00
|
|
|
} 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());
|
2016-06-24 20:56:34 +00:00
|
|
|
if (items.containsKey(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
|
|
|
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) {
|
2016-12-31 02:22:54 +00:00
|
|
|
throw new AngelHttpException.badRequest(message: 'Invalid 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
|
|
|
}
|
2016-12-31 02:22:54 +00:00
|
|
|
} 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());
|
2016-06-24 20:56:34 +00:00
|
|
|
if (items.containsKey(desiredId)) {
|
2016-06-24 19:41:41 +00:00
|
|
|
MemoryModel item = items[desiredId];
|
2016-06-24 20:56:34 +00:00
|
|
|
items[desiredId] = null;
|
2016-06-19 05:02:41 +00:00
|
|
|
return _makeJson(desiredId, item);
|
2016-12-31 02:22:54 +00:00
|
|
|
} else throw new AngelHttpException.notFound();
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
|
|
|
}
|