Project

General

Profile

1
import { Injectable } from '@angular/core';
2
import { Http, Response, Headers, RequestOptions } from '@angular/http';
3
import { HttpClient } from '@angular/common/http';
4
import { HttpHeaders } from '@angular/common/http';
5
import { Observable } from 'rxjs';
6
import {COOKIE} from "../../login/utils/helper.class"
7
import { map } from "rxjs/operators";
8

    
9
@Injectable()
10
export class SubscribeService {
11

    
12
  constructor(private http:HttpClient) {
13
    }
14
     getCommunitySubscribers(pid:string, url:string){
15
       return this.http.get<any>(url+"/community/"+pid+"/subscribers");
16
                       //.map(res => <any> res.json());
17
     }
18

    
19
     isSubscribedToCommunity(pid:string, email:string, url:string){
20
       return this.http.get(url+"/community/"+pid+"/subscribers")
21
                       //.map(res =>  ((<any>res =="")?{}:<any> res.json()))
22

    
23
         .pipe(map(res => {
24
                         if(res['status'] && res['status'] != 200) {
25
                           return null;
26
                         }
27
                         if(res['subscribers'] && res['subscribers'] != null){
28

    
29
                           for(var i =0; i< res['subscribers'].length; i++ ){
30
                             if(res['subscribers'][i]!=null && res['subscribers'][i].email == email){
31
                               return true;
32
                             }
33
                           }
34
                         }
35
                         return false;
36

    
37
                       }));
38
     }
39
     subscribeToCommunity(pid:string, email:string, url:string){
40
       var subscriber = {"email":email};
41
       return this.http.post(url+"/community/"+pid+"/subscribers", JSON.stringify(subscriber), this.getAuthOptionsWithBody());
42
           //.map(res => <any> res.json());
43
     }
44
     unSubscribeToCommunity(pid:string, email:string, url:string){
45

    
46
       return this.http.post<any>(url+"/community/"+pid+"/subscribers/delete", JSON.stringify([email]), this.getAuthOptionsWithBody());
47
           //.map(res => <any> res.json());
48
     }
49
     getCommunitiesSubscribedTo(email:string, url:string){
50
      return this.http.get(url+"/subscriber/communities?email="+email);
51
                     //.map(res => <any> res.json());
52
     }
53

    
54
     /*
55
     public getAuthOptionsWithBody():RequestOptions{
56
     let headers = new Headers();
57
     headers.append('Content-Type', 'application/json');
58
     headers.append('X-XSRF-TOKEN',  COOKIE.getCookie(COOKIE.cookieName_id));
59
     let options = new RequestOptions({ headers: headers, withCredentials:true  });
60
       return options;
61
     }
62
    */
63

    
64
  public getAuthOptionsWithBody():any {
65

    
66
    const httpOptions = {
67
      headers: new HttpHeaders({
68
        'Content-Type': 'application/json',
69
        'X-XSRF-TOKEN': COOKIE.getCookie(COOKIE.cookieName_id)
70
      }), withCredentials: true
71
    };
72
  }
73
}
    (1-1/1)