Fixed some memory service bugs
This commit is contained in:
parent
1899e586f1
commit
448c8dc99a
3 changed files with 25 additions and 15 deletions
|
@ -1,14 +1,23 @@
|
||||||
part of angel_framework.http;
|
part of angel_framework.http;
|
||||||
|
|
||||||
|
/// Represents data that can be serialized into a MemoryService;
|
||||||
|
class MemoryModel {
|
||||||
|
int id;
|
||||||
|
}
|
||||||
|
|
||||||
/// An in-memory [Service].
|
/// An in-memory [Service].
|
||||||
class MemoryService<T> extends Service {
|
class MemoryService<T> extends Service {
|
||||||
Map <int, T> items = {};
|
Map <int, MemoryModel> items = {};
|
||||||
|
|
||||||
_makeJson(int index, T t) {
|
MemoryService() :super() {
|
||||||
if (T == null || T == Map)
|
if (!reflectType(T).isAssignableTo(reflectType(MemoryModel))) {
|
||||||
return mergeMap([god.serializeObject(t), {'id': index}]);
|
throw new Exception(
|
||||||
else
|
"MemoryServices only support classes that inherit from MemoryModel.");
|
||||||
return t;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_makeJson(int index, MemoryModel t) {
|
||||||
|
return t..id = index;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List> index([Map params]) async {
|
Future<List> index([Map params]) async {
|
||||||
|
@ -21,7 +30,7 @@ class MemoryService<T> extends Service {
|
||||||
Future read(id, [Map params]) async {
|
Future read(id, [Map params]) async {
|
||||||
int desiredId = int.parse(id.toString());
|
int desiredId = int.parse(id.toString());
|
||||||
if (items.containsKey(desiredId)) {
|
if (items.containsKey(desiredId)) {
|
||||||
T found = items[desiredId];
|
MemoryModel found = items[desiredId];
|
||||||
if (found != null) {
|
if (found != null) {
|
||||||
return _makeJson(desiredId, found);
|
return _makeJson(desiredId, found);
|
||||||
} else throw new AngelHttpException.NotFound();
|
} else throw new AngelHttpException.NotFound();
|
||||||
|
@ -30,9 +39,12 @@ class MemoryService<T> extends Service {
|
||||||
|
|
||||||
Future create(data, [Map params]) async {
|
Future create(data, [Map params]) async {
|
||||||
//try {
|
//try {
|
||||||
var created = (data is Map) ? god.deserializeDatum(data, outputType: T) : data;
|
MemoryModel created = (data is MemoryModel) ? data : god.deserializeDatum(
|
||||||
items[items.length] = created;
|
data, outputType: T);
|
||||||
return _makeJson(items.length - 1, created);
|
|
||||||
|
created.id = items.length;
|
||||||
|
items[created.id] = created;
|
||||||
|
return created;
|
||||||
/*} catch (e) {
|
/*} catch (e) {
|
||||||
throw new AngelHttpException.BadRequest(message: 'Invalid data.');
|
throw new AngelHttpException.BadRequest(message: 'Invalid data.');
|
||||||
}*/
|
}*/
|
||||||
|
@ -69,11 +81,9 @@ class MemoryService<T> extends Service {
|
||||||
Future remove(id, [Map params]) async {
|
Future remove(id, [Map params]) async {
|
||||||
int desiredId = int.parse(id.toString());
|
int desiredId = int.parse(id.toString());
|
||||||
if (items.containsKey(desiredId)) {
|
if (items.containsKey(desiredId)) {
|
||||||
T item = items[desiredId];
|
MemoryModel item = items[desiredId];
|
||||||
items[desiredId] = null;
|
items[desiredId] = null;
|
||||||
return _makeJson(desiredId, item);
|
return _makeJson(desiredId, item);
|
||||||
} else throw new AngelHttpException.NotFound();
|
} else throw new AngelHttpException.NotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
MemoryService() : super();
|
|
||||||
}
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
name: angel_framework
|
name: angel_framework
|
||||||
version: 1.0.0-dev+2
|
version: 1.0.0-dev+3
|
||||||
description: Core libraries for the Angel framework.
|
description: Core libraries for the Angel framework.
|
||||||
author: Tobe O <thosakwe@gmail.com>
|
author: Tobe O <thosakwe@gmail.com>
|
||||||
homepage: https://github.com/angel-dart/angel_framework
|
homepage: https://github.com/angel-dart/angel_framework
|
||||||
|
|
|
@ -3,7 +3,7 @@ import 'package:http/http.dart' as http;
|
||||||
import 'package:json_god/json_god.dart' as god;
|
import 'package:json_god/json_god.dart' as god;
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
class Todo {
|
class Todo extends MemoryModel {
|
||||||
String text;
|
String text;
|
||||||
String over;
|
String over;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue