Siopas Inventory PETI for ISTW Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
5.7 KiB

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<List<AssetStatusModel>> fetchAssetStatusFromApi() async {
final apiURL = '${await getBaseUrl()}/asset-status';
final response = await http.get(Uri.parse(apiURL));
if (response.statusCode == 200) {
List<dynamic> data = json.decode(response.body)['data']['asset_status'];
print('Success Fetch Data API Asset Status');
List<AssetStatusModel> assetStatusApiList = data
.map(
(item) => AssetStatusModel.fromJson(item as Map<String, dynamic>))
.toList();
return assetStatusApiList;
} else {
throw Exception('Failed to fetch data from API Peti');
}
}
Future<List<PetiAssetModel>> fetchPetiFromApi() async {
final apiURL = '${await getBaseUrl()}/peti-asset';
final response = await http.get(Uri.parse(apiURL));
if (response.statusCode == 200) {
List<dynamic> data = json.decode(response.body)['data']['petis'];
print('Success Fetch Data Peti');
List<PetiAssetModel> petiDBList = data
.map((item) => PetiAssetModel.fromJson(item as Map<String, dynamic>))
.toList();
return petiDBList;
} else {
throw Exception('Failed to fetch data from API Peti');
}
}
Future<List<WarehouseModel>> fetchWarehouseFromApi() async {
final apiURL = '${await getBaseUrl()}/m-warehouse';
final response = await http.get(Uri.parse(apiURL));
if (response.statusCode == 200) {
List<dynamic> data = json.decode(response.body)['data']['warehouse'];
print('Success Fetch Data Warehouse');
List<WarehouseModel> warehouseDBList = data
.map((item) => WarehouseModel.fromJson(item as Map<String, dynamic>))
.toList();
return warehouseDBList;
} else {
throw Exception('Failed to fetch data from API Warehouse');
}
}
Future<List<CustomerModel>> fetchCustomerFromApi() async {
final apiURL = '${await getBaseUrl()}/m-customer';
final response = await http.get(Uri.parse(apiURL));
if (response.statusCode == 200) {
List<dynamic> data = json.decode(response.body)['data']['customers'];
print('Success Fetch Data Customer');
List<CustomerModel> customerDBList = data
.map((item) => CustomerModel.fromJson(item as Map<String, dynamic>))
.toList();
return customerDBList;
} else {
throw Exception('Failed to fetch data from API Customer');
}
}
Future<List<TypePetiModel>> fetchTipePetiFromApi() async {
final apiURL = '${await getBaseUrl()}/m-type-peti';
final response = await http.get(Uri.parse(apiURL));
if (response.statusCode == 200) {
List<dynamic> data = json.decode(response.body)['data']['tipe_peti'];
print('Success Fetch Data Tipe Peti');
List<TypePetiModel> tipePetiDBList = data
.map((item) => TypePetiModel.fromJson(item as Map<String, dynamic>))
.toList();
return tipePetiDBList;
} else {
throw Exception('Failed to fetch data from API Tipe Peti');
}
}
Future<List<ConditionPetiModel>> fetchKondisiPetiFromApi() async {
final apiURL = '${await getBaseUrl()}/m-kondisi-peti';
final response = await http.get(Uri.parse(apiURL));
if (response.statusCode == 200) {
List<dynamic> data = json.decode(response.body)['data']['kondisi_peti'];
print('Success Fetch Data Master Kondisi Peti');
List<ConditionPetiModel> kondisiPetiDBList = data
.map((item) =>
ConditionPetiModel.fromJson(item as Map<String, dynamic>))
.toList();
return kondisiPetiDBList;
} else {
throw Exception('Failed to fetch data from API Master Kondisi Peti');
}
}
Future<List<TransferPetiModel>> fetchTransferPetiFromApi() async {
final apiURL = '${await getBaseUrl()}/m-transfer-peti';
final response = await http.get(Uri.parse(apiURL));
if (response.statusCode == 200) {
List<dynamic> data = json.decode(response.body)['data']['transfer_peti'];
print('Success Fetch Data Transfer Peti');
List<TransferPetiModel> transferPetiDBList = data
.map((item) =>
TransferPetiModel.fromJson(item as Map<String, dynamic>))
.toList();
return transferPetiDBList;
} else {
throw Exception('Failed to fetch data from API Transfer Peti');
}
}
Future<List<DisposalPetiModel>> fetchDisposalFromApi() async {
final apiURL = '${await getBaseUrl()}/m-disposal-peti';
final response = await http.get(Uri.parse(apiURL));
if (response.statusCode == 200) {
List<dynamic> data = json.decode(response.body)['data']['disposals'];
print('Success Fetch Data Disposal Peti');
List<DisposalPetiModel> disposalPetiDBList = data
.map((item) =>
DisposalPetiModel.fromJson(item as Map<String, dynamic>))
.toList();
return disposalPetiDBList;
} else {
throw Exception('Failed to fetch data from API Disposal Peti');
}
}
}