-
Notifications
You must be signed in to change notification settings - Fork 0
/
unruffled_feathersjs_repository.dart
129 lines (118 loc) · 3.92 KB
/
unruffled_feathersjs_repository.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
part of unruffled_feathersjs;
mixin FeathersJsRemoteRepository<T extends DataModel> on RemoteRepository<T> {
Future<Paginate<T>?> getAllPaginated({
String? path,
bool local = false,
Map<String, String>? headers,
Map<String, dynamic>? query,
QueryBuilder<T>? queryBuilder,
OfflineExceptionCallback? onOfflineException,
String listKey = 'data',
String totalKey = 'total',
String limitKey = 'limit',
String pageKey = 'skip',
}) async {
if (queryBuilder != null) {
query?.addAll(parseQuery(queryBuilder: queryBuilder));
}
if (local) {
int limit = queryBuilder?.limit ?? 0;
int skip = queryBuilder?.page ?? 0;
var list = await localRepository.getAll(queryBuilder: queryBuilder);
return Paginate(
total: 0,
limit: limit,
skip: skip,
data: list,
);
}
return await sendRequest(
url: path ?? url(method: RequestMethod.get),
method: RequestMethod.get,
headers: headers,
query: query,
onSuccess: (data) async {
if (data is Map &&
data.containsKey(listKey) &&
data.containsKey(totalKey) &&
data.containsKey(limitKey) &&
data.containsKey(pageKey)) {
var deserialized = deserialize(data[listKey]);
for (var model in deserialized.models) {
final key = dataAdapter.key(model);
if (key == null) continue;
await localRepository.save(key, model);
}
var models = deserialized.models;
return Paginate(
total: data[totalKey],
limit: data[limitKey],
skip: data[pageKey],
data: models,
);
}
throw ('Response bad format');
},
onError: (e) => throw e,
onOfflineException: () async {
onOfflineException?.call();
return await getAllPaginated(local: true, queryBuilder: queryBuilder);
},
);
}
@override
Map<String, dynamic> parseLimit(int limit) => {'\$limit': limit};
@override
Map<String, dynamic> parsePage(int page) => {'\$skip': page};
@override
Map<String, dynamic> parseSort(SortCondition sort) =>
{'\$sort[${sort.property.property}]': sort.sort == SortType.asc ? 1 : -1};
@override
Map<String, dynamic> parseEqual(FilterCondition<T> condition) =>
{condition.property.property: condition.value};
@override
Map<String, dynamic> parseNotEqual(FilterCondition<T> condition) =>
{'${condition.property.property}[\$ne]': condition.value};
@override
Map<String, dynamic> parseGreaterThan(FilterCondition<T> condition) => {
'${condition.property.property}[${condition.include ? '\$gte' : '\$gt'}]':
condition.value
};
@override
Map<String, dynamic> parseLessThan(FilterCondition<T> condition) => {
'${condition.property.property}[${condition.include ? '\$lte' : '\$lt'}]':
condition.value
};
@override
Map<String, dynamic> parseInValues(FilterCondition<T> condition) {
return condition.values.asMap().map((key, value) => MapEntry(
'${condition.property.property}[\$in][$key]',
value,
));
}
@override
Map<String, dynamic> parseOrCondition(List<FilterOperation<T>> operations) {
Map<String, dynamic> map = {};
for (var i = 0; i < operations.length; i++) {
map.addAll(parseOperation(operations[i]).map((key, value) {
final index = key.indexOf('[');
if (index != -1) {
key =
'[${key.substring(0, index)}]${key.substring(index, key.length)}';
} else {
key = '[$key]';
}
return MapEntry('\$or[$i]$key', value);
}));
}
return map;
}
@override
Map<String, dynamic> parseAndCondition(List<FilterOperation<T>> operations) {
Map<String, dynamic> map = {};
for (var operation in operations) {
map.addAll(parseOperation(operation));
}
return map;
}
}