Create RedisService + create test
This commit is contained in:
parent
de01012758
commit
2c057f82e2
6 changed files with 74 additions and 1 deletions
1
.dart_tool/pub/bin/sdk-version
Normal file
1
.dart_tool/pub/bin/sdk-version
Normal file
|
@ -0,0 +1 @@
|
|||
2.0.0
|
BIN
.dart_tool/pub/bin/test/test.dart.snapshot.dart2
Normal file
BIN
.dart_tool/pub/bin/test/test.dart.snapshot.dart2
Normal file
Binary file not shown.
1
lib/angel_redis.dart
Normal file
1
lib/angel_redis.dart
Normal file
|
@ -0,0 +1 @@
|
|||
export 'src/redis_service.dart';
|
46
lib/src/redis_service.dart
Normal file
46
lib/src/redis_service.dart
Normal 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']);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,6 @@
|
|||
name: angel_redis
|
||||
dependencies:
|
||||
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
23
test/all_test.dart
Normal 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);
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue