passo_mobile_app/lib/sevices/profile/profile_service.dart

98 lines
3.9 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:unit2/model/login_data/employee_info/employee_info.dart';
import 'package:unit2/model/profile/basic_info.dart';
import 'package:unit2/model/profile/basic_information/citizenship.dart';
import 'package:unit2/model/profile/basic_information/contact_information.dart';
import 'package:unit2/model/profile/basic_information/identification_information.dart';
import 'package:unit2/model/profile/eligibility.dart';
import 'package:unit2/model/profile/learning_development.dart';
import 'package:unit2/model/profile/profileInfomation.dart';
import 'package:unit2/model/profile/references.dart';
import 'package:unit2/utils/request.dart';
import 'package:unit2/utils/urls.dart';
import '../../model/profile/basic_information/primary-information.dart';
class ProfileService {
static final ProfileService _instance = ProfileService();
static ProfileService get instance => _instance;
Future<ProfileInformation?> getProfile(String token, int id) async {
String url = Url.instance.profileInformation();
String path = url + id.toString();
ProfileInformation? profileInformation0;
List<PersonalReference> references = [];
ContactInfo contactInfo;
Identification identification;
List<Identification> identificationInformation =[];
List<ContactInfo> contactInformation = [];
PrimaryInformation primaryInformation;
List<EligibityCert> eligibilities = [];
List<Citizenship> citizenships = [];
List<LearningDevelopement> learningsDevelopments = [];
Map<String, String> headers = {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': "Token $token"
};
// try{
http.Response response = await Request.instance
.getRequest(path: path, param: {}, headers: headers);
if (response.statusCode == 200) {
Map data = jsonDecode(response.body);
// get primary information
primaryInformation = PrimaryInformation.fromJson(
data['data']['basic_information']['primary_information']);
// get all contacts
data['data']['basic_information']['contact_information']
.forEach((var contact) {
contactInfo = ContactInfo.fromJson(contact['contact_info']);
contactInformation.add(contactInfo);
});
// get all identifications
data['data']['basic_information']['identification_records'].forEach((var identity){
identification = Identification.fromJson(identity);
identificationInformation.add(identification);
});
//get all eligibilities
data['data']['eligibilities'].forEach((var cert){
EligibityCert eligibility = EligibityCert.fromJson(cert);
eligibilities.add(eligibility);
});
// get all citizenships
if(data['data']['citizenship']!= null){
data['data']['citizenships'].forEach((var citizenship){
Citizenship person = Citizenship.fromJson(citizenship);
citizenships.add(person);
});
}
// get all references;
data['data']['personal_references'].forEach((var person){
PersonalReference reference = PersonalReference.fromJson(person);
references.add(reference);
});
//get all learning and developments
data['data']['learning_development'].forEach((var training){
LearningDevelopement learnings = LearningDevelopement.fromJson(training);
learningsDevelopments.add(learnings);
});
BasicInfo basicInfo = BasicInfo(contactInformation: contactInformation,
primaryInformation: primaryInformation,
identifications: identificationInformation,
citizenships: citizenships
);
ProfileInformation profileInformation = ProfileInformation(basicInfo: basicInfo,eligibilities: eligibilities,references: references!,learningsAndDevelopment: learningsDevelopments);
profileInformation0 = profileInformation;
}
// }catch(e){
// throw(e.toString());
// }
return profileInformation0;
}
}