You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
3.8 KiB
Dart
150 lines
3.8 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:http/http.dart';
|
|
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;
|
|
|
|
// todo @majd there is a method postJsonForObject, use this, rather then postJsonForResponse
|
|
|
|
Future<List<GasRefillModel>> getRequestPages({required List items, required int pageItemNumber}) async {
|
|
|
|
Map<String, dynamic> 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<GasRefillModel>((request) => GasRefillModel.fromJson(request)).toList();
|
|
}
|
|
|
|
Future<GasRefillModel> createModel({
|
|
required GasRefillModel model,
|
|
}) async {
|
|
|
|
|
|
Map<String, dynamic> body = {
|
|
"gazRefillNo": await generateGazRefillNo(),
|
|
"assignedEmployee": {
|
|
"id": UserApiClient().user?.id.toString(),
|
|
"name": UserApiClient().user?.username.toString()
|
|
},
|
|
"status": {
|
|
"id": 0,
|
|
"name": "",
|
|
"value": 0
|
|
},
|
|
};
|
|
|
|
body["gazRefillDetails"] = model.details
|
|
?.map((model) => {
|
|
"gasType": {
|
|
"id": model.type?.id,
|
|
"name": model.type?.label.toString(),
|
|
"value": model.type?.id
|
|
},
|
|
"cylinderType": {
|
|
"id": 0,
|
|
"name": "",
|
|
"value": 0
|
|
},
|
|
"cylinderSize": {
|
|
"id": model.cylinderSize?.id,
|
|
"name": model.cylinderSize?.label.toString(),
|
|
"value": model.cylinderSize?.id,
|
|
},
|
|
"requestedQty": model.requestedQuantity,
|
|
"deliverdQty": 0
|
|
|
|
})
|
|
.toList();
|
|
|
|
print(body);
|
|
|
|
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<String, dynamic> body = {
|
|
"id": oldModel?.id,
|
|
"gazRefillNo": await generateGazRefillNo(),
|
|
"assignedEmployee": {
|
|
"id": UserApiClient().user?.id.toString(),
|
|
"name": UserApiClient().user?.username.toString()
|
|
},
|
|
"status": {
|
|
"id": 0,
|
|
"name": "",
|
|
"value": 0
|
|
},
|
|
};
|
|
|
|
body["gazRefillDetails"] = newModel.details
|
|
?.map((model) => {
|
|
"gasType": {
|
|
"id": model.type?.id,
|
|
"name": model.type?.label.toString(),
|
|
"value": model.type?.id
|
|
},
|
|
"cylinderType": {
|
|
"id": 0,
|
|
"name": "",
|
|
"value": 0
|
|
},
|
|
"cylinderSize": {
|
|
"id": model.cylinderSize?.id,
|
|
"name": model.cylinderSize?.label.toString(),
|
|
"value": model.cylinderSize?.id,
|
|
},
|
|
"requestedQty": model.requestedQuantity,
|
|
"deliverdQty": 0
|
|
|
|
})
|
|
.toList();
|
|
|
|
|
|
|
|
final reponse = await ApiClient().putJsonForResponse("${URLs.host1}${URLs.updateGasRefill}/${newModel.id}", body);
|
|
|
|
oldModel?.fromGasRefillModel(newModel);
|
|
}
|
|
|
|
|
|
Future<String> generateGazRefillNo() async {
|
|
|
|
final reponse = await ApiClient().getJsonForResponse("${URLs.host1}${URLs.generateGazRefillNo}");
|
|
var data = json.decode(reponse.body);
|
|
return data['data'];
|
|
|
|
}
|
|
}
|