whereDate function

CoffeeQueryFilter whereDate(
  1. dynamic type,
  2. DateCompare compare,
  3. dynamic value
)

Creates a filter for date searches.

type: The property to filter by, either a String or a Function. compare: The date part to compare (e.g., DateCompare.day, DateCompare.month, etc.). value: The value to compare the date part against.

Returns a CoffeeQueryFilter configured for date searches.

Usage Example

whereDate('createdAt', DateCompare.year, 2022)
whereDate((Model model) => model.createdAt, DateCompare.dd_mm_yyyy, '27-10-2022')

Implementation

CoffeeQueryFilter whereDate(dynamic type, DateCompare compare, dynamic value) {
  final propertyName = _getPropertyName(type);

  String getExpression(String typeFormat, [dynamic formattedValue]) {
    return '$propertyName=$typeFormat=$formattedValue';
  }

  String getValueBySplitPosition(int position) {
    final splitValue = value.toString().split('-');
    return (splitValue.length > position) ? splitValue[position] : '';
  }

  switch (compare) {
    case DateCompare.day:
    case DateCompare.month:
    case DateCompare.year:
      return CoffeeQueryFilter(
        expression: getExpression(compare.toString().split('.').last, value),
        type: 'filter',
      );
    case DateCompare.dd_mm:
      final day = getValueBySplitPosition(0);
      final month = getValueBySplitPosition(1);
      return CoffeeQueryFilter(
        expression: '${getExpression('day', day)},${getExpression('month', month)}',
        type: 'filter',
      );
    case DateCompare.dd_yyyy:
      final day = getValueBySplitPosition(0);
      final year = getValueBySplitPosition(1);
      return CoffeeQueryFilter(
        expression: '${getExpression('day', day)},${getExpression('year', year)}',
        type: 'filter',
      );
    case DateCompare.mm_yyyy:
      final month = getValueBySplitPosition(0);
      final year = getValueBySplitPosition(1);
      return CoffeeQueryFilter(
        expression: '${getExpression('month', month)},${getExpression('year', year)}',
        type: 'filter',
      );
    case DateCompare.dd_mm_yyyy:
      final day = getValueBySplitPosition(0);
      final month = getValueBySplitPosition(1);
      final year = getValueBySplitPosition(2);
      return CoffeeQueryFilter(
        expression: '${getExpression('day', day)},${getExpression('month', month)},${getExpression('year', year)}',
        type: 'filter',
      );
  }
}