passo_mobile_app/lib/sevices/profile/address_service.dart

164 lines
5.0 KiB
Dart
Raw Normal View History

2023-05-02 08:42:15 +00:00
import 'dart:convert';
import 'package:unit2/theme-data.dart/colors.dart';
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<Map<String, dynamic>> add(
{required AddressClass address,
required int categoryId,
required String? details,
required int? blockNumber,
required int? lotNumber,
required String token,
required int profileId}) async {
2023-06-06 06:54:51 +00:00
Map<String, dynamic> _response = {};
2023-05-02 08:42:15 +00:00
String authtoken = "Token $token";
String path = '${Url.instance.addressPath()}$profileId/';
Map<String, String> 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);
2023-06-06 06:54:51 +00:00
_response = data['response'];
_response.addAll({'data': data['response']['data']});
_response.addAll(
{'message': data['response']['message']},
);
_response.addAll(
{'success': true},
);
2023-05-02 08:42:15 +00:00
} else {
2023-06-06 06:54:51 +00:00
Map data = jsonDecode(response.body);
String message = data['response']['details'];
_response.addAll({'message': message});
_response.addAll(
{'success': false},
);
2023-05-02 08:42:15 +00:00
}
return _response;
} catch (e) {
throw e.toString();
}
}
////delete
Future<bool> 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<String, dynamic> params = {"force_mode": "true"};
Map<String, String> 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<Map<String, dynamic>> update(
{required AddressClass address,
required int categoryId,
required String? details,
required int? blockNumber,
required int? lotNumber,
required String token,
required int profileId}) async {
2023-06-06 06:54:51 +00:00
Map<String, dynamic> _response = {};
2023-05-02 08:42:15 +00:00
String authtoken = "Token $token";
String path = '${Url.instance.addressPath()}$profileId/';
Map<String, String> 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(
'http://${Url.instance.host()}${Url.instance.addressPath()}$profileId/'),
headers: headers,
body: jsonEncode(<String, dynamic>{
"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
}));
2023-06-06 06:54:51 +00:00
if (response.statusCode == 200) {
2023-05-02 08:42:15 +00:00
Map data = jsonDecode(response.body);
2023-06-06 06:54:51 +00:00
_response = data['response'];
_response.addAll({'data': data['response']['data']});
_response.addAll(
{'message': data['response']['message']},
);
_response.addAll(
{'success': true},
);
2023-05-02 08:42:15 +00:00
} else {
2023-06-06 06:54:51 +00:00
Map data = jsonDecode(response.body);
String message = data['response']['details'];
_response.addAll({'message': message});
_response.addAll(
{'success': false},
);
2023-05-02 08:42:15 +00:00
}
return _response;
} catch (e) {
throw e.toString();
}
}
}