findByDate method

List<Map<String, dynamic>> findByDate(
  1. DateTime date, {
  2. bool includeCreatedAt = true,
  3. bool includeUpdatedAt = false,
  4. DateSearchType searchType = DateSearchType.exact,
})

Implementation

List<Map<String, dynamic>> findByDate(DateTime date,
    {bool includeCreatedAt = true,
    bool includeUpdatedAt = false,
    DateSearchType searchType = DateSearchType.exact}) {
  return List<Map<String, dynamic>>.from(collection.values.where((document) {
    bool isMatch = false;
    DateTime createdAt = DateTime.parse(document['createdAt']);
    DateTime updatedAt = DateTime.parse(document['updatedAt']);
    if (includeCreatedAt) {
      switch (searchType) {
        case DateSearchType.exact:
          isMatch = createdAt.year == date.year &&
              createdAt.month == date.month &&
              createdAt.day == date.day;
          break;
        case DateSearchType.after:
          isMatch =
              createdAt.isAfter(date) || createdAt.isAtSameMomentAs(date);
          break;
        case DateSearchType.before:
          isMatch =
              createdAt.isBefore(date) || createdAt.isAtSameMomentAs(date);
          break;
      }
    }
    if (!isMatch && includeUpdatedAt) {
      switch (searchType) {
        case DateSearchType.exact:
          isMatch = updatedAt.year == date.year &&
              updatedAt.month == date.month &&
              updatedAt.day == date.day;
          break;
        case DateSearchType.after:
          isMatch =
              updatedAt.isAfter(date) || updatedAt.isAtSameMomentAs(date);
          break;
        case DateSearchType.before:
          isMatch =
              updatedAt.isBefore(date) || updatedAt.isAtSameMomentAs(date);
          break;
      }
    }
    return isMatch;
  }).toList());
}