diff --git a/.dart_tool/pub/bin/sdk-version b/.dart_tool/pub/bin/sdk-version new file mode 100644 index 00000000..227cea21 --- /dev/null +++ b/.dart_tool/pub/bin/sdk-version @@ -0,0 +1 @@ +2.0.0 diff --git a/.dart_tool/pub/bin/test/test.dart.snapshot.dart2 b/.dart_tool/pub/bin/test/test.dart.snapshot.dart2 new file mode 100644 index 00000000..91ac2527 Binary files /dev/null and b/.dart_tool/pub/bin/test/test.dart.snapshot.dart2 differ diff --git a/lib/angel_redis.dart b/lib/angel_redis.dart new file mode 100644 index 00000000..954851eb --- /dev/null +++ b/lib/angel_redis.dart @@ -0,0 +1 @@ +export 'src/redis_service.dart'; \ No newline at end of file diff --git a/lib/src/redis_service.dart b/lib/src/redis_service.dart new file mode 100644 index 00000000..80517f80 --- /dev/null +++ b/lib/src/redis_service.dart @@ -0,0 +1,46 @@ +import 'dart:async'; +import 'dart:convert'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:resp_client/resp_commands.dart'; + +class RedisService extends Service> { + final RespCommands respCommands; + + /// An optional string prefixed to keys before they are inserted into Redis. + /// + /// Consider using this if you are using several different Redis collections + /// within a single application. + final String prefix; + + RedisService(this.respCommands, {this.prefix}); + + String _applyPrefix(String id) => prefix == null ? id : '$prefix:$id'; + + @override + Future> read(String id, + [Map params]) async { + var value = await respCommands.get(_applyPrefix(id)); + + if (value == null) { + throw new AngelHttpException.notFound( + message: 'No record found for ID $id'); + } else { + return json.decode(value); + } + } + + @override + Future> update(String id, Map data, + [Map params]) async { + data = new Map.from(data)..['id'] = id; + await respCommands.set(_applyPrefix(id), json.encode(data)); + return data; + } + + @override + Future> remove(String id, + [Map params]) async { + var client = respCommands.client; + throw await client.writeArrayOfBulk(['MULTI']); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 275bb26e..fa8c400f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,4 +1,6 @@ name: angel_redis dependencies: angel_framework: ^2.0.0-alpha - resp_client: ^0.1.6 \ No newline at end of file + resp_client: ^0.1.6 +dev_dependencies: + test: ^1.0.0 \ No newline at end of file diff --git a/test/all_test.dart b/test/all_test.dart new file mode 100644 index 00000000..448d7212 --- /dev/null +++ b/test/all_test.dart @@ -0,0 +1,23 @@ +import 'package:angel_redis/angel_redis.dart'; +import 'package:resp_client/resp_client.dart'; +import 'package:resp_client/resp_commands.dart'; +import 'package:test/test.dart'; + +main() async { + RespServerConnection connection; + RedisService service; + + setUp(() async { + connection = await connectSocket('localhost'); + service = new RedisService(new RespCommands(new RespClient(connection)), + prefix: 'angel_redis_test'); + }); + + tearDown(() => connection.close()); + + test('create with id', () async { + var input = {'id': 'foo', 'bar': 'baz'}; + var output = await service.create(input); + expect(input, output); + }); +}