returnQueryParameters function

Map<String, dynamic> returnQueryParameters({
  1. String? byCity,
  2. LatLng? byDist,
  3. String? byName,
  4. String? byState,
  5. String? byPostal,
  6. BreweryType? byType,
  7. int? byPage,
  8. int? perPage,
  9. List<SortFieldType> sortFields = const [],
  10. Sort? sortOrder,
})

Implementation

Map<String, dynamic> returnQueryParameters({
  String? byCity,
  LatLng? byDist,
  String? byName,
  String? byState,
  String? byPostal,
  BreweryType? byType,
  int? byPage,
  int? perPage,
  List<SortFieldType> sortFields = const [],
  Sort? sortOrder,
}) {
  Map<String, dynamic>? qp = {};
  if (byCity != null) {
    qp[_by_city] = byCity;
  }
  if (byDist != null) {
    qp[_by_dist] = '${byDist.latitude},${byDist.longitude}';
  }
  if (byName != null) {
    qp[_by_name] = byName;
  }
  if (byState != null) {
    qp[_by_state] = byState;
  }
  if (byPostal != null) {
    qp[_by_postal] = byPostal;
  }
  if (byType != null) {
    qp[_by_type] = byType.toString().split('.').last;
  }
  if (byPage != null) {
    qp[_by_page] = byPage;
  }
  if (perPage != null) {
    if (perPage > 50) {
      throw Exception("Max per page is 50.");
    }
    if (perPage < 1) {
      throw Exception("Minimum per page is 1.");
    }
    qp[_per_page] = perPage.toString(); // issue1 fix
  }
  if (sortFields.isNotEmpty) {
    String lastString =
        sortFields[sortFields.length - 1].toString().split('.').last;

    for (var s in sortFields) {
      String current = s.toString().split('.').last;
      if (lastString == current) {
        if (qp.containsKey(_sort)) {
          qp[_sort] += current;
        } else {
          qp[_sort] = current;
        }
      } else {
        if (qp.containsKey(_sort)) {
          qp[_sort] += current + ",";
        } else {
          qp[_sort] = current + ",";
        }
      }
    }
    if (sortOrder != null) {
      String so = sortOrder.toString().split('.').last;
      qp[_sort] += ':$so';
    }
  }
  return qp;
}