Skip to content

Commit

Permalink
Bug fixes for addObject, setData & more
Browse files Browse the repository at this point in the history
  • Loading branch information
nhathiwala committed Nov 19, 2021
1 parent 7174273 commit 90df241
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.metadata
.pub/
.vscode/
.vscode/**/*.json
.idea/

build/
Expand Down
1 change: 1 addition & 0 deletions .pubignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.metadata
.pub/
.vscode/
.vscode/**/*.json
.idea/

build/
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/src/error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ class AlgoliaError {
Map get error => _message;

int get statusCode => _statusCode;

@override
String toString() =>
'AlgoliaError(_message: $_message, _statusCode: $_statusCode)';
}
19 changes: 16 additions & 3 deletions lib/src/index_reference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class AlgoliaIndexReference extends AlgoliaQuery {
/// ID of the referenced index.
///
String get index => _index;
String get encodedIndex => Uri.encodeFull(_index);

///
/// **Settings**
Expand Down Expand Up @@ -146,8 +145,22 @@ class AlgoliaIndexReference extends AlgoliaQuery {
/// so that the resulting list will be chronologically-sorted.
///
Future<AlgoliaTask> addObject(Map<String, dynamic> 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<String, dynamic> body = json.decode(response.body);

if (!(response.statusCode >= 200 && response.statusCode < 300)) {
throw AlgoliaError._(body, response.statusCode);
}

return AlgoliaTask._(algolia, _index, body);
}

///
Expand Down
9 changes: 7 additions & 2 deletions lib/src/object_reference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down Expand Up @@ -121,7 +126,7 @@ class AlgoliaObjectReference {
data['objectID'] = _objectId;
data['createIfNotExists'] = createIfNotExists;
var response = await algolia._apiCall(
ApiRequestType.put,
ApiRequestType.post,
url,
data: data,
);
Expand Down
36 changes: 34 additions & 2 deletions lib/src/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class AlgoliaQuery {
final Map<String, dynamic> _parameters;

Map<String, dynamic> get parameters => _parameters;
String get encodedIndex => Uri.encodeFull(_index);

AlgoliaQuery _copyWithParameters(Map<String, dynamic> parameters) {
return AlgoliaQuery._(
Expand All @@ -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();
Expand All @@ -75,7 +76,38 @@ class AlgoliaQuery {
}
var response = await algolia._apiCall(
ApiRequestType.post,
'indexes/$_index/query',
'indexes/$encodedIndex/query',
data: _parameters,
);
Map<String, dynamic> 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<AlgoliaQuerySnapshot> 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(<String, dynamic>{
'attributesToRetrieve': const ['*']
});
}
var response = await algolia._apiCall(
ApiRequestType.post,
'indexes/$encodedIndex/deleteByQuery',
data: _parameters,
);
Map<String, dynamic> body = json.decode(response.body);
Expand Down
11 changes: 9 additions & 2 deletions test/algolia_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 90df241

Please sign in to comment.