Create RedisService + create test

This commit is contained in:
Tobe O 2018-10-21 08:29:42 -04:00
parent de01012758
commit 2c057f82e2
6 changed files with 74 additions and 1 deletions

View file

@ -0,0 +1 @@
2.0.0

Binary file not shown.

1
lib/angel_redis.dart Normal file
View file

@ -0,0 +1 @@
export 'src/redis_service.dart';

View file

@ -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<String, Map<String, dynamic>> {
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<Map<String, dynamic>> read(String id,
[Map<String, dynamic> 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<Map<String, dynamic>> update(String id, Map<String, dynamic> data,
[Map<String, dynamic> params]) async {
data = new Map<String, dynamic>.from(data)..['id'] = id;
await respCommands.set(_applyPrefix(id), json.encode(data));
return data;
}
@override
Future<Map<String, dynamic>> remove(String id,
[Map<String, dynamic> params]) async {
var client = respCommands.client;
throw await client.writeArrayOfBulk(['MULTI']);
}
}

View file

@ -2,3 +2,5 @@ name: angel_redis
dependencies: dependencies:
angel_framework: ^2.0.0-alpha angel_framework: ^2.0.0-alpha
resp_client: ^0.1.6 resp_client: ^0.1.6
dev_dependencies:
test: ^1.0.0

23
test/all_test.dart Normal file
View file

@ -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);
});
}