Project

General

Profile

1
/**
2
 * Created by stefania on 4/26/17.
3
 */
4
import { Injectable } from '@angular/core';
5
import { Http, Response, Headers, RequestOptions } from '@angular/http';
6
import { Observable } from 'rxjs/Rx';
7
import { Topic } from './../domain/topic';
8
import { Question } from './../domain/question';
9
import { ActiveTopicQuestions } from './../domain/active-topic-questions';
10

    
11
@Injectable()
12
export class FAQService {
13

    
14
    constructor (private http: Http) {}
15

    
16
    private _faqsUrl = process.env.API_ENDPOINT;
17

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

    
26
    getTopics() {
27
        return this.http.get(this._faqsUrl + 'topic')
28
                .map(res => <Array<Topic>> res.json())
29
                .catch(this.handleError);
30
    }
31

    
32
    saveTopic(topic: Topic) {
33

    
34
        let headers = new Headers({'Content-Type': 'application/json'});
35
        let options = new RequestOptions({headers: headers});
36

    
37
        FAQService.removeNulls(topic);
38

    
39
        return this.http.post(this._faqsUrl + 'topic', JSON.stringify(topic), options)
40
            .map(res => <Topic> res.json())
41
            .catch(this.handleError);
42
    }
43

    
44
    updateTopic(topic: Topic) {
45
        let headers = new Headers({'Content-Type': 'application/json'});
46
        let options = new RequestOptions({headers: headers});
47

    
48
        FAQService.removeNulls(topic);
49

    
50
        return this.http.put(this._faqsUrl + 'topic', JSON.stringify(topic), options)
51
            .map(res => <Topic> res.json())
52
            .catch(this.handleError);
53
    }
54

    
55
    deleteTopics(ids : string[]) {
56
        let headers = new Headers({'Content-Type': 'application/json'});
57
        let options = new RequestOptions({headers: headers});
58

    
59
        return this.http.post(this._faqsUrl + 'topic/delete',JSON.stringify(ids), options)
60
            .catch(this.handleError);
61
    }
62

    
63
    orderTopic(ids: string[], order: string) {
64
        let headers = new Headers({'Content-Type': 'application/json'});
65
        let options = new RequestOptions({headers: headers});
66

    
67
        return this.http.post(this._faqsUrl + 'topic/toggle?order='+ order, JSON.stringify(ids), options)
68
            .map( res => <string[]> res.json())
69
            .catch(this.handleError);
70
    }
71

    
72
    getQuestions() {
73
        return this.http.get(this._faqsUrl + 'question')
74
            .map(res => <Array<Question>> res.json())
75
            .catch(this.handleError);
76
    }
77

    
78
    saveQuestion(question: Question) {
79

    
80
        let headers = new Headers({'Content-Type': 'application/json'});
81
        let options = new RequestOptions({headers: headers});
82

    
83
        FAQService.removeNulls(question);
84

    
85
        return this.http.post(this._faqsUrl + 'question', JSON.stringify(question), options)
86
            .map(res => <Question> res.json())
87
            .catch(this.handleError);
88
    }
89

    
90
    toggleQuestion(ids : string[],status : boolean) {
91
        let headers = new Headers({'Content-Type': 'application/json'});
92
        let options = new RequestOptions({headers: headers});
93

    
94
        return this.http.post(this._faqsUrl + 'question/toggle?status='+ status.toString(), JSON.stringify(ids), options)
95
            .map( res => <string[]> res.json())
96
            .catch(this.handleError);
97
    }
98

    
99
    deleteQuestion(id : string) {
100
        return this.http.delete(this._faqsUrl + 'question/' + id)
101
            .catch(this.handleError);
102
    }
103

    
104
    deleteQuestions(ids : string[]) {
105
        let headers = new Headers({'Content-Type': 'application/json'});
106
        let options = new RequestOptions({headers: headers});
107

    
108
        return this.http.post(this._faqsUrl + 'question/delete',JSON.stringify(ids), options)
109
            .catch(this.handleError);
110
    }
111

    
112
    // getResources() {
113
    //     return this.http.get(this._resourcesUrl)
114
    //         .map(res => <ResourcePage> res.json())
115
    //         .catch(this.handleError);
116
    // }
117
    //
118
    // getResource(resourceType: string, id: string) {
119
    //     return this.http.get(this._resourcesUrl + resourceType + "/" + id)
120
    //         .map(res => <Resource> res.json())
121
    //         .catch(this.handleError);
122
    // }
123

    
124
    private handleError(error: Response) {
125
        // in a real world app, we may send the error to some remote logging infrastructure
126
        // instead of just logging it to the console
127
        console.error(error);
128
        return Observable.throw(error.json().error || 'Server error');
129
    }
130

    
131

    
132
}
(1-1/4)