This commit is contained in:
thosakwe 2017-04-09 22:11:48 -04:00
parent 24d45e64b5
commit 550a334c66
4 changed files with 41 additions and 10 deletions

View file

@ -1,6 +1,5 @@
# rethink # rethink
[![version 1.0.7](https://img.shields.io/badge/pub-1.0.7-brightgreen.svg)](https://pub.dartlang.org/packages/angel_rethink)
[![version 1.0.6](https://img.shields.io/badge/pub-1.0.6-brightgreen.svg)](https://pub.dartlang.org/packages/angel_rethink)
[![build status](https://travis-ci.org/angel-dart/rethink.svg?branch=master)](https://travis-ci.org/angel-dart/rethink) [![build status](https://travis-ci.org/angel-dart/rethink.svg?branch=master)](https://travis-ci.org/angel-dart/rethink)
RethinkDB-enabled services for the Angel framework. RethinkDB-enabled services for the Angel framework.

View file

@ -1,4 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'dart:io';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:json_god/json_god.dart' as god; import 'package:json_god/json_god.dart' as god;
import 'package:rethinkdb_driver2/rethinkdb_driver2.dart'; import 'package:rethinkdb_driver2/rethinkdb_driver2.dart';
@ -54,7 +55,7 @@ class RethinkService extends Service {
RqlQuery _getQueryInner(RqlQuery query, Map params) { RqlQuery _getQueryInner(RqlQuery query, Map params) {
if (params == null || !params.containsKey('query')) if (params == null || !params.containsKey('query'))
return null; return query;
else { else {
if (params['query'] is RqlQuery) if (params['query'] is RqlQuery)
return params['query']; return params['query'];
@ -179,11 +180,42 @@ class RethinkService extends Service {
} }
@override @override
Future modify(id, data, [Map params]) => update(id, data, params); Future modify(id, data, [Map params]) async {
var d = _serialize(data);
if (d is Map && d.containsKey('id')) {
try {
await read(d['id'], params);
} on AngelHttpException catch (e) {
if (e.statusCode == HttpStatus.NOT_FOUND)
return await create(data, params);
else
rethrow;
}
}
var query = buildQuery(table.get(id?.toString()), params).update(d);
await _sendQuery(query);
return await read(id, params);
}
@override @override
Future update(id, data, [Map params]) async { Future update(id, data, [Map params]) async {
var query = buildQuery(table.get(id?.toString()), params).update(data); var d = _serialize(data);
if (d is Map && d.containsKey('id')) {
try {
await read(d['id'], params);
} on AngelHttpException catch (e) {
if (e.statusCode == HttpStatus.NOT_FOUND)
return await create(data, params);
else
rethrow;
}
}
if (d is Map && !d.containsKey('id')) d['id'] = id.toString();
var query = buildQuery(table.get(id?.toString()), params).replace(d);
await _sendQuery(query); await _sendQuery(query);
return await read(id, params); return await read(id, params);
} }

View file

@ -1,5 +1,5 @@
name: angel_rethink name: angel_rethink
version: 1.0.6 version: 1.0.7
description: RethinkDB-enabled services for the Angel framework. description: RethinkDB-enabled services for the Angel framework.
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
environment: environment:

View file

@ -19,9 +19,9 @@ main() {
app = new Angel(); app = new Angel();
app.use('/todos', new RethinkService(conn, r.table('todos'))); app.use('/todos', new RethinkService(conn, r.table('todos')));
app.onError((e, req, res) async { app.errorHandler = (e, req, res) async {
print('Whoops: $e'); print('Whoops: $e');
}); };
app.fatalErrorStream.listen((e) { app.fatalErrorStream.listen((e) {
print('Whoops: ${e.error}'); print('Whoops: ${e.error}');
@ -55,13 +55,13 @@ main() {
expect(result['completed'], equals(todo.completed)); expect(result['completed'], equals(todo.completed));
}); });
test('update', () async { test('modify', () async {
var todo = new Todo(title: 'Clean your room'); var todo = new Todo(title: 'Clean your room');
var creation = await todoService.create(todo.toJson()); var creation = await todoService.create(todo.toJson());
print('Creation: $creation'); print('Creation: $creation');
var id = creation['id']; var id = creation['id'];
var result = await todoService.update(id, {'title': 'Eat healthy'}); var result = await todoService.modify(id, {'title': 'Eat healthy'});
print('Response: $result'); print('Response: $result');
expect(result, isMap); expect(result, isMap);