connect-admin/src/app/pages/htmlpagecontent/html-page-content.service.ts

48 lines
2.0 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import {COOKIE} from "../../openaireLibrary/login/utils/helper.class"
import { HtmlPageContent } from "../../domain/html-page-content";
import { CustomOptions } from '../../openaireLibrary/services/servicesUtils/customOptions.class';
@Injectable()
export class HtmlPageContentService {
constructor(private http:Http) {
}
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") HtmlPageContentService.removeNulls(obj[k]);
}
}
getHtmlPageContent(community : string, page: string, adminToolsAPIURL:string) {
return this.http.get(adminToolsAPIURL + 'htmlpagecontent?community=' + community + "&page="+page)
.map(res => <HtmlPageContent> res.json())
.catch(this.handleError);
}
updateHtmlPageContent(htmlPageContent: HtmlPageContent, adminToolsAPIURL:string) {
//let headers = new Headers({'Content-Type': 'application/json'});
//let options = new RequestOptions({headers: headers});
HtmlPageContentService.removeNulls(htmlPageContent);
return this.http.post(adminToolsAPIURL + 'htmlpagecontent/update', JSON.stringify(htmlPageContent), CustomOptions.getAuthOptionsWithBody())
//return this.http.post(adminToolsAPIURL + 'htmlpagecontent/update', JSON.stringify(htmlPageContent), options)
.map(res => <HtmlPageContent> 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');
}
}