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