Move subscribe.service.ts to openaireLibrary

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@52702 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
sofia.baltzi 2018-07-06 10:33:16 +00:00
parent fa78a1e6f9
commit 456eb136e5
1 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,55 @@
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;
}
}