helper-data-service.ts 4.97 KB
Newer Older
1 2 3 4 5
import {Http, Headers} from '@angular/http';
import {Injectable} from '@angular/core';
import {Observable} from "rxjs/Observable";
import 'rxjs/add/operator/map';
import { ICourseField } from '../store/coursefields/coursefields.types';
6
import { ISectorField } from '../store/sectorfields/sectorfields.types';
7
import { IRegion, IRegions, IRegionSchool } from '../store/regionschools/regionschools.types';
8
import { ISector, ISectors, ISectorCourse } from '../store/sectorcourses/sectorcourses.types';
9
import { AppSettings } from '../app.settings';
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

const HEADER = { headers: new Headers({ 'Content-Type': 'application/json' }) };

@Injectable()
export class HelperDataService {
    constructor(private http: Http) {
    };
    getCourseFields() {
        return new Promise((resolve, reject) => {
            this.http.get(`${AppSettings.API_ENDPOINT}/coursefields/list`)
            .map(response => <ICourseField[]>response.json())
            .subscribe(data => {
                resolve(data);
            }, // put the data returned from the server in our variable
            error => {
                console.log("Error HTTP GET Service"); // in case of failure show this message
                reject("Error HTTP GET Service");
            },
28
            () => console.log("Course Fields Received"));//run this code in all cases); */
29 30
        });
    };
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    getSectorFields() {
        return new Promise((resolve, reject) => {
            this.http.get(`${AppSettings.API_ENDPOINT}/sectorfields/list`)
            .map(response => <ISectorField[]>response.json())
            .subscribe(data => {
                resolve(data);
            }, // put the data returned from the server in our variable
            error => {
                console.log("Error HTTP GET Service"); // in case of failure show this message
                reject("Error HTTP GET Service");
            },
            () => console.log("Sector Fields Received"));//run this code in all cases); */
        });
    };

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    getRegionsWithSchools() {
        return new Promise((resolve, reject) => {
            this.http.get(`${AppSettings.API_ENDPOINT}/regions/list`)
            .map(response => response.json())
            .subscribe(data => {
//                console.log(data);
                resolve(this.transformRegionSchoolsSchema(data));
            }, // put the data returned from the server in our variable
            error => {
                console.log("Error HTTP GET Service"); // in case of failure show this message
                reject("Error HTTP GET Service");
            },
            () => console.log("region schools service"));//run this code in all cases); */
        });
    };

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    getSectorsWithCourses() {
        return new Promise((resolve, reject) => {
            this.http.get(`${AppSettings.API_ENDPOINT}/coursesectorfields/list`)
            .map(response => response.json())
            .subscribe(data => {
//                console.log(data);
                resolve(this.transformSectorCoursesSchema(data));
            }, // put the data returned from the server in our variable
            error => {
                console.log("Error HTTP GET Service"); // in case of failure show this message
                reject("Error HTTP GET Service");
            },
            () => console.log("region schools service"));//run this code in all cases); */
        });
    };

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    transformRegionSchoolsSchema(regionSchools: any) {
        let rsa = Array<IRegion>();
        let trackRegionId: string;
        let trackIndex: number;

        trackRegionId = "";
        trackIndex = -1;

        let j=0;
        regionSchools.forEach(regionSchool => {
            if (trackRegionId !== regionSchool.region_id) {
                trackIndex++;
                rsa.push(<IRegion>{'region_id': regionSchool.region_id, 'region_name': regionSchool.region_name, 'epals': Array<IRegionSchool>()});
                trackRegionId = regionSchool.region_id;
            }
            rsa[trackIndex].epals.push(<IRegionSchool>{'epal_id': regionSchool.epal_id, 'epal_name': regionSchool.epal_name, 'globalIndex': j, 'selected': false});
            j++;
        });
        return rsa;
    }

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    transformSectorCoursesSchema(sectorCourses: any) {
        let rsa = Array<ISector>();
        let trackSectorId: string;
        let trackIndex: number;

        trackSectorId = "";
        trackIndex = -1;

        let j=0;
        sectorCourses.forEach(sectorCourse => {
            if (trackSectorId !== sectorCourse.sector_id) {
                trackIndex++;
                rsa.push(<ISector>{'sector_id': sectorCourse.sector_id, 'sector_name': sectorCourse.sector_name, 'sector_selected': sectorCourse.sector_selected, 'courses': Array<ISectorCourse>()});
                trackSectorId = sectorCourse.sector_id;
            }
            rsa[trackIndex].courses.push(<ISectorCourse>{'course_id': sectorCourse.course_id, 'course_name': sectorCourse.course_name, 'globalIndex': j, 'selected': false});
            j++;
        });
        return rsa;
    }

121
}