openaire-library/utils/subscribe/subscribe.service.ts

56 lines
2.2 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import {COOKIE} from "../../login/utils/helper.class"
@Injectable()
export class SubscribeService {
constructor(private http:Http) {
}
getCommunitySubscribers(pid:string, url:string){
return this.http.get(url+"/community/"+pid+"/subscribers")
.map(res => <any> res.json()).do(res => {console.log(res)}).do(res => {console.log(res)});
}
isSubscribedToCommunity(pid:string, email:string, url:string){
return this.http.get(url+"/community/"+pid+"/subscribers")
.map(res => ((<any>res =="")?{}:<any> res.json()))
.map(res => {
if(res.subscribers && res.subscribers != null){
for(var i =0; i< res.subscribers.length; i++ ){
if(res.subscribers[i]!=null && res.subscribers[i].email == email){
return true;
}
}
}
return false;
}).do(res => {console.log("Response is "+res)});
}
subscribeToCommunity(pid:string, email:string, url:string){
var subscriber = {"email":email};
return this.http.post(url+"/community/"+pid+"/subscribers", JSON.stringify(subscriber), this.getAuthOptionsWithBody())
.map(res => <any> res.json())
.do(res => {console.log("Response is "+res)});
}
unSubscribeToCommunity(pid:string, email:string, url:string){
return this.http.post(url+"/community/"+pid+"/subscribers/delete", JSON.stringify([email]), this.getAuthOptionsWithBody())
.map(res => <any> res.json())
.do(res => {console.log("Response is "+res)});
}
public getAuthOptionsWithBody():RequestOptions{
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('X-XSRF-TOKEN', COOKIE.getCookie(COOKIE.cookieName_id));
let options = new RequestOptions({ headers: headers, withCredentials:true });
return options;
}
}