-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathparse_object.dart
278 lines (245 loc) · 9.33 KB
/
parse_object.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
part of flutter_parse_sdk;
class ParseObject extends ParseBase implements ParseCloneable {
/// Creates a new Parse Object
///
/// [String] className refers to the Table Name in your Parse Server,
/// [bool] debug will overwrite the current default debug settings and
/// [ParseHttpClient] can be overwritten to create your own HTTP Client
ParseObject(String className,
{bool debug, ParseHTTPClient client, bool autoSendSessionId})
: super() {
setClassName(className);
_path = '$keyEndPointClasses$className';
_debug = isDebugEnabled(objectLevelDebug: debug);
_client = client ??
ParseHTTPClient(
sendSessionId:
autoSendSessionId ?? ParseCoreData().autoSendSessionId,
securityContext: ParseCoreData().securityContext);
}
ParseObject.clone(String className) : this(className);
@override
dynamic clone(Map<String, dynamic> map) =>
ParseObject.clone(className)..fromJson(map);
String _path;
bool _debug;
ParseHTTPClient _client;
/// Gets an object from the server using it's [String] objectId
Future<ParseResponse> getObject(String objectId) async {
try {
String uri = _path;
if (objectId != null) {
uri += '/$objectId';
}
final Uri url = getSanitisedUri(_client, '$uri');
final Response result = await _client.get(url);
return handleResponse<ParseObject>(
this, result, ParseApiRQ.get, _debug, className);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.get, _debug, className);
}
}
/// Gets all objects from this table - Limited response at the moment
Future<ParseResponse> getAll() async {
try {
final Uri url = getSanitisedUri(_client, '$_path');
final Response result = await _client.get(url);
return handleResponse<ParseObject>(
this, result, ParseApiRQ.getAll, _debug, className);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.getAll, _debug, className);
}
}
/// Creates a new object and saves it online
Future<ParseResponse> create() async {
try {
final Uri url = getSanitisedUri(_client, '$_path');
final String body = json.encode(toJson(forApiRQ: true));
final Response result = await _client.post(url, body: body);
//Set the objectId on the object after it is created.
//This allows you to perform operations on the object after creation
if (result.statusCode == 201) {
final Map<String, dynamic> map = json.decode(result.body);
objectId = map['objectId'].toString();
}
return handleResponse<ParseObject>(
this, result, ParseApiRQ.create, _debug, className);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.create, _debug, className);
}
}
/// Saves the current object online
Future<ParseResponse> save() async {
if (getObjectData()[keyVarObjectId] == null) {
return create();
} else {
try {
final Uri url = getSanitisedUri(_client, '$_path/$objectId');
final String body = json.encode(toJson(forApiRQ: true));
final Response result = await _client.put(url, body: body);
return handleResponse<ParseObject>(
this, result, ParseApiRQ.save, _debug, className);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.save, _debug, className);
}
}
}
/// Removes an element from an Array
@Deprecated('Prefer to use the setRemove() method in save()')
Future<ParseResponse> remove(String key, dynamic values) async {
if (key != null) {
return await _sortArrays(ParseApiRQ.remove, 'Remove', key, values);
} else {
return null;
}
}
/// Removes an element from an Array
void setRemove(String key, dynamic values) {
_arrayOperation('Remove', key, values);
}
/// Remove multiple elements from an array of an object
@Deprecated('Prefer to use the setRemoveAll() method in save()')
Future<ParseResponse> removeAll(String key, List<dynamic> values) async {
if (key != null) {
return await _sortArrays(ParseApiRQ.removeAll, 'Remove', key, values);
} else {
return null;
}
}
/// Remove multiple elements from an array of an object
void setRemoveAll(String key, List<dynamic> values) {
_arrayOperation('Remove', key, values);
}
/// Add a multiple elements to an array of an object
@Deprecated('Prefer to use the setAddAll() method in save()')
Future<ParseResponse> addAll(String key, List<dynamic> values) async {
if (key != null) {
return await _sortArrays(ParseApiRQ.addAll, 'Add', key, values);
} else {
return null;
}
}
/// Add a multiple elements to an array of an object
void setAddAll(String key, List<dynamic> values) {
_arrayOperation('Add', key, values);
}
/// Add a multiple elements to an array of an object, but only when they are unique
@Deprecated('Prefer to use the setAddAll() method in save()')
Future<ParseResponse> addUnique(String key, List<dynamic> values) async {
if (key != null) {
return await _sortArrays(ParseApiRQ.addUnique, 'AddUnique', key, values);
} else {
return null;
}
}
/// Add a multiple elements to an array of an object
void setAddUnique(String key, List<dynamic> values) {
_arrayOperation('AddUnique', key, values);
}
/// Add a single element to an array of an object
@Deprecated('Prefer to use the setAdd() method in save()')
Future<ParseResponse> add(String key, dynamic values) async {
if (key != null) {
return await _sortArrays(ParseApiRQ.add, 'Add', key, values);
} else {
return null;
}
}
/// Add a single element to an array of an object
void setAdd(String key, dynamic values) {
_arrayOperation('Add', key, values);
}
/// Can be used to add arrays to a given type
Future<ParseResponse> _sortArrays(ParseApiRQ apiRQType, String arrayAction,
String key, List<dynamic> values) async {
try {
if (objectId != null) {
final Uri url = getSanitisedUri(_client, '$_path/$objectId');
final String body =
'{\"$key\":{\"__op\":\"$arrayAction\",\"objects\":${json.encode(parseEncode(values))}}}';
final Response result = await _client.put(url, body: body);
return handleResponse<ParseObject>(
this, result, apiRQType, _debug, className);
} else {
return null;
}
} on Exception catch (e) {
return handleException(e, apiRQType, _debug, className);
}
}
/// Used in array Operations in save() method
void _arrayOperation(String arrayAction, String key, List<dynamic> values) {
set<Map<String, dynamic>>(
key, <String, dynamic>{'__op': arrayAction, 'objects': values});
}
/// Increases a num of an object by x amount
@Deprecated('Prefer to use the setIncrement() method in save()')
Future<ParseResponse> increment(String key, num amount) async {
if (key != null) {
return await _increment(ParseApiRQ.increment, 'Increment', key, amount);
} else {
return null;
}
}
/// Increases a num of an object by x amount
void setIncrement(String key, num amount) {
set<Map<String, dynamic>>(
key, <String, dynamic>{'__op': 'Increment', 'amount': amount});
}
/// Decreases a num of an object by x amount
@Deprecated('Prefer to use the setDecrement() method in save()')
Future<ParseResponse> decrement(String key, num amount) async {
if (key != null) {
return await _increment(ParseApiRQ.decrement, 'Increment', key, -amount);
} else {
return null;
}
}
/// Decreases a num of an object by x amount
void setDecrement(String key, num amount) {
set<Map<String, dynamic>>(
key, <String, dynamic>{'__op': 'Increment', 'amount': -amount});
}
/// Can be used to add arrays to a given type
Future<ParseResponse> _increment(
ParseApiRQ apiRQType, String countAction, String key, num amount) async {
try {
if (objectId != null) {
final Uri url = getSanitisedUri(_client, '$_path/$objectId');
final String body =
'{\"$key\":{\"__op\":\"$countAction\",\"amount\":$amount}}';
final Response result = await _client.put(url, body: body);
return handleResponse<ParseObject>(
this, result, apiRQType, _debug, className);
} else {
return null;
}
} on Exception catch (e) {
return handleException(e, apiRQType, _debug, className);
}
}
/// Can be used to create custom queries
Future<ParseResponse> query(String query) async {
try {
final Uri url = getSanitisedUri(_client, '$_path', query: query);
final Response result = await _client.get(url);
return handleResponse<ParseObject>(
this, result, ParseApiRQ.query, _debug, className);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.query, _debug, className);
}
}
/// Deletes the current object locally and online
Future<ParseResponse> delete({String id, String path}) async {
try {
path ??= _path;
id ??= objectId;
final Uri url = getSanitisedUri(_client, '$_path/$id');
final Response result = await _client.delete(url);
return handleResponse<ParseObject>(
this, result, ParseApiRQ.delete, _debug, className);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.delete, _debug, className);
}
}
}