Skip to content

Commit 90df241

Browse files
committed
Bug fixes for addObject, setData & more
1 parent 7174273 commit 90df241

File tree

8 files changed

+76
-9
lines changed

8 files changed

+76
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
.metadata
66
.pub/
77
.vscode/
8+
.vscode/**/*.json
89
.idea/
910

1011
build/

.pubignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
.metadata
66
.pub/
77
.vscode/
8+
.vscode/**/*.json
89
.idea/
910

1011
build/

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
),
2424
]);
2525
```
26+
- [Added] `deleteObjects` Delete by query: now you can delete objects based on your query.
27+
- [Bug] `addObject` for add object without `objectID` has been fixed.
28+
- [Bug] `setData` for set object data has been fixed [Issue #52](https://github.com/knoxpo/dart_algolia/issues/52)
29+
- [Bug] `partialUpdateObject` for partial update object data has been fixed [Issue #59](https://github.com/knoxpo/dart_algolia/issues/59)
2630
## 1.0.3
2731
### Bug fixes
2832

lib/src/error.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ class AlgoliaError {
1111
Map get error => _message;
1212

1313
int get statusCode => _statusCode;
14+
15+
@override
16+
String toString() =>
17+
'AlgoliaError(_message: $_message, _statusCode: $_statusCode)';
1418
}

lib/src/index_reference.dart

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class AlgoliaIndexReference extends AlgoliaQuery {
2121
/// ID of the referenced index.
2222
///
2323
String get index => _index;
24-
String get encodedIndex => Uri.encodeFull(_index);
2524

2625
///
2726
/// **Settings**
@@ -146,8 +145,22 @@ class AlgoliaIndexReference extends AlgoliaQuery {
146145
/// so that the resulting list will be chronologically-sorted.
147146
///
148147
Future<AlgoliaTask> addObject(Map<String, dynamic> data) async {
149-
final newDocument = object();
150-
return await newDocument.setData(data);
148+
if (data['objectID'] != null) {
149+
final newDocument = object();
150+
return await newDocument.setData(data);
151+
}
152+
var response = await algolia._apiCall(
153+
ApiRequestType.post,
154+
'indexes/$encodedIndex',
155+
data: data,
156+
);
157+
Map<String, dynamic> body = json.decode(response.body);
158+
159+
if (!(response.statusCode >= 200 && response.statusCode < 300)) {
160+
throw AlgoliaError._(body, response.statusCode);
161+
}
162+
163+
return AlgoliaTask._(algolia, _index, body);
151164
}
152165

153166
///

lib/src/object_reference.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ class AlgoliaObjectReference {
4646
if (_objectId != null) {
4747
url += '/$encodedObjectID';
4848
}
49+
if (data['objectID'] != null && _objectId == null) {
50+
url += "/${Uri.encodeFull(data['objectID'])}";
51+
} else if (data['objectID'] != null) {
52+
data.remove('objectID');
53+
}
4954
var response = await algolia._apiCall(
50-
ApiRequestType.post,
55+
ApiRequestType.put,
5156
url,
5257
data: data,
5358
);
@@ -121,7 +126,7 @@ class AlgoliaObjectReference {
121126
data['objectID'] = _objectId;
122127
data['createIfNotExists'] = createIfNotExists;
123128
var response = await algolia._apiCall(
124-
ApiRequestType.put,
129+
ApiRequestType.post,
125130
url,
126131
data: data,
127132
);

lib/src/query.dart

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class AlgoliaQuery {
3131
final Map<String, dynamic> _parameters;
3232

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

3536
AlgoliaQuery _copyWithParameters(Map<String, dynamic> parameters) {
3637
return AlgoliaQuery._(
@@ -49,7 +50,7 @@ class AlgoliaQuery {
4950
String toString() {
5051
return {
5152
'url': '${algolia._host}indexes' +
52-
(_index.isNotEmpty ? '/' + Uri.encodeFull(_index) : ''),
53+
(encodedIndex.isNotEmpty ? '/' + encodedIndex : ''),
5354
'headers': algolia._headers,
5455
'parameters': _parameters,
5556
}.toString();
@@ -75,7 +76,38 @@ class AlgoliaQuery {
7576
}
7677
var response = await algolia._apiCall(
7778
ApiRequestType.post,
78-
'indexes/$_index/query',
79+
'indexes/$encodedIndex/query',
80+
data: _parameters,
81+
);
82+
Map<String, dynamic> body = json.decode(response.body);
83+
if (!(response.statusCode >= 200 && response.statusCode < 300)) {
84+
throw AlgoliaError._(body, response.statusCode);
85+
}
86+
87+
return AlgoliaQuerySnapshot._(algolia, _index, body);
88+
}
89+
90+
///
91+
/// **DeleteObjects**
92+
///
93+
/// This will execute the query and retrieve data from Algolia with [AlgoliaQuerySnapshot]
94+
/// response.
95+
///
96+
Future<AlgoliaQuerySnapshot> deleteObjects() async {
97+
if (_parameters.containsKey('minimumAroundRadius')) {
98+
assert(
99+
(_parameters.containsKey('aroundLatLng') ||
100+
_parameters.containsKey('aroundLatLngViaIP')),
101+
'This setting only works within the context of a circular geo search, enabled by `aroundLatLng` or `aroundLatLngViaIP`.');
102+
}
103+
if (_parameters['attributesToRetrieve'] == null) {
104+
_copyWithParameters(<String, dynamic>{
105+
'attributesToRetrieve': const ['*']
106+
});
107+
}
108+
var response = await algolia._apiCall(
109+
ApiRequestType.post,
110+
'indexes/$encodedIndex/deleteByQuery',
79111
data: _parameters,
80112
);
81113
Map<String, dynamic> body = json.decode(response.body);

test/algolia_test.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,8 +1497,15 @@ void main() async {
14971497
'modifiedAt': DateTime.now(),
14981498
'price': 200,
14991499
};
1500-
taskAdded = await algolia.instance.index('contacts').addObject(addData);
1501-
await taskAdded.waitTask();
1500+
try {
1501+
taskAdded = await algolia.instance.index('contacts').addObject(addData);
1502+
// taskAdded = await algolia.instance.index('contacts').object('1').setData(addData);
1503+
await taskAdded.waitTask();
1504+
} on AlgoliaError catch (err) {
1505+
print(err.toString());
1506+
print(err.statusCode);
1507+
print(err.error);
1508+
}
15021509

15031510
// Checking if has [AlgoliaTask]
15041511
expect(taskAdded.runtimeType, AlgoliaTask);

0 commit comments

Comments
 (0)