/** * Created by stefania on 7/13/17. */ import { Injectable } from '@angular/core'; import { Http, Response, Headers, RequestOptions } from '@angular/http'; import { Observable } from 'rxjs/Rx'; import { Page } from "../domain/page"; import { PageHelpContent } from "../domain/page-help-content"; import { Community } from "../domain/community"; import { Entity } from "../domain/entity"; @Injectable() export class HelpContentService { constructor(private http:Http) { } private _helpContentUrl = process.env.API_ENDPOINT; static removeNulls(obj){ var isArray = obj instanceof Array; for (var k in obj){ if (obj[k]===null || obj[k]==='') isArray ? obj.splice(k,1) : delete obj[k]; else if (typeof obj[k]=="object") HelpContentService.removeNulls(obj[k]); } } getPages() { return this.http.get(this._helpContentUrl + 'page') .map(res => > res.json()) .catch(this.handleError); } getPage(pageId:string) { return this.http.get(this._helpContentUrl + 'page/'+pageId) .map(res => res.json()) .catch(this.handleError); } getCommunities() { return this.http.get(this._helpContentUrl + 'community') .map(res => > res.json()) .catch(this.handleError); } getCommunity(community_id: string) { return this.http.get(this._helpContentUrl + 'community/'+community_id) .map(res => res.json()) .catch(this.handleError); } getCommunitiesFull() { return this.http.get(this._helpContentUrl + 'communityFull') .map(res => > res.json()) .catch(this.handleError); } getCommunityFull(community_id: string) { return this.http.get(this._helpContentUrl + 'communityFull/'+community_id) .map(res => res.json()) .catch(this.handleError); } saveCommunity(community: Community) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); HelpContentService.removeNulls(community); return this.http.post(this._helpContentUrl + 'community/save', JSON.stringify(community), options) .map(res => res.json()) .catch(this.handleError); } updateCommunity(community: Community) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); HelpContentService.removeNulls(community); return this.http.post(this._helpContentUrl + 'community/update', JSON.stringify(community), options) .map(res => res.json()) .catch(this.handleError); } deleteCommunities(ids : string[]) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); return this.http.post(this._helpContentUrl + 'community/delete',JSON.stringify(ids), options) .catch(this.handleError); } getCommunityPages(community_id: string) { return this.http.get(this._helpContentUrl + 'community/'+community_id+'/pages') .map(res => > res.json()) .catch(this.handleError); } getCommunityPagesByType(community_id: string, params: string) { return this.http.get(this._helpContentUrl + 'community/'+community_id+'/pages'+params) .map(res => > res.json()) .catch(this.handleError); } getEntities() { return this.http.get(this._helpContentUrl + 'entity') .map(res => > res.json()) .catch(this.handleError); } getCommunityEntities(community_id: string) { return this.http.get(this._helpContentUrl + 'community/'+community_id+'/entities') .map(res => > res.json()) .catch(this.handleError); } saveEntity(entity: Entity) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); HelpContentService.removeNulls(entity); return this.http.post(this._helpContentUrl + 'entity/save', JSON.stringify(entity), options) .map(res => res.json()) .catch(this.handleError); } updateEntity(entity: Entity) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); HelpContentService.removeNulls(entity); return this.http.post(this._helpContentUrl + 'entity/update', JSON.stringify(entity), options) .map(res => res.json()) .catch(this.handleError); } // toggleEntity(selectedCommunityId: string, id : string,status : boolean) { // let headers = new Headers({'Content-Type': 'application/json'}); // let options = new RequestOptions({headers: headers}); // // return this.http.post(this._helpContentUrl + 'community/'+selectedCommunityId+'/entity/toggle?status='+ status.toString()+'&entityId='+id.toString(), options) // .catch(this.handleError); // } toggleEntities(selectedCommunityId: string, ids : string[],status : boolean) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); return this.http.post(this._helpContentUrl +'community/'+selectedCommunityId+ '/entity/toggle?status='+ status.toString(), JSON.stringify(ids), options) //.map( res => res.json()) .catch(this.handleError); } deleteEntities(ids : string[]) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); return this.http.post(this._helpContentUrl + 'entity/delete',JSON.stringify(ids), options) .catch(this.handleError); } toggleEntityOfPage(pageId: string, entityId : string,status : boolean) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); return this.http.post(this._helpContentUrl + 'page/'+pageId+'/entity/toggle?status='+ status.toString()+'&entityId='+entityId.toString(), options) .catch(this.handleError); } savePage(page: Page) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); HelpContentService.removeNulls(page); return this.http.post(this._helpContentUrl + 'page/save', JSON.stringify(page), options) .map(res => res.json()) .catch(this.handleError); } updatePage(page: Page) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); HelpContentService.removeNulls(page); return this.http.post(this._helpContentUrl + 'page/update', JSON.stringify(page), options) .map(res => res.json()) .catch(this.handleError); } togglePages(selectedCommunityId: string, ids : string[],status : boolean) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); return this.http.post(this._helpContentUrl + 'community/'+selectedCommunityId+'/page/toggle?status='+ status.toString(),JSON.stringify(ids), options) .catch(this.handleError); } deletePages(ids : string[]) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); return this.http.post(this._helpContentUrl + 'page/delete',JSON.stringify(ids), options) .catch(this.handleError); } getPageHelpContents() { return this.http.get(this._helpContentUrl + 'pagehelpcontent') .map(res => > res.json()) .catch(this.handleError); } getCommunityPageHelpContents(community_id: string) { return this.http.get(this._helpContentUrl + 'pagehelpcontent?communityId='+community_id) .map(res => > res.json()) .catch(this.handleError); } getPageHelpContent(id : string) { return this.http.get(this._helpContentUrl + 'pagehelpcontent/' + id) .map(res => res.json()) .catch(this.handleError); } savePageHelpContent(pageHelpContent: PageHelpContent) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); HelpContentService.removeNulls(pageHelpContent); return this.http.post(this._helpContentUrl + 'pagehelpcontent', JSON.stringify(pageHelpContent), options) .map(res => res.json()) .catch(this.handleError); } updatePageHelpContent(pageHelpContent: PageHelpContent) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); HelpContentService.removeNulls(pageHelpContent); return this.http.put(this._helpContentUrl + 'pagehelpcontent', JSON.stringify(pageHelpContent), options) .map(res => res.json()) .catch(this.handleError); } deletePageHelpContents(ids : string[]) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); return this.http.post(this._helpContentUrl + 'pagehelpcontent/delete',JSON.stringify(ids), options) .catch(this.handleError); } togglePageHelpContents(ids : string[],status : boolean) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); return this.http.post(this._helpContentUrl + 'pagehelpcontent/toggle?status='+ status.toString(), JSON.stringify(ids), options) //.map( res => res.json()) .catch(this.handleError); } private handleError(error: Response) { // in a real world app, we may send the error to some remote logging infrastructure // instead of just logging it to the console console.error(error); return Observable.throw(error.json().error || 'Server error'); } getDataProviders() { //return this.http.get('https://beta.services.openaire.eu/search/v2/api/datasources?format=json').map(res => res.json()).map(res => res.results).do(res => {console.log(res)}).catch(this.handleError); return this.http.get('https://beta.services.openaire.eu/search/v2/api/datasources?format=json').map(res => res.json()).map(res => res.results).do(res => {console.log(res)}).catch(this.handleError); } }