Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat queries update #761

Merged
merged 6 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 63 additions & 42 deletions templates/dart/lib/query.dart.twig
Original file line number Diff line number Diff line change
@@ -1,99 +1,120 @@
part of {{ language.params.packageName }};

import 'dart:convert';

/// Helper class to generate query strings.
class Query {
Query._();
final String method;
final String? attribute;
final dynamic values;

Query._(this.method, [this.attribute = null, this.values = null]);

Map<String, dynamic> toJson() {
final map = <String, dynamic>{
'method': method,
};

if(attribute != null) {
map['attribute'] = attribute;
}

if(values != null) {
map['values'] = values is List ? values : [values];
}

return map;
}

@override
String toString() => jsonEncode(toJson());

/// Filter resources where [attribute] is equal to [value].
///
/// [value] can be a single value or a list. If a list is used
/// the query will return resources where [attribute] is equal
/// to any of the values in the list.
static String equal(String attribute, dynamic value) =>
_addQuery(attribute, 'equal', value);
static Query equal(String attribute, dynamic value) =>
Query._('equal', attribute, value);

/// Filter resources where [attribute] is not equal to [value].
///
/// [value] can be a single value or a list. If a list is used
/// the query will return resources where [attribute] is equal
/// to any of the values in the list.
static String notEqual(String attribute, dynamic value) =>
_addQuery(attribute, 'notEqual', value);
static Query notEqual(String attribute, dynamic value) =>
Query._('notEqual', attribute, [value]);

/// Filter resources where [attribute] is less than [value].
static String lessThan(String attribute, dynamic value) =>
_addQuery(attribute, 'lessThan', value);
static Query lessThan(String attribute, dynamic value) =>
Query._('lessThan', attribute, value);

/// Filter resources where [attribute] is less than or equal to [value].
static String lessThanEqual(String attribute, dynamic value) =>
_addQuery(attribute, 'lessThanEqual', value);
static Query lessThanEqual(String attribute, dynamic value) =>
Query._('lessThanEqual', attribute, value);

/// Filter resources where [attribute] is greater than [value].
static String greaterThan(String attribute, dynamic value) =>
_addQuery(attribute, 'greaterThan', value);
static Query greaterThan(String attribute, dynamic value) =>
Query._('greaterThan', attribute, value);

/// Filter resources where [attribute] is greater than or equal to [value].
static String greaterThanEqual(String attribute, dynamic value) =>
_addQuery(attribute, 'greaterThanEqual', value);
static Query greaterThanEqual(String attribute, dynamic value) =>
Query._('greaterThanEqual', attribute, value);

/// Filter resources where by searching [attribute] for [value].
static String search(String attribute, String value) =>
_addQuery(attribute, 'search', value);
static Query search(String attribute, String value) =>
Query._('search', attribute, value);

/// Filter resources where [attribute] is null.
static String isNull(String attribute) => 'isNull("$attribute")';
static Query isNull(String attribute) => Query._('isNull', attribute);

/// Filter resources where [attribute] is not null.
static String isNotNull(String attribute) => 'isNotNull("$attribute")';
static Query isNotNull(String attribute) => Query._('isNotNull', attribute);

/// Filter resources where [attribute] is between [start] and [end] (inclusive).
static String between(String attribute, dynamic start, dynamic end) =>
'between("$attribute", ${_parseValues(start)}, ${_parseValues(end)})';
static Query between(String attribute, dynamic start, dynamic end) =>
Query._('between', attribute, [start, end]);

/// Filter resources where [attribute] starts with [value].
static String startsWith(String attribute, String value) =>
_addQuery(attribute, 'startsWith', value);
static Query startsWith(String attribute, String value) =>
Query._('startsWith', attribute, value);

/// Filter resources where [attribute] ends with [value].
static String endsWith(String attribute, String value) =>
_addQuery(attribute, 'endsWith', value);
static Query endsWith(String attribute, String value) =>
Query._('endsWith', attribute, value);

/// Filter resouorces where [attribute] contains [value]
/// [value] can be a single value or a list.
static Query contains(String attribute, dynamic value) =>
Query._('contains', attribute, value);

static Query or(List<Query> queries) => Query._('or', null, queries);

/// Specify which attributes should be returned by the API call.
static String select(List<String> attributes) =>
'select([${attributes.map((attr) => "\"$attr\"").join(",")}])';
static Query select(List<String> attributes) =>
Query._('select', null, attributes);

/// Sort results by [attribute] ascending.
static String orderAsc(String attribute) => 'orderAsc("$attribute")';
static Query orderAsc(String attribute) => Query._('orderAsc', attribute);

/// Sort results by [attribute] descending.
static String orderDesc(String attribute) => 'orderDesc("$attribute")';
static Query orderDesc(String attribute) => Query._('orderDesc', attribute);

/// Return results before [id].
///
/// Refer to the [Cursor Based Pagination]({{sdk.url}}/docs/pagination#cursor-pagination)
/// docs for more information.
static String cursorBefore(String id) => 'cursorBefore("$id")';
static Query cursorBefore(String id) => Query._('cursorBefore', null, id);

/// Return results after [id].
///
/// Refer to the [Cursor Based Pagination]({{sdk.url}}/docs/pagination#cursor-pagination)
/// docs for more information.
static String cursorAfter(String id) => 'cursorAfter("$id")';
static Query cursorAfter(String id) => Query._('cursorAfter', null, id);

/// Return only [limit] results.
static String limit(int limit) => 'limit($limit)';
static Query limit(int limit) => Query._('limit', null, limit);

/// Return results from [offset].
///
/// Refer to the [Offset Pagination]({{sdk.url}}/docs/pagination#offset-pagination)
/// docs for more information.
static String offset(int offset) => 'offset($offset)';

static String _addQuery(String attribute, String method, dynamic value) => (value
is List)
? '$method("$attribute", [${value.map((item) => _parseValues(item)).join(",")}])'
: '$method("$attribute", [${_parseValues(value)}])';
static Query offset(int offset) => Query._('offset', null, offset);

static String _parseValues(dynamic value) =>
(value is String) ? '"$value"' : '$value';
}
Loading
Loading