Project

General

Profile

1
import { Injectable } from '@angular/core';
2
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
3
import { Observable } from 'rxjs';
4
import {COOKIE} from "../../openaireLibrary/login/utils/helper.class"
5
import { HtmlPageContent } from "../../domain/html-page-content";
6
import { CustomOptions } from '../../openaireLibrary/services/servicesUtils/customOptions.class';
7
import {catchError} from "rxjs/operators";
8

    
9
@Injectable()
10
export class HtmlPageContentService {
11

    
12
    constructor(private http:HttpClient) {
13
    }
14

    
15
    static removeNulls(obj){
16
        var isArray = obj instanceof Array;
17
        for (var k in obj){
18
            if (obj[k]===null || obj[k]==='') isArray ? obj.splice(k,1) : delete obj[k];
19
            else if (typeof obj[k]=="object") HtmlPageContentService.removeNulls(obj[k]);
20
        }
21
    }
22

    
23
      getHtmlPageContent(community : string, page: string, adminToolsAPIURL:string) {
24
          return this.http.get<Array<HtmlPageContent>>(adminToolsAPIURL + 'htmlpagecontent?community=' + community + "&page="+page)
25
              //.map(res => <HtmlPageContent> res.json())
26
              .pipe(catchError(this.handleError));
27
      }
28

    
29
      updateHtmlPageContent(htmlPageContent: HtmlPageContent, adminToolsAPIURL:string) {
30
          //let headers = new Headers({'Content-Type': 'application/json'});
31
          //let options = new RequestOptions({headers: headers});
32

    
33
          HtmlPageContentService.removeNulls(htmlPageContent);
34

    
35
          return this.http.post<HtmlPageContent>(adminToolsAPIURL + 'htmlpagecontent/update', JSON.stringify(htmlPageContent), CustomOptions.getAuthOptionsWithBody())
36
          //return this.http.post(adminToolsAPIURL + 'htmlpagecontent/update', JSON.stringify(htmlPageContent), options)
37
              //.map(res => <HtmlPageContent> res.json())
38
              .pipe(catchError(this.handleError));
39
      }
40

    
41
      private handleError(error: HttpErrorResponse) {
42
          // in a real world app, we may send the error to some remote logging infrastructure
43
          // instead of just logging it to the console
44
          console.error(error);
45
          return Observable.throw(error.error || 'Server error');
46
      }
47

    
48
}
(7-7/7)