connect/src/app/communities/communities.service.ts

91 lines
2.8 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { CommunityInfo } from '../utils/communityInfo';
@Injectable()
export class CommunitiesService {
constructor(private http:Http) {
}
getResults(url: string) {
return this.http.get(url).map(res => <any> res.json()).map(res => this.parseResults(res));
}
getCommunity(id:string , url: string) {
return this.http.get(url).map(res => <any> res.json()).map(res => this.parseCommunities(id,res));
}
parseResults(data: any): CommunityInfo[] {
let communities: CommunityInfo[] = [];
let length = Array.isArray(data) ? data.length :1;
for (let i=0; i<length; i++) {
let resData = Array.isArray(data) ? data[i] : data;
var result: CommunityInfo = new CommunityInfo();
if(Array.isArray(resData)) {
result['title'] = resData[0].name;
result['shortTitle'] = resData[0].shortName;
result['communityId'] = resData[0].id;
result['logoUrl'] = resData[0].logoUrl;
result['description'] = resData[0].description;
} else {
result['title'] = resData.name;
result['shortTitle'] = resData.shortName;
result['communityId'] = resData.id;
result['logoUrl'] = resData.logoUrl;
result['description'] = resData.description;
}
communities.push(result);
}
return communities;
}
parseCommunities(id:string, data:any): CommunityInfo[] {
let community: CommunityInfo[] = [];
let notFound:boolean = true;
let length = Array.isArray(data) ? data.length :1;
for (let i=0; i<length && notFound; i++) {
let resData = Array.isArray(data) ? data[i] : data;
var result: CommunityInfo = new CommunityInfo();
if(Array.isArray(resData) && id == resData[0].id) {
notFound = false;
result['title'] = resData[0].name;
result['shortTitle'] = resData[0].shortName;
result['communityId'] = resData[0].id;
result['logoUrl'] = resData[0].logoUrl;
result['description'] = resData[0].description;
} else if(!Array.isArray(resData) && id == resData.id) {
notFound = false;
result['title'] = resData.name;
result['shortTitle'] = resData.shortName;
result['communityId'] = resData.id;
result['logoUrl'] = resData.logoUrl;
result['description'] = resData.description;
}
community.push(result);
}
return community;
}
}