import 'dart:convert'; import 'package:unit2/model/profile/attachment.dart'; import 'package:unit2/model/profile/eligibility.dart'; import 'package:unit2/utils/request.dart'; import 'package:unit2/utils/urls.dart'; import 'package:http/http.dart' as http; class EligibilityService { static final EligibilityService _instance = EligibilityService(); static EligibilityService get instance => _instance; Future> getEligibilities( int profileId, String token) async { List eligibilities = []; String authToken = "Token $token"; String path = "${Url.instance.getEligibilities()}$profileId/"; Map headers = { 'Content-Type': 'application/json; charset=UTF-8', 'Authorization': authToken }; try { http.Response response = await Request.instance .getRequest(path: path, headers: headers, param: {}); if (response.statusCode == 200) { Map data = jsonDecode(response.body); if (data['data'] != null) { data['data'].forEach((var cert) { EligibityCert eligibility = EligibityCert.fromJson(cert); eligibilities.add(eligibility); }); } } } catch (e) { throw e.toString(); } return eligibilities; } Future delete( {required int eligibilityId, required int profileId, required String token}) async { bool? success; String authtoken = "Token $token"; String path = "${Url.instance.deleteEligibility()}$profileId/"; Map body = {"eligibility_id": eligibilityId}; 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!; } Future> add( {required EligibityCert eligibityCert, required String token, required int profileId}) async { Map? _response = {}; String authtoken = "Token $token"; String path = '${Url.instance.addEligibility()}$profileId/'; Map headers = { 'Content-Type': 'application/json; charset=UTF-8', 'Authorization': authtoken }; Map body = { 'eligibility_id': eligibityCert.eligibility!.id, 'license_number': eligibityCert.licenseNumber, 'exam_date': eligibityCert.examDate?.toString(), 'validity_date': eligibityCert.validityDate?.toString(), 'rating': eligibityCert.rating, '_citymunCode': eligibityCert.examAddress?.cityMunicipality?.code, '_countryId': eligibityCert.examAddress?.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; } else { _response.addAll({'success': false}); } return _response; } catch (e) { throw e.toString(); } } Future> update( {required EligibityCert eligibityCert, required String token, required int profileId, required int oldEligibility}) async { Map? response = {}; String authtoken = "Token $token"; String path = '${Url.instance.addEligibility()}$profileId/'; Map headers = { 'Content-Type': 'application/json; charset=UTF-8', 'Authorization': authtoken }; Map body = { 'eligibility_id': eligibityCert.eligibility!.id, 'license_number': eligibityCert.licenseNumber, 'exam_date': eligibityCert.examDate?.toString(), 'validity_date': eligibityCert.validityDate?.toString(), 'rating': eligibityCert.rating, '_citymunCode': eligibityCert.examAddress?.cityMunicipality?.code, '_countryId': eligibityCert.examAddress?.country!.id, '_oldEligibilityId': oldEligibility }; try { http.Response res = await Request.instance .putRequest(path: path, body: body, headers: headers, param: {}); if (res.statusCode == 200) { Map data = jsonDecode(res.body); response = data; } else { response.addAll({'success': false}); } return response; } catch (e) { throw e.toString(); } } Future deleteAttachment( {required Attachment attachment, required int moduleId, required String profileId, required String token}) async { bool? success; String authtoken = "Token $token"; String path = "${Url.instance.attachments()}$profileId/"; Map? body; try{ body = { "attachment_module": moduleId, attachment: [ { "id": attachment.id, "created_at": attachment.createdAt, "source": attachment.source, "filename": attachment.filename, "category": { "id": attachment.category?.id, "subclass": { "id": attachment.category?.subclass?.id, "name": attachment.category?.subclass?.name, "attachment_class": { "id": attachment.category?.subclass?.attachmentClass?.id, "name": attachment.category?.subclass?.attachmentClass?.name } }, "description": attachment.category?.description } } ] }; }catch(e){ print("body error"+e.toString()); } 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!; } }