getSearchableFilter method

Map<String, Object?> getSearchableFilter({
  1. required Map<String, Object?> inputs,
  2. String searchFiled = 'search',
})

Implementation

Map<String, Object?> getSearchableFilter({
  required Map<String, Object?> inputs,
  String searchFiled = 'search',
}) {
  var resSearch = <String, Object?>{};
  if (inputs[searchFiled] != null &&
      inputs[searchFiled].toString().trim().isNotEmpty) {
    var searchOr = [];
    for (var key in form.fields.keys) {
      if (form.fields[key]!.searchable) {
        searchOr.add(
          DQ.field(key, DQ.like(inputs[searchFiled].toString().trim())),
        );
      }
    }

    if (searchOr.isNotEmpty) {
      resSearch = DQ.or(searchOr);
    }
  }

  var resFilter = <String, Object?>{};
  var filterAnd = [];
  for (var key in form.fields.keys) {
    if (form.fields[key]!.filterable) {
      if (inputs[key] != null) {
        filterAnd.add(
          DQ.field(
            key,
            ObjectDescovery.descovr(
              inputs[key],
              form.fields[key]!.type,
              def: form.fields[key]!.defaultValue,
            ),
          ),
        );
      }
    }
  }
  if (filterAnd.isNotEmpty) {
    resFilter = DQ.and(filterAnd);
  }

  var res = <String, Object?>{};
  if (resSearch.isNotEmpty && resFilter.isNotEmpty) {
    res = DQ.and([resSearch, resFilter]);
  }
  res = {...resSearch, ...resFilter};
  return res;
}