get_coupon method

dynamic get_coupon({
  1. String? context,
  2. int? id,
  3. int? page,
  4. int? perPage,
  5. String? search,
  6. String? after,
  7. String? before,
  8. String? modifiedAfter,
  9. String? modifiedBefore,
  10. bool? datesAreGmt,
  11. List<int>? exclude,
  12. List<int>? include,
  13. int? offset,
  14. Order? order,
  15. String? orderBy,
  16. String? code,
})

Implementation

get_coupon({
  String? context,
  int? id,
  int? page,
  int? perPage,
  String? search,
  String? after,
  String? before,
  String? modifiedAfter,
  String? modifiedBefore,
  bool? datesAreGmt,
  List<int>? exclude,
  List<int>? include,
  int? offset,
  Order? order,
  String? orderBy,
  String? code,
}) async {
  String request_api = 'coupons';

  // If id is provided, fetch a single coupon
  if (id != null) {
    request_api = 'coupons/$id';
  } else {
    // If id is not provided, fetch all coupons with pagination and other parameters
    if (perPage != null) {
      request_api += '?per_page=$perPage';
    } else {
      request_api += '?per_page=10'; // Default perPage
    }

    if (context != null) request_api += '&context=$context';
    if (page != null) request_api += '&page=$page';
    if (search != null) request_api += '&search=$search';
    if (after != null) request_api += '&after=$after';
    if (before != null) request_api += '&before=$before';
    if (modifiedAfter != null) {
      request_api += '&modified_after=$modifiedAfter';
    }
    if (modifiedBefore != null) {
      request_api += '&modified_before=$modifiedBefore';
    }
    if (datesAreGmt != null) request_api += '&dates_are_gmt=$datesAreGmt';
    if (exclude != null) request_api += '&exclude=${exclude.join(',')}';
    if (include != null) request_api += '&include=${include.join(',')}';
    if (offset != null) request_api += '&offset=$offset';
    if (order != null) {
      String orderType = 'asc';
      if (order == Order.ascending) {
        orderType = 'asc';
      }
      if (order == Order.descending) {
        orderType = 'desc';
      }
      request_api = '$request_api&order=$orderType';
    }
    if (orderBy != null) request_api += '&orderby=$orderBy';
    if (code != null) request_api += '&code=$code';
  }

  Response res = await ApiServices()
      .getRequest(request_api, baseUrl, consumerKey, consumerSecret);
  var decode_data = json.decode(res.body);

  // If id is provided, return a single CouponModel object
  if (id != null) {
    return CouponModel.fromJson(decode_data);
  } else {
    // If id is not provided, return a list of CouponModel objects
    List<CouponModel> parsed_list = [];
    for (var i = 0; i < decode_data.length; i++) {
      CouponModel parsed_data = CouponModel.fromJson(decode_data[i]);
      parsed_list.add(parsed_data);
    }
    return parsed_list;
  }
}