diff --git a/README.md b/README.md index 06280dad..d899a0ff 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # rethink - -[![version 1.0.6](https://img.shields.io/badge/pub-1.0.6-brightgreen.svg)](https://pub.dartlang.org/packages/angel_rethink) +[![version 1.0.7](https://img.shields.io/badge/pub-1.0.7-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) RethinkDB-enabled services for the Angel framework. diff --git a/lib/src/rethink_service.dart b/lib/src/rethink_service.dart index a869ee59..b655f3b1 100644 --- a/lib/src/rethink_service.dart +++ b/lib/src/rethink_service.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:io'; import 'package:angel_framework/angel_framework.dart'; import 'package:json_god/json_god.dart' as god; import 'package:rethinkdb_driver2/rethinkdb_driver2.dart'; @@ -54,7 +55,7 @@ class RethinkService extends Service { RqlQuery _getQueryInner(RqlQuery query, Map params) { if (params == null || !params.containsKey('query')) - return null; + return query; else { if (params['query'] is RqlQuery) return params['query']; @@ -179,11 +180,42 @@ class RethinkService extends Service { } @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 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); return await read(id, params); } diff --git a/pubspec.yaml b/pubspec.yaml index 537cf14c..242f29cd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_rethink -version: 1.0.6 +version: 1.0.7 description: RethinkDB-enabled services for the Angel framework. author: Tobe O environment: diff --git a/test/generic_test.dart b/test/generic_test.dart index e61765bf..7d9aec5e 100644 --- a/test/generic_test.dart +++ b/test/generic_test.dart @@ -19,9 +19,9 @@ main() { app = new Angel(); app.use('/todos', new RethinkService(conn, r.table('todos'))); - app.onError((e, req, res) async { + app.errorHandler = (e, req, res) async { print('Whoops: $e'); - }); + }; app.fatalErrorStream.listen((e) { print('Whoops: ${e.error}'); @@ -55,13 +55,13 @@ main() { expect(result['completed'], equals(todo.completed)); }); - test('update', () async { + test('modify', () async { var todo = new Todo(title: 'Clean your room'); var creation = await todoService.create(todo.toJson()); print('Creation: $creation'); 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'); expect(result, isMap);