1
|
import { Injectable } from '@angular/core';
|
2
|
import { Http, Response, Headers, RequestOptions } from '@angular/http';
|
3
|
import { Observable } from 'rxjs/Rx';
|
4
|
|
5
|
import { CommunityInfo } from '../utils/communityInfo';
|
6
|
|
7
|
@Injectable()
|
8
|
export class CommunitiesService {
|
9
|
|
10
|
constructor(private http:Http) {
|
11
|
}
|
12
|
|
13
|
getResults(url: string) {
|
14
|
return this.http.get(url).map(res => <any> res.json()).map(res => this.parseResults(res));
|
15
|
}
|
16
|
|
17
|
parseResults(data: any): CommunityInfo[] {
|
18
|
let communities: CommunityInfo[] = [];
|
19
|
|
20
|
let length = Array.isArray(data) ? data.length :1;
|
21
|
|
22
|
for (let i=0; i<length; i++) {
|
23
|
let resData = Array.isArray(data) ? data[i] : data;
|
24
|
|
25
|
var result: CommunityInfo = new CommunityInfo();
|
26
|
|
27
|
if(Array.isArray(resData)) {
|
28
|
result['title'] = resData[0].name;
|
29
|
result['shortTitle'] = resData[0].shortName;
|
30
|
result['communityId'] = resData[0].id;
|
31
|
result['queryId'] = resData[0].queryId;
|
32
|
result['logoUrl'] = resData[0].logoUrl;
|
33
|
result['description'] = resData[0].description;
|
34
|
|
35
|
if(resData[0].managers != null) {
|
36
|
if(result['managers'] == undefined) {
|
37
|
result['managers'] = new Array<string>();
|
38
|
}
|
39
|
|
40
|
let managers = resData[0].managers;
|
41
|
let length = Array.isArray(managers) ? managers.length : 1;
|
42
|
|
43
|
for(let i=0; i<length; i++) {
|
44
|
let manager = Array.isArray(managers) ? managers[i] : managers;
|
45
|
result.managers[i] = manager;
|
46
|
}
|
47
|
}
|
48
|
|
49
|
if(resData[0].subjects != null) {
|
50
|
if(result['subjects'] == undefined) {
|
51
|
result['subjects'] = new Array<string>();
|
52
|
}
|
53
|
|
54
|
let subjects = resData[0].subjects;
|
55
|
let length = Array.isArray(subjects) ? subjects.length : 1;
|
56
|
|
57
|
for(let i=0; i<length; i++) {
|
58
|
let subject = Array.isArray(subjects) ? subjects[i] : subjects;
|
59
|
result.subjects[i] = subject;
|
60
|
}
|
61
|
}
|
62
|
|
63
|
} else {
|
64
|
result['title'] = resData.name;
|
65
|
result['shortTitle'] = resData.shortName;
|
66
|
result['communityId'] = resData.id;
|
67
|
result['queryId'] = resData.queryId;
|
68
|
result['logoUrl'] = resData.logoUrl;
|
69
|
result['description'] = resData.description;
|
70
|
|
71
|
if(resData.managers != null) {
|
72
|
if(result['managers'] == undefined) {
|
73
|
result['managers'] = new Array<string>();
|
74
|
}
|
75
|
|
76
|
let managers = resData.managers;
|
77
|
let length = Array.isArray(managers) ? managers.length : 1;
|
78
|
|
79
|
for(let i=0; i<length; i++) {
|
80
|
let manager = Array.isArray(managers) ? managers[i] : managers;
|
81
|
result.managers[i] = manager;
|
82
|
}
|
83
|
}
|
84
|
|
85
|
if(resData.subjects != null) {
|
86
|
if(result['subjects'] == undefined) {
|
87
|
result['subjects'] = new Array<string>();
|
88
|
}
|
89
|
|
90
|
let subjects = resData.subjects;
|
91
|
let length = Array.isArray(subjects) ? subjects.length : 1;
|
92
|
|
93
|
for(let i=0; i<length; i++) {
|
94
|
let subject = Array.isArray(subjects) ? subjects[i] : subjects;
|
95
|
result.subjects[i] = subject;
|
96
|
}
|
97
|
}
|
98
|
}
|
99
|
communities.push(result);
|
100
|
}
|
101
|
|
102
|
return communities;
|
103
|
}
|
104
|
|
105
|
}
|