passo_mobile_app/lib/sevices/profile/contact_services.dart

143 lines
4.5 KiB
Dart
Raw Normal View History

2023-04-05 00:54:24 +00:00
import 'dart:convert';
import 'package:unit2/model/profile/basic_information/contact_information.dart';
import 'package:unit2/utils/request.dart';
import 'package:unit2/utils/urls.dart';
import 'package:http/http.dart' as http;
class ContactService {
static final ContactService _instance = ContactService();
static ContactService get instance => _instance;
2023-04-11 01:27:53 +00:00
Future<List<CommService>> getServiceProvider(
2023-04-05 00:54:24 +00:00
{required int serviceTypeId}) async {
2023-04-11 01:27:53 +00:00
String path = Url.instance.getCommunicationProvider();
2023-04-05 00:54:24 +00:00
Map<String, String> params = {"service_type__id": serviceTypeId.toString()};
2023-04-11 01:27:53 +00:00
List<CommService> serviceProviders = [];
2023-04-05 00:54:24 +00:00
Map<String, String> headers = {
'Content-Type': 'application/json; charset=UTF-8',
};
try {
http.Response response = await Request.instance
.getRequest(param: params, path: path, headers: headers);
if (response.statusCode == 200) {
Map data = jsonDecode(response.body);
if (data['data'] != null) {
for (var element in data['data']) {
CommService commService = CommService.fromJson(element);
2023-04-11 01:27:53 +00:00
serviceProviders.add(commService);
2023-04-05 00:54:24 +00:00
}
}
}
} catch (e) {
throw e.toString();
}
return serviceProviders;
}
//// update
Future<Map<dynamic, dynamic>> update(
{required int profileId,
required String token,
required ContactInfo contactInfo}) async {
String path = "${Url.instance.contactPath()}$profileId/";
String authToken = "Token $token";
Map<String, String> headers = {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': authToken
};
Map body = {
"personid": profileId,
"contactinfoid": contactInfo.id,
"_numbermail": contactInfo.numbermail,
"_active": contactInfo.active,
"_primary": contactInfo.primary,
2023-04-11 01:27:53 +00:00
"_commServiceId": contactInfo.commService!.id
2023-04-05 00:54:24 +00:00
};
Map<dynamic, dynamic> responseStatus = {};
try {
http.Response response = await Request.instance
.putRequest(path: path, headers: headers, body: body, param: {});
if (response.statusCode == 200) {
Map data = jsonDecode(response.body);
responseStatus = data;
} else {
responseStatus.addAll({'success': false});
}
return responseStatus;
} catch (e) {
throw e.toString();
}
}
//// add
2023-04-11 01:27:53 +00:00
Future<Map<dynamic, dynamic>> add(
{required int profileId,
required String token,
required ContactInfo contactInfo}) async {
String path = "${Url.instance.contactPath()}$profileId/";
String authToken = "Token $token";
Map<String, String> headers = {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': authToken
};
Map<dynamic, dynamic> responseStatus = {};
Map body = {
"personid": profileId,
"_numbermail": contactInfo.numbermail,
"_active": contactInfo.active,
"_primary": contactInfo.primary,
"_commServiceId": contactInfo.commService!.id
};
try {
http.Response response = await Request.instance
.postRequest(path: path, headers: headers, body: body, param: {});
if (response.statusCode == 201) {
Map data = jsonDecode(response.body);
responseStatus = data;
} else {
responseStatus.addAll({'success': false});
}
return responseStatus;
} catch (e) {
throw e.toString();
}
}
2023-04-05 00:54:24 +00:00
////delete
Future<bool> deleteContact(
{required int profileId,
required String token,
required ContactInfo contactInfo}) async {
String path = "${Url.instance.deleteContact()}$profileId/";
String authToken = "Token $token";
Map<String, String> headers = {
'Content-Type': 'application/json; charset=UTF-8',
'mode': 'same-origin',
'include': 'credentials',
'Authorization': authToken
};
bool success = false;
Map<String, dynamic> params = {"force_mode": "true"};
Map body = {
"personid": profileId,
"contactinfoid": contactInfo.id,
"_numbermail": contactInfo.numbermail,
"_active": contactInfo.active,
"_primary": contactInfo.primary,
"_commServiceId": contactInfo.commService!.id
};
try {
http.Response response = await Request.instance.deleteRequest(
path: path, headers: headers, body: body, param: params);
2023-04-11 01:27:53 +00:00
if (response.statusCode == 200) {
2023-04-05 00:54:24 +00:00
Map data = jsonDecode(response.body);
success = data['success'];
}
} catch (e) {
throw e.toString();
}
return success;
}
}