This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
FoodLoop-Web/src/app/providers/api-service.ts

72 lines
1.7 KiB
TypeScript
Raw Normal View History

2017-05-09 13:39:48 +01:00
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
/* this provider handles the interaction between server and client */
@Injectable()
export class ApiService {
private apiUrl = 'https://dev.app.peartrade.org/api';
2017-06-05 18:47:34 +01:00
private sessionKey: string = null;
2017-05-09 13:39:48 +01:00
constructor(
private http: Http,
2017-06-05 18:47:34 +01:00
) {
if (localStorage.getItem('sessionKey') ) {
this.sessionKey = localStorage.getItem('sessionKey');
}
}
private getSessionKey() {
console.log('get key');
return this.sessionKey;
}
private setSessionKey(key) {
console.log('set key');
this.sessionKey = key;
localStorage.setItem('sessionKey', this.sessionKey);
}
private removeSessionKey() {
console.log('remove key');
this.sessionKey = null;
localStorage.removeItem('sessionKey');
}
2017-05-09 13:39:48 +01:00
public register(data) {
return this.http.post(
this.apiUrl + '/register',
data
).map( response => response.json() );
}
public login(data) {
let login_event = this.http.post(
this.apiUrl + '/login',
data
).map( response => response.json() );
login_event.subscribe(
2017-06-05 18:47:34 +01:00
result => { this.setSessionKey(result.session_key) }
2017-05-09 13:39:48 +01:00
);
return login_event;
}
2017-06-05 18:47:34 +01:00
public logout() {
console.log(this.sessionKey);
return this.http.post(
this.apiUrl + '/logout',
{
session_key : this.sessionKey,
}
).map( response => { this.removeSessionKey(); return response.json() } );
}
2017-05-09 13:39:48 +01:00
public search(data) {
data.session_key = this.sessionKey;
return this.http.post(
this.apiUrl + '/search',
data
).map( response => response.json() );
}
}