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.
63 lines
2.2 KiB
Dart
63 lines
2.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:test_sa/models/device/device.dart';
|
|
|
|
import '../controllers/api_routes/urls.dart';
|
|
import 'api_client.dart';
|
|
|
|
class DevicesApiClient {
|
|
static final DevicesApiClient _instance = DevicesApiClient._internal();
|
|
final List<Device> devices = [];
|
|
|
|
DevicesApiClient._internal();
|
|
|
|
factory DevicesApiClient() => _instance;
|
|
|
|
/// Fetch devices by [hospitalId] and insert the result into [devices] list
|
|
Future getEquipment(String hospitalId) async {
|
|
|
|
|
|
final response = await ApiClient().postJsonForResponse(
|
|
URLs.host1 + URLs.getEquipment,
|
|
{'client': hospitalId},
|
|
isFormData: false
|
|
);
|
|
|
|
Map equipmentListJson = json.decode(utf8.decode(response.bodyBytes));
|
|
print(equipmentListJson);
|
|
devices.clear();
|
|
devices.addAll(equipmentListJson['data'] != null ? equipmentListJson['data'].map<Device>((device) => Device.fromJson(device)).toList(): []);
|
|
debugPrint("devices : ${devices.length}");
|
|
}
|
|
|
|
/// Returns a list of devices by [hospitalId] and [serialNumber] (or | and) [number]
|
|
Future<List<Device>> getDevicesList({required String hospitalId, String? serialNumber, String? number}) async {
|
|
final response = await ApiClient().postJsonForResponse(
|
|
URLs.host1 + URLs.getEquipment,
|
|
{
|
|
'client': hospitalId,
|
|
if (serialNumber?.isEmpty == false) 'name': serialNumber,
|
|
if (number?.isEmpty == false) 'number': number,
|
|
},
|
|
isFormData: false
|
|
);
|
|
List categoriesListJson = json.decode(utf8.decode(response.bodyBytes));
|
|
return categoriesListJson.map((device) => Device.fromJson(device)).toList();
|
|
}
|
|
|
|
/// Returns a list of devices by [hospitalId] (and optionally) [serialNumber]
|
|
Future<List<Device>> getDevicesListBySN({required String hospitalId, required String serialNumber}) async {
|
|
final response = await ApiClient().postJsonForResponse(
|
|
URLs.host1 + URLs.getEquipment,
|
|
{
|
|
'client': hospitalId,
|
|
if (serialNumber.isNotEmpty) 'serial_qr': serialNumber,
|
|
},
|
|
isFormData: false
|
|
);
|
|
List categoriesListJson = json.decode(utf8.decode(response.bodyBytes));
|
|
return categoriesListJson.map((device) => Device.fromJson(device)).toList();
|
|
}
|
|
}
|