passo_mobile_app/lib/sevices/profile/profile_service.dart

120 lines
4.8 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/educational_background.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/model/profile/work_history.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;
PrimaryInformation primaryInformation;
List<WorkHistory> workExperiences =[];
List<PersonalReference> references = [];
List<Identification> identificationInformation = [];
List<ContactInfo> contactInformation = [];
List<EligibityCert> eligibilities = [];
List<Citizenship> citizenships = [];
List<LearningDevelopement> learningsDevelopments = [];
List<EducationalBackground> educationalBackgrounds = [];
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 = ContactInfo.fromJson(contact['contact_info']);
contactInformation.add(contactInfo);
});
// get all identifications
data['data']['basic_information']['identification_records']
.forEach((var identity) {
Identification 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);
});
//get all educational background
data['data']['education_background'].forEach((var education) {
EducationalBackground educationalBackground =
EducationalBackground.fromJson(education);
educationalBackgrounds.add(educationalBackground);
});
// get all work history
data['data']['work_experiences'].forEach((var work){
WorkHistory experience = WorkHistory.fromJson(work);
workExperiences.add(experience);
});
BasicInfo basicInfo = BasicInfo(
contactInformation: contactInformation,
primaryInformation: primaryInformation,
identifications: identificationInformation,
citizenships: citizenships);
ProfileInformation profileInformation = ProfileInformation(
workExperiences: workExperiences,
basicInfo: basicInfo,
eligibilities: eligibilities,
references: references,
learningsAndDevelopment: learningsDevelopments,
educationalBackgrounds: educationalBackgrounds);
profileInformation0 = profileInformation;
}
// }catch(e){
// throw(e.toString());
// }
return profileInformation0;
}
}