import 'dart:convert'; import 'package:test_sa/api/user_api_client.dart'; import '../controllers/api_routes/urls.dart'; import '../models/gas_refill/gas_refill_model.dart'; import 'api_client.dart'; class GasRefillApiClient { static final GasRefillApiClient _instance = GasRefillApiClient._internal(); GasRefillApiClient._internal(); factory GasRefillApiClient() => _instance; Future> getRequestPages({required List items, required int pageItemNumber}) async { Map body = { "uid": "${UserApiClient().user?.id}", "token": "${UserApiClient().user?.token}", "pageSize": "${(items.length) ~/ pageItemNumber}", }; final response = await ApiClient().postJsonForResponse( "${URLs.host1}${URLs.getGasRefill}", body, isFormData: false, ); // client's request was successfully received var requestsListJson = json.decode(utf8.decode(response.bodyBytes)); print(requestsListJson); return requestsListJson['data'].map((request) => GasRefillModel.fromJson(request)).toList(); } Future createModel({ required GasRefillModel model, }) async { Map body = { "uid": UserApiClient().user?.id.toString(), "token": UserApiClient().user?.token ?? "", "title": model.title ?? "", "status": "0", //model.status.value.toString(), }; body["gazRefillDetails"] = jsonEncode(model.details ?.map((model) => { "gasType": model.type?.id.toString(), "cylinderSize": model.cylinderSize?.id.toString(), "requestedQty": model.requestedQuantity.toString(), }) .toList()); final response = await ApiClient().postJsonForResponse( "${URLs.host1}${URLs.requestGasRefill}", body, isFormData: false ); return GasRefillModel.fromJson(json.decode(utf8.decode(response.bodyBytes))[0]); } Future updateModel({ required GasRefillModel? oldModel, required GasRefillModel newModel, }) async { Map body = { "uid": UserApiClient().user?.id.toString(), "token": UserApiClient().user?.token, "title": newModel.title, "status": newModel.status?.id.toString(), }; body["gazRefillDetails"] = jsonEncode(newModel.details ?.map((model) => { "gasType": model.type?.id.toString(), "cylinderSize": model.cylinderSize?.id.toString(), "requestedQty": model.requestedQuantity.toString(), "deliverdQty": model.deliveredQuantity.toString(), }) .toList()); final reponse = await ApiClient().postJsonForResponse("${URLs.host1}${URLs.updateGasRefill}/${newModel.id}", body); oldModel?.fromGasRefillModel(newModel); } }