import 'dart:convert'; import '../../model/profile/basic_information/adress.dart'; import '../../utils/request.dart'; import '../../utils/urls.dart'; import 'package:http/http.dart' as http; class AddressService { static final AddressService _instance = AddressService(); static AddressService get instance => _instance; ////add Future> add( {required AddressClass address, required int categoryId, required String? details, required int? blockNumber, required int? lotNumber, required String token, required int profileId}) async { Map _response = {}; String authtoken = "Token $token"; String path = '${Url.instance.addressPath()}$profileId/'; Map headers = { 'Content-Type': 'application/json; charset=UTF-8', 'Authorization': authtoken }; Map body = { "id": address.id, "details": details, "_addressCatId": categoryId, "_areaClass": address.areaClass, "_blockNo": blockNumber, "_lotNo": lotNumber, "_citymunCode": address.cityMunicipality?.code, "_brgyCode": address.barangay?.code, "_countryId": address.country!.id }; try { http.Response response = await Request.instance .postRequest(path: path, body: body, headers: headers, param: {}); if (response.statusCode == 201) { Map data = jsonDecode(response.body); _response = data['response']; _response.addAll({'data': data['response']['data']}); _response.addAll( {'message': data['response']['message']}, ); _response.addAll( {'success': true}, ); } else { Map data = jsonDecode(response.body); String message = data['response']['details']; _response.addAll({'message': message}); _response.addAll( {'success': false}, ); } return _response; } catch (e) { throw e.toString(); } } ////delete Future delete( {required int addressId, required int profileId, required String token}) async { bool? success; String authtoken = "Token $token"; String path = "${Url.instance.addressPath()}$profileId/"; Map body = {"id": addressId}; Map params = {"force_mode": "true"}; Map headers = { 'Content-Type': 'application/json; charset=UTF-8', 'Authorization': authtoken }; try { http.Response response = await Request.instance.deleteRequest( path: path, headers: headers, body: body, param: params); if (response.statusCode == 200) { Map data = jsonDecode(response.body); success = data['success']; } else { success = false; } } catch (e) { throw (e.toString()); } return success!; } ////update Future> update( {required AddressClass address, required int categoryId, required String? details, required int? blockNumber, required int? lotNumber, required String token, required int profileId}) async { Map _response = {}; String authtoken = "Token $token"; String path = '${Url.instance.addressPath()}$profileId/'; Map headers = { 'Content-Type': 'application/json; charset=UTF-8', 'Authorization': authtoken }; Map body = { "id": address.id, "details": details, "_addressCatId": categoryId, "_areaClass": address.areaClass, "_blockNo": blockNumber, "_lotNo": lotNumber, "_citymunCode": address.cityMunicipality?.code, "_brgyCode": address.barangay?.code, "_countryId": address.country!.id }; try { http.Response response = await http.patch( Uri.parse( 'https://${Url.instance.host()}${Url.instance.addressPath()}$profileId/'), headers: headers, body: jsonEncode({ "id": address.id, "details": details, "_addressCatId": categoryId, "_areaClass": address.areaClass, "_blockNo": blockNumber, "_lotNo": lotNumber, "_citymunCode": address.cityMunicipality?.code, "_brgyCode": address.barangay?.code, "_countryId": address.country!.id })); if (response.statusCode == 200) { Map data = jsonDecode(response.body); _response = data['response']; _response.addAll({'data': data['response']['data']}); _response.addAll( {'message': data['response']['message']}, ); _response.addAll( {'success': true}, ); } else { Map data = jsonDecode(response.body); String message = data['response']['details']; _response.addAll({'message': message}); _response.addAll( {'success': false}, ); } return _response; } catch (e) { throw e.toString(); } } }