129 lines
4.2 KiB
Dart
129 lines
4.2 KiB
Dart
|
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;
|
||
|
|
||
|
Future<List<ServiceProvider>> getServiceProvider(
|
||
|
{required int serviceTypeId}) async {
|
||
|
String path = Url.instance.getServiceType();
|
||
|
Map<String, String> params = {"service_type__id": serviceTypeId.toString()};
|
||
|
List<ServiceProvider> serviceProviders = [];
|
||
|
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);
|
||
|
serviceProviders.add(commService.serviceProvider!);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
} 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,
|
||
|
"_commServiceId": contactInfo.commService!.serviceProvider!.id!
|
||
|
};
|
||
|
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
|
||
|
// 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,
|
||
|
// "_numbermail": contactInfo.numbermail,
|
||
|
// "_active": contactInfo.active,
|
||
|
// "_primary": contactInfo.primary,
|
||
|
// "_commServiceId": contactInfo
|
||
|
// }
|
||
|
// }
|
||
|
|
||
|
////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);
|
||
|
if (response.statusCode == 20) {
|
||
|
Map data = jsonDecode(response.body);
|
||
|
success = data['success'];
|
||
|
}
|
||
|
} catch (e) {
|
||
|
throw e.toString();
|
||
|
}
|
||
|
return success;
|
||
|
}
|
||
|
}
|