Project

General

Profile

« Previous | Next » 

Revision 51228

Added by Sofia Baltzi over 6 years ago

Add communites folder,Move communities.service to openaireLibrary and Change imports

View differences:

modules/uoa-services-library/trunk/ng-openaire-library/src/app/connect/communities/communities.service.ts
1
import {Injectable}                 from '@angular/core';
2
import {Http, Response}             from '@angular/http';
3
import {Headers, RequestOptions}    from '@angular/http';
4
import {Observable}                 from 'rxjs/Rx';
5

  
6
import {CommunityInfo}              from '../community/communityInfo';
7

  
8
@Injectable()
9
export class CommunitiesService {
10

  
11
    constructor(private http:Http) {
12
    }
13

  
14
    getCommunities(url: string) {
15
        return this.http.get(url).map(res => <any> res.json()).map(res => this.parseCommunities(res));
16
    }
17

  
18
    parseCommunities(data: any): CommunityInfo[] {
19
        let communities: CommunityInfo[] = [];
20

  
21
        let length = Array.isArray(data) ? data.length :1;
22

  
23
        for (let i=0; i<length; i++) {
24
            let resData = Array.isArray(data) ? data[i] : data;
25

  
26
            var result: CommunityInfo = new CommunityInfo();
27

  
28
            if(Array.isArray(resData)) {
29
                result['title'] = resData[0].name;
30
                result['shortTitle'] = resData[0].shortName;
31
                result['communityId'] = resData[0].id;
32
                result['queryId'] = resData[0].queryId;
33
                result['logoUrl'] = resData[0].logoUrl;
34
                result['description'] = resData[0].description;
35
                result['date'] = resData[0].creationDate;
36

  
37
                if(resData[0].managers != null) {
38
                    if(result['managers'] == undefined) {
39
                      result['managers'] = new Array<string>();
40
                    }
41

  
42
                    let managers = resData[0].managers;
43
                    let length = Array.isArray(managers) ? managers.length : 1;
44

  
45
                    for(let i=0; i<length; i++) {
46
                      let manager = Array.isArray(managers) ? managers[i] : managers;
47
                      result.managers[i] = manager;
48
                    }
49
                }
50

  
51
                if(resData[0].subjects != null) {
52
                    if(result['subjects'] == undefined) {
53
                      result['subjects'] = new Array<string>();
54
                    }
55

  
56
                    let subjects = resData[0].subjects;
57
                    let length = Array.isArray(subjects) ? subjects.length : 1;
58

  
59
                    for(let i=0; i<length; i++) {
60
                      let subject = Array.isArray(subjects) ? subjects[i] : subjects;
61
                      result.subjects[i] = subject;
62
                    }
63
                }
64

  
65
            } else {
66
                result['title'] = resData.name;
67
                result['shortTitle'] = resData.shortName;
68
                result['communityId'] = resData.id;
69
                result['queryId'] = resData.queryId;
70
                result['logoUrl'] = resData.logoUrl;
71
                result['description'] = resData.description;
72
                result['date'] = resData.creationDate;
73

  
74
                if(resData.managers != null) {
75
                    if(result['managers'] == undefined) {
76
                      result['managers'] = new Array<string>();
77
                    }
78

  
79
                    let managers = resData.managers;
80
                    let length = Array.isArray(managers) ? managers.length : 1;
81

  
82
                    for(let i=0; i<length; i++) {
83
                      let manager = Array.isArray(managers) ? managers[i] : managers;
84
                      result.managers[i] = manager;
85
                    }
86
                }
87

  
88
                if(resData.subjects != null) {
89
                    if(result['subjects'] == undefined) {
90
                      result['subjects'] = new Array<string>();
91
                    }
92

  
93
                    let subjects = resData.subjects;
94
                    let length = Array.isArray(subjects) ? subjects.length : 1;
95

  
96
                    for(let i=0; i<length; i++) {
97
                      let subject = Array.isArray(subjects) ? subjects[i] : subjects;
98
                      result.subjects[i] = subject;
99
                    }
100
                }
101
            }
102
            communities.push(result);
103
        }
104

  
105
        return communities;
106
    }
107
}

Also available in: Unified diff