Skip to content

Commit d336caf

Browse files
committed
Banners #5
1 parent eb553fc commit d336caf

File tree

5 files changed

+286
-230
lines changed

5 files changed

+286
-230
lines changed

lib/src/api/models/cloud_database_queries/order_by_query_filter.dart

Lines changed: 0 additions & 59 deletions
This file was deleted.

lib/src/api/models/cloud_database_queries/query_filter.dart

Lines changed: 218 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import 'order_by_query_filter.dart';
2-
import 'where_query_filter.dart';
1+
import 'package:codelessly_json_annotation/codelessly_json_annotation.dart';
2+
import 'package:equatable/equatable.dart';
3+
4+
part 'query_filter.g.dart';
35

46
/// An abstract class that represents common firestore operations.
5-
abstract class QueryFilter {
7+
sealed class QueryFilter {
68
/// The name of the field to target.
79
final String field;
810

@@ -17,3 +19,216 @@ abstract class QueryFilter {
1719
/// [OrderByQueryFilter].
1820
bool get isOrderByFilter => this is OrderByQueryFilter;
1921
}
22+
23+
/// Enum representing the Firestore where operation.
24+
///
25+
/// Reference: https://firebase.google.com/docs/firestore/query-data/queries?hl=en
26+
///
27+
/// Cloud Firestore supports the following comparison operators:
28+
/// < less than
29+
/// <= less than or equal to
30+
/// == equal to
31+
/// > greater than
32+
/// >= greater than or equal to
33+
/// != not equal to
34+
/// array-contains
35+
/// array-contains-any
36+
/// in
37+
/// not-in
38+
enum WhereQueryOperator {
39+
/// Compares two values and returns true if they are equal.
40+
equal('Equal To', '=='),
41+
42+
/// Checks if two values are not equal.
43+
notEqual('Not Equal To', '!='),
44+
45+
/// Compares two values and returns true if the first is less than the second.
46+
lessThan('Less Than', '<'),
47+
48+
/// Compares two values and returns true if the first is less than or equal
49+
/// to the second.
50+
lessThanOrEqual('Less Than Or Equal To', '<='),
51+
52+
/// Compares two values and returns true if the first is greater than the
53+
/// second.
54+
greaterThan('Greater Than', '>'),
55+
56+
/// Compares two values and returns true if the first is greater than or equal
57+
/// to the second.
58+
greaterThanOrEqual('Greater Than Or Equal To', '>='),
59+
60+
/// Checks if an array contains a value.
61+
arrayContains('Contains', 'array-contains'),
62+
63+
/// Checks if an array contains any of the values in a given array.
64+
arrayContainsAny('Contains any', 'array-contains-any'),
65+
66+
/// Checks if a value is in a given array.
67+
inArray('Is in', 'in'),
68+
69+
/// Checks if a value is not in a given array.
70+
notInArray('Is not in', 'not-in');
71+
72+
/// The human-readable name of the operation.
73+
final String label;
74+
75+
/// The operator to use in the query.
76+
final String operator;
77+
78+
const WhereQueryOperator(this.label, this.operator);
79+
80+
/// Returns true if this operation is a range operation.
81+
bool get isRangeOperator =>
82+
this == WhereQueryOperator.greaterThan ||
83+
this == WhereQueryOperator.greaterThanOrEqual ||
84+
this == WhereQueryOperator.lessThan ||
85+
this == WhereQueryOperator.lessThanOrEqual;
86+
87+
/// Returns true if this operation is an inequality operation.
88+
bool get isInequalityOperator =>
89+
isRangeOperator ||
90+
this == WhereQueryOperator.notEqual ||
91+
this == WhereQueryOperator.notInArray;
92+
93+
/// Returns true if this operation is an equality operation.
94+
bool get isEqualityOperator =>
95+
this == WhereQueryOperator.equal ||
96+
this == WhereQueryOperator.arrayContains ||
97+
this == WhereQueryOperator.arrayContainsAny ||
98+
this == WhereQueryOperator.inArray;
99+
100+
/// Returns a set of all range operators.
101+
static Set<WhereQueryOperator> get rangeOperators => {
102+
WhereQueryOperator.greaterThan,
103+
WhereQueryOperator.greaterThanOrEqual,
104+
WhereQueryOperator.lessThan,
105+
WhereQueryOperator.lessThanOrEqual,
106+
};
107+
108+
/// Returns a set of all inequality operators.
109+
static Set<WhereQueryOperator> get inequalityOperators => {
110+
...rangeOperators,
111+
WhereQueryOperator.notEqual,
112+
WhereQueryOperator.notInArray,
113+
};
114+
115+
/// Returns a set of all equality operators.
116+
static Set<WhereQueryOperator> get equalityOperators => {
117+
WhereQueryOperator.equal,
118+
WhereQueryOperator.arrayContains,
119+
WhereQueryOperator.arrayContainsAny,
120+
WhereQueryOperator.inArray,
121+
};
122+
123+
/// Returns true if the given [operators] iterable contains any equality
124+
/// operator.
125+
static bool hasEqualityOperator(Iterable<WhereQueryOperator> operators) {
126+
return operators.any((operator) => operator.isEqualityOperator);
127+
}
128+
129+
/// Returns true if the given [operators] iterable contains any inequality
130+
/// operator.
131+
static bool hasInequalityOperator(Iterable<WhereQueryOperator> operators) {
132+
return operators.any((operator) => operator.isInequalityOperator);
133+
}
134+
}
135+
136+
/// Represents the where operation in Codelessly Cloud Database.
137+
///
138+
/// Takes three parameters: a field to filter on, a comparison operator, and a value.
139+
///
140+
/// Reference: https://firebase.google.com/docs/firestore/query-data/queries?hl=en
141+
///
142+
/// TODO: Add support for OR queries.
143+
@JsonSerializable()
144+
final class WhereQueryFilter extends QueryFilter with EquatableMixin {
145+
/// The where operation to perform.
146+
final WhereQueryOperator operator;
147+
148+
/// The value to compare against.
149+
final String value;
150+
151+
/// Creates a new [WhereQueryFilter] instance, given a [field], an [operator],
152+
/// and a [value].
153+
const WhereQueryFilter({
154+
required super.field,
155+
required this.operator,
156+
required this.value,
157+
});
158+
159+
/// Creates a copy of this [WhereQueryFilter] instance, given a [field], an
160+
/// [operator], and a [value].
161+
WhereQueryFilter copyWith({
162+
String? field,
163+
WhereQueryOperator? operator,
164+
String? value,
165+
}) =>
166+
WhereQueryFilter(
167+
field: field ?? this.field,
168+
operator: operator ?? this.operator,
169+
value: value ?? this.value,
170+
);
171+
172+
/// Converts a JSON object to a [WhereQueryFilter] instance.
173+
factory WhereQueryFilter.fromJson(Map<String, dynamic> json) =>
174+
_$WhereQueryFilterFromJson(json);
175+
176+
/// Converts this [WhereQueryFilter] instance to a JSON object.
177+
Map<String, dynamic> toJson() => _$WhereQueryFilterToJson(this);
178+
179+
@override
180+
List<Object?> get props => [field, operator, value];
181+
}
182+
183+
/// Enum representing the Firestore order by operations.
184+
enum OrderByQuerySortOrder {
185+
/// Represents an ascending order operation.
186+
ascending('asc', 'In Ascending Order'),
187+
188+
/// Represents a descending order operation.
189+
descending('desc', 'In Descending Order');
190+
191+
/// The operation name.
192+
final String value;
193+
194+
/// The human-readable name of the operation.
195+
final String label;
196+
197+
const OrderByQuerySortOrder(this.value, this.label);
198+
}
199+
200+
/// Represents the order-by operation in Firestore.
201+
@JsonSerializable()
202+
final class OrderByQueryFilter extends QueryFilter with EquatableMixin {
203+
/// The order-by operation to perform.
204+
final OrderByQuerySortOrder sortOrder;
205+
206+
/// Creates a new [OrderByQueryFilter] instance, given a [field] and an
207+
/// [sortOrder].
208+
const OrderByQueryFilter({
209+
required super.field,
210+
this.sortOrder = OrderByQuerySortOrder.ascending,
211+
});
212+
213+
/// Creates a copy of this [OrderByQueryFilter] instance with the given
214+
/// parameters.
215+
OrderByQueryFilter copyWith({
216+
String? field,
217+
OrderByQuerySortOrder? sortOrder,
218+
}) {
219+
return OrderByQueryFilter(
220+
field: field ?? this.field,
221+
sortOrder: sortOrder ?? this.sortOrder,
222+
);
223+
}
224+
225+
/// Converts a JSON object to a [OrderByQueryFilter] instance.
226+
factory OrderByQueryFilter.fromJson(Map<String, dynamic> json) =>
227+
_$OrderByQueryFilterFromJson(json);
228+
229+
/// Converts this [OrderByQueryFilter] instance to a JSON object.
230+
Map<String, dynamic> toJson() => _$OrderByQueryFilterToJson(this);
231+
232+
@override
233+
List<Object?> get props => [field, sortOrder];
234+
}

lib/src/api/models/cloud_database_queries/query_filter.g.dart

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)