More memory service bugs
This commit is contained in:
parent
babb07c814
commit
2459c82bf9
2 changed files with 12 additions and 15 deletions
|
@ -7,7 +7,7 @@ class MemoryModel {
|
||||||
|
|
||||||
/// An in-memory [Service].
|
/// An in-memory [Service].
|
||||||
class MemoryService<T> extends Service {
|
class MemoryService<T> extends Service {
|
||||||
List<MemoryModel> items = [];
|
Map <int, MemoryModel> items = {};
|
||||||
|
|
||||||
MemoryService() :super() {
|
MemoryService() :super() {
|
||||||
if (!reflectType(T).isAssignableTo(reflectType(MemoryModel))) {
|
if (!reflectType(T).isAssignableTo(reflectType(MemoryModel))) {
|
||||||
|
@ -21,18 +21,15 @@ class MemoryService<T> extends Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List> index([Map params]) async {
|
Future<List> index([Map params]) async {
|
||||||
List result = [];
|
return items.keys
|
||||||
|
.where((index) => items[index] != null)
|
||||||
for (int i = 0; i < items.length; i++) {
|
.map((index) => _makeJson(index, items[index]))
|
||||||
result.add(_makeJson(i, items[i]));
|
.toList();
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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.length > desiredId) {
|
if (items.containsKey(desiredId)) {
|
||||||
MemoryModel found = items[desiredId];
|
MemoryModel found = items[desiredId];
|
||||||
if (found != null) {
|
if (found != null) {
|
||||||
return _makeJson(desiredId, found);
|
return _makeJson(desiredId, found);
|
||||||
|
@ -46,7 +43,7 @@ class MemoryService<T> extends Service {
|
||||||
data, outputType: T);
|
data, outputType: T);
|
||||||
|
|
||||||
created.id = items.length;
|
created.id = items.length;
|
||||||
items.add(created);
|
items[created.id] = created;
|
||||||
return created;
|
return created;
|
||||||
/*} catch (e) {
|
/*} catch (e) {
|
||||||
throw new AngelHttpException.BadRequest(message: 'Invalid data.');
|
throw new AngelHttpException.BadRequest(message: 'Invalid data.');
|
||||||
|
@ -55,7 +52,7 @@ class MemoryService<T> extends Service {
|
||||||
|
|
||||||
Future modify(id, data, [Map params]) async {
|
Future modify(id, data, [Map params]) async {
|
||||||
int desiredId = int.parse(id.toString());
|
int desiredId = int.parse(id.toString());
|
||||||
if (items.length > desiredId) {
|
if (items.containsKey(desiredId)) {
|
||||||
try {
|
try {
|
||||||
Map existing = god.serializeObject(items[desiredId]);
|
Map existing = god.serializeObject(items[desiredId]);
|
||||||
data = mergeMap([existing, data]);
|
data = mergeMap([existing, data]);
|
||||||
|
@ -70,7 +67,7 @@ class MemoryService<T> extends Service {
|
||||||
|
|
||||||
Future update(id, data, [Map params]) async {
|
Future update(id, data, [Map params]) async {
|
||||||
int desiredId = int.parse(id.toString());
|
int desiredId = int.parse(id.toString());
|
||||||
if (items.length > desiredId) {
|
if (items.containsKey(desiredId)) {
|
||||||
try {
|
try {
|
||||||
items[desiredId] =
|
items[desiredId] =
|
||||||
(data is Map) ? god.deserializeDatum(data, outputType: T) : data;
|
(data is Map) ? god.deserializeDatum(data, outputType: T) : data;
|
||||||
|
@ -83,9 +80,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.length > desiredId) {
|
if (items.containsKey(desiredId)) {
|
||||||
MemoryModel item = items[desiredId];
|
MemoryModel item = items[desiredId];
|
||||||
items.removeAt(desiredId);
|
items[desiredId] = null;
|
||||||
return _makeJson(desiredId, item);
|
return _makeJson(desiredId, item);
|
||||||
} else throw new AngelHttpException.NotFound();
|
} else throw new AngelHttpException.NotFound();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
name: angel_framework
|
name: angel_framework
|
||||||
version: 1.0.0-dev+3
|
version: 1.0.0-dev+5
|
||||||
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
|
||||||
|
|
Loading…
Reference in a new issue