import 'dart:convert'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:siopas/connection/connection.dart'; import 'package:siopas/models/asset_status_model.dart'; import 'package:siopas/models/condition_peti_model.dart'; import 'package:siopas/models/customer_model.dart'; import 'package:siopas/models/transfer_peti_model.dart'; import 'package:siopas/models/type_peti_model.dart'; import 'package:siopas/models/warehouse_mode.dart'; import 'package:internet_connection_checker/internet_connection_checker.dart'; import '../migrations/databasehelper.dart'; import 'package:http/http.dart' as http; import '../models/disposal_model.dart'; import '../models/m_asset_status_model.dart'; class SyncronizationDataAPI { final conn = SqfliteDatabaseHelper.instance; Future> fetchAssetStatusFromApi() async { final apiURL = '${await getBaseUrl()}/asset-status'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['asset_status']; print('Success Fetch Data API Asset Status'); List assetStatusApiList = data .map( (item) => AssetStatusModel.fromJson(item as Map)) .toList(); return assetStatusApiList; } else { throw Exception('Failed to fetch data from API Peti'); } } Future> fetchPetiFromApi() async { final apiURL = '${await getBaseUrl()}/peti-asset'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['petis']; print('Success Fetch Data Peti'); List petiDBList = data .map((item) => PetiAssetModel.fromJson(item as Map)) .toList(); return petiDBList; } else { throw Exception('Failed to fetch data from API Peti'); } } Future> fetchWarehouseFromApi() async { final apiURL = '${await getBaseUrl()}/m-warehouse'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['warehouse']; print('Success Fetch Data Warehouse'); List warehouseDBList = data .map((item) => WarehouseModel.fromJson(item as Map)) .toList(); return warehouseDBList; } else { throw Exception('Failed to fetch data from API Warehouse'); } } Future> fetchCustomerFromApi() async { final apiURL = '${await getBaseUrl()}/m-customer'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['customers']; print('Success Fetch Data Customer'); List customerDBList = data .map((item) => CustomerModel.fromJson(item as Map)) .toList(); return customerDBList; } else { throw Exception('Failed to fetch data from API Customer'); } } Future> fetchTipePetiFromApi() async { final apiURL = '${await getBaseUrl()}/m-type-peti'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['tipe_peti']; print('Success Fetch Data Tipe Peti'); List tipePetiDBList = data .map((item) => TypePetiModel.fromJson(item as Map)) .toList(); return tipePetiDBList; } else { throw Exception('Failed to fetch data from API Tipe Peti'); } } Future> fetchKondisiPetiFromApi() async { final apiURL = '${await getBaseUrl()}/m-kondisi-peti'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['kondisi_peti']; print('Success Fetch Data Master Kondisi Peti'); List kondisiPetiDBList = data .map((item) => ConditionPetiModel.fromJson(item as Map)) .toList(); return kondisiPetiDBList; } else { throw Exception('Failed to fetch data from API Master Kondisi Peti'); } } Future> fetchTransferPetiFromApi() async { final apiURL = '${await getBaseUrl()}/m-transfer-peti'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['transfer_peti']; print('Success Fetch Data Transfer Peti'); List transferPetiDBList = data .map((item) => TransferPetiModel.fromJson(item as Map)) .toList(); return transferPetiDBList; } else { throw Exception('Failed to fetch data from API Transfer Peti'); } } Future> fetchDisposalFromApi() async { final apiURL = '${await getBaseUrl()}/m-disposal-peti'; final response = await http.get(Uri.parse(apiURL)); if (response.statusCode == 200) { List data = json.decode(response.body)['data']['disposals']; print('Success Fetch Data Disposal Peti'); List disposalPetiDBList = data .map((item) => DisposalPetiModel.fromJson(item as Map)) .toList(); return disposalPetiDBList; } else { throw Exception('Failed to fetch data from API Disposal Peti'); } } }