diff --git a/.gitignore b/.gitignore index 962edde..e6f4c44 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ .metadata .pub/ .vscode/ +.vscode/**/*.json .idea/ build/ diff --git a/.pubignore b/.pubignore index 962edde..e6f4c44 100644 --- a/.pubignore +++ b/.pubignore @@ -5,6 +5,7 @@ .metadata .pub/ .vscode/ +.vscode/**/*.json .idea/ build/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 3803ea7..f0bdd18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,10 @@ ), ]); ``` +- [Added] `deleteObjects` Delete by query: now you can delete objects based on your query. +- [Bug] `addObject` for add object without `objectID` has been fixed. +- [Bug] `setData` for set object data has been fixed [Issue #52](https://github.com/knoxpo/dart_algolia/issues/52) +- [Bug] `partialUpdateObject` for partial update object data has been fixed [Issue #59](https://github.com/knoxpo/dart_algolia/issues/59) ## 1.0.3 ### Bug fixes diff --git a/lib/src/error.dart b/lib/src/error.dart index 9ac41ac..8be6cd1 100644 --- a/lib/src/error.dart +++ b/lib/src/error.dart @@ -11,4 +11,8 @@ class AlgoliaError { Map get error => _message; int get statusCode => _statusCode; + + @override + String toString() => + 'AlgoliaError(_message: $_message, _statusCode: $_statusCode)'; } diff --git a/lib/src/index_reference.dart b/lib/src/index_reference.dart index cbdd725..5cf51de 100644 --- a/lib/src/index_reference.dart +++ b/lib/src/index_reference.dart @@ -21,7 +21,6 @@ class AlgoliaIndexReference extends AlgoliaQuery { /// ID of the referenced index. /// String get index => _index; - String get encodedIndex => Uri.encodeFull(_index); /// /// **Settings** @@ -146,8 +145,22 @@ class AlgoliaIndexReference extends AlgoliaQuery { /// so that the resulting list will be chronologically-sorted. /// Future addObject(Map data) async { - final newDocument = object(); - return await newDocument.setData(data); + if (data['objectID'] != null) { + final newDocument = object(); + return await newDocument.setData(data); + } + var response = await algolia._apiCall( + ApiRequestType.post, + 'indexes/$encodedIndex', + data: data, + ); + Map body = json.decode(response.body); + + if (!(response.statusCode >= 200 && response.statusCode < 300)) { + throw AlgoliaError._(body, response.statusCode); + } + + return AlgoliaTask._(algolia, _index, body); } /// diff --git a/lib/src/object_reference.dart b/lib/src/object_reference.dart index b5d62c9..5474f02 100644 --- a/lib/src/object_reference.dart +++ b/lib/src/object_reference.dart @@ -46,8 +46,13 @@ class AlgoliaObjectReference { if (_objectId != null) { url += '/$encodedObjectID'; } + if (data['objectID'] != null && _objectId == null) { + url += "/${Uri.encodeFull(data['objectID'])}"; + } else if (data['objectID'] != null) { + data.remove('objectID'); + } var response = await algolia._apiCall( - ApiRequestType.post, + ApiRequestType.put, url, data: data, ); @@ -121,7 +126,7 @@ class AlgoliaObjectReference { data['objectID'] = _objectId; data['createIfNotExists'] = createIfNotExists; var response = await algolia._apiCall( - ApiRequestType.put, + ApiRequestType.post, url, data: data, ); diff --git a/lib/src/query.dart b/lib/src/query.dart index d99f47e..b6c6114 100644 --- a/lib/src/query.dart +++ b/lib/src/query.dart @@ -31,6 +31,7 @@ class AlgoliaQuery { final Map _parameters; Map get parameters => _parameters; + String get encodedIndex => Uri.encodeFull(_index); AlgoliaQuery _copyWithParameters(Map parameters) { return AlgoliaQuery._( @@ -49,7 +50,7 @@ class AlgoliaQuery { String toString() { return { 'url': '${algolia._host}indexes' + - (_index.isNotEmpty ? '/' + Uri.encodeFull(_index) : ''), + (encodedIndex.isNotEmpty ? '/' + encodedIndex : ''), 'headers': algolia._headers, 'parameters': _parameters, }.toString(); @@ -75,7 +76,38 @@ class AlgoliaQuery { } var response = await algolia._apiCall( ApiRequestType.post, - 'indexes/$_index/query', + 'indexes/$encodedIndex/query', + data: _parameters, + ); + Map body = json.decode(response.body); + if (!(response.statusCode >= 200 && response.statusCode < 300)) { + throw AlgoliaError._(body, response.statusCode); + } + + return AlgoliaQuerySnapshot._(algolia, _index, body); + } + + /// + /// **DeleteObjects** + /// + /// This will execute the query and retrieve data from Algolia with [AlgoliaQuerySnapshot] + /// response. + /// + Future deleteObjects() async { + if (_parameters.containsKey('minimumAroundRadius')) { + assert( + (_parameters.containsKey('aroundLatLng') || + _parameters.containsKey('aroundLatLngViaIP')), + 'This setting only works within the context of a circular geo search, enabled by `aroundLatLng` or `aroundLatLngViaIP`.'); + } + if (_parameters['attributesToRetrieve'] == null) { + _copyWithParameters({ + 'attributesToRetrieve': const ['*'] + }); + } + var response = await algolia._apiCall( + ApiRequestType.post, + 'indexes/$encodedIndex/deleteByQuery', data: _parameters, ); Map body = json.decode(response.body); diff --git a/test/algolia_test.dart b/test/algolia_test.dart index 0263e43..00bf876 100644 --- a/test/algolia_test.dart +++ b/test/algolia_test.dart @@ -1497,8 +1497,15 @@ void main() async { 'modifiedAt': DateTime.now(), 'price': 200, }; - taskAdded = await algolia.instance.index('contacts').addObject(addData); - await taskAdded.waitTask(); + try { + taskAdded = await algolia.instance.index('contacts').addObject(addData); + // taskAdded = await algolia.instance.index('contacts').object('1').setData(addData); + await taskAdded.waitTask(); + } on AlgoliaError catch (err) { + print(err.toString()); + print(err.statusCode); + print(err.error); + } // Checking if has [AlgoliaTask] expect(taskAdded.runtimeType, AlgoliaTask);