From babb07c8149e565f2c54841271ff5afbf0377952 Mon Sep 17 00:00:00 2001 From: regiostech Date: Fri, 24 Jun 2016 16:38:29 -0400 Subject: [PATCH] Fixed some more memory service bugs --- lib/src/http/services/memory.dart | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/src/http/services/memory.dart b/lib/src/http/services/memory.dart index 06a777f4..29a4adf1 100644 --- a/lib/src/http/services/memory.dart +++ b/lib/src/http/services/memory.dart @@ -7,7 +7,7 @@ class MemoryModel { /// An in-memory [Service]. class MemoryService extends Service { - Map items = {}; + List items = []; MemoryService() :super() { if (!reflectType(T).isAssignableTo(reflectType(MemoryModel))) { @@ -21,15 +21,18 @@ class MemoryService extends Service { } Future index([Map params]) async { - return items.keys - .where((index) => items[index] != null) - .map((index) => _makeJson(index, items[index])) - .toList(); + List result = []; + + for (int i = 0; i < items.length; i++) { + result.add(_makeJson(i, items[i])); + } + + return result; } Future read(id, [Map params]) async { int desiredId = int.parse(id.toString()); - if (items.containsKey(desiredId)) { + if (items.length > desiredId) { MemoryModel found = items[desiredId]; if (found != null) { return _makeJson(desiredId, found); @@ -43,7 +46,7 @@ class MemoryService extends Service { data, outputType: T); created.id = items.length; - items[created.id] = created; + items.add(created); return created; /*} catch (e) { throw new AngelHttpException.BadRequest(message: 'Invalid data.'); @@ -52,7 +55,7 @@ class MemoryService extends Service { Future modify(id, data, [Map params]) async { int desiredId = int.parse(id.toString()); - if (items.containsKey(desiredId)) { + if (items.length > desiredId) { try { Map existing = god.serializeObject(items[desiredId]); data = mergeMap([existing, data]); @@ -67,7 +70,7 @@ class MemoryService extends Service { Future update(id, data, [Map params]) async { int desiredId = int.parse(id.toString()); - if (items.containsKey(desiredId)) { + if (items.length > desiredId) { try { items[desiredId] = (data is Map) ? god.deserializeDatum(data, outputType: T) : data; @@ -80,9 +83,9 @@ class MemoryService extends Service { Future remove(id, [Map params]) async { int desiredId = int.parse(id.toString()); - if (items.containsKey(desiredId)) { + if (items.length > desiredId) { MemoryModel item = items[desiredId]; - items[desiredId] = null; + items.removeAt(desiredId); return _makeJson(desiredId, item); } else throw new AngelHttpException.NotFound(); }