1
1
part of {{ language .params .packageName }};
2
2
3
+
3
4
/// Helper class to generate query strings.
4
5
class Query {
5
- Query._();
6
+ final String method;
7
+ final String? attribute;
8
+ final dynamic values;
9
+
10
+ Query._(this.method, [this.attribute = null, this.values = null]);
11
+
12
+ Map<String , dynamic > toJson() {
13
+ final map = <String , dynamic >{
14
+ 'method': method,
15
+ };
16
+
17
+ if(attribute != null) {
18
+ map['attribute'] = attribute;
19
+ }
20
+
21
+ if(values != null) {
22
+ map['values'] = values is List ? values : [values];
23
+ }
24
+
25
+ return map;
26
+ }
27
+
28
+ @override
29
+ String toString() => jsonEncode(toJson());
6
30
7
31
/// Filter resources where [attribute] is equal to [value].
8
32
///
9
33
/// [value] can be a single value or a list. If a list is used
10
34
/// the query will return resources where [attribute] is equal
11
35
/// to any of the values in the list.
12
36
static String equal(String attribute, dynamic value) =>
13
- _addQuery(attribute, 'equal', value);
37
+ Query._( 'equal', attribute, value).toString( );
14
38
15
39
/// Filter resources where [attribute] is not equal to [value].
16
- ///
17
- /// [value] can be a single value or a list. If a list is used
18
- /// the query will return resources where [attribute] is equal
19
- /// to any of the values in the list.
20
40
static String notEqual(String attribute, dynamic value) =>
21
- _addQuery(attribute, 'notEqual', value);
41
+ Query._( 'notEqual', attribute, [ value]).toString( );
22
42
23
43
/// Filter resources where [attribute] is less than [value].
24
44
static String lessThan(String attribute, dynamic value) =>
25
- _addQuery(attribute, 'lessThan', value);
45
+ Query._( 'lessThan', attribute, value).toString( );
26
46
27
47
/// Filter resources where [attribute] is less than or equal to [value].
28
48
static String lessThanEqual(String attribute, dynamic value) =>
29
- _addQuery(attribute, 'lessThanEqual', value);
49
+ Query._( 'lessThanEqual', attribute, value).toString( );
30
50
31
51
/// Filter resources where [attribute] is greater than [value].
32
52
static String greaterThan(String attribute, dynamic value) =>
33
- _addQuery(attribute, 'greaterThan', value);
53
+ Query._( 'greaterThan', attribute, value).toString( );
34
54
35
55
/// Filter resources where [attribute] is greater than or equal to [value].
36
56
static String greaterThanEqual(String attribute, dynamic value) =>
37
- _addQuery(attribute, 'greaterThanEqual', value);
57
+ Query._( 'greaterThanEqual', attribute, value).toString( );
38
58
39
59
/// Filter resources where by searching [attribute] for [value].
40
60
static String search(String attribute, String value) =>
41
- _addQuery(attribute, 'search', value);
61
+ Query._( 'search', attribute, value).toString( );
42
62
43
63
/// Filter resources where [attribute] is null.
44
- static String isNull(String attribute) => 'isNull("$ attribute")' ;
64
+ static String isNull(String attribute) => Query._( 'isNull', attribute).toString() ;
45
65
46
66
/// Filter resources where [attribute] is not null.
47
- static String isNotNull(String attribute) => 'isNotNull("$ attribute")' ;
67
+ static String isNotNull(String attribute) => Query._( 'isNotNull', attribute).toString() ;
48
68
49
69
/// Filter resources where [attribute] is between [start] and [end] (inclusive).
50
70
static String between(String attribute, dynamic start, dynamic end) =>
51
- 'between("$ attribute", ${_parseValues( start)}, ${_parseValues( end)})' ;
71
+ Query._( 'between', attribute, [ start, end]).toString() ;
52
72
53
73
/// Filter resources where [attribute] starts with [value].
54
74
static String startsWith(String attribute, String value) =>
55
- _addQuery(attribute, 'startsWith', value);
75
+ Query._( 'startsWith', attribute, value).toString( );
56
76
57
77
/// Filter resources where [attribute] ends with [value].
58
78
static String endsWith(String attribute, String value) =>
59
- _addQuery(attribute, 'endsWith', value);
79
+ Query._('endsWith', attribute, value).toString();
80
+
81
+ /// Filter resources where [attribute] contains [value]
82
+ /// [value] can be a single value or a list.
83
+ static String contains(String attribute, dynamic value) =>
84
+ Query._('contains', attribute, value).toString();
85
+
86
+ static String or(List<String > queries) =>
87
+ Query._('and', null, queries.map((query) => jsonDecode(query)).toList()).toString();
88
+
89
+ static String and(List<String > queries) =>
90
+ Query._('and', null, queries.map((query) => jsonDecode(query)).toList()).toString();
60
91
61
92
/// Specify which attributes should be returned by the API call.
62
93
static String select(List<String > attributes) =>
63
- 'select([${attributes.map((attr) => "\"$attr\"").join(",")}])' ;
94
+ Query._('select', null, attributes).toString() ;
64
95
65
96
/// Sort results by [attribute] ascending.
66
- static String orderAsc(String attribute) => 'orderAsc("$ attribute")' ;
97
+ static String orderAsc(String attribute) => Query._( 'orderAsc', attribute).toString() ;
67
98
68
99
/// Sort results by [attribute] descending.
69
- static String orderDesc(String attribute) => 'orderDesc("$ attribute")' ;
100
+ static String orderDesc(String attribute) => Query._( 'orderDesc', attribute).toString() ;
70
101
71
102
/// Return results before [id].
72
103
///
73
104
/// Refer to the [Cursor Based Pagination]({{sdk .url }}/docs/pagination#cursor-pagination)
74
105
/// docs for more information.
75
- static String cursorBefore(String id) => 'cursorBefore("$id")' ;
106
+ static String cursorBefore(String id) => Query._( 'cursorBefore', null, id).toString() ;
76
107
77
108
/// Return results after [id].
78
109
///
79
110
/// Refer to the [Cursor Based Pagination]({{sdk .url }}/docs/pagination#cursor-pagination)
80
111
/// docs for more information.
81
- static String cursorAfter(String id) => 'cursorAfter("$id")' ;
112
+ static String cursorAfter(String id) => Query._( 'cursorAfter', null, id).toString() ;
82
113
83
114
/// Return only [limit] results.
84
- static String limit(int limit) => 'limit($ limit)' ;
115
+ static String limit(int limit) => Query._( 'limit', null, limit).toString() ;
85
116
86
117
/// Return results from [offset].
87
118
///
88
119
/// Refer to the [Offset Pagination]({{sdk .url }}/docs/pagination#offset-pagination)
89
120
/// docs for more information.
90
- static String offset(int offset) => 'offset($offset)';
91
-
92
- static String _addQuery(String attribute, String method, dynamic value) => (value
93
- is List)
94
- ? '$method("$attribute", [${value.map((item) => _parseValues(item)).join(",")}])'
95
- : '$method("$attribute", [${_parseValues(value)}])';
121
+ static String offset(int offset) => Query._('offset', null, offset).toString();
96
122
97
- static String _parseValues(dynamic value) =>
98
- (value is String) ? '"$value"' : '$value';
99
123
}
0 commit comments