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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 16:32:13 +01:00
|
|
|
// Login API
|
|
|
|
|
2017-06-05 18:47:34 +01:00
|
|
|
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() );
|
|
|
|
}
|
2017-06-15 16:32:13 +01:00
|
|
|
|
|
|
|
// Leaderboard Api
|
|
|
|
|
2017-06-15 16:58:06 +01:00
|
|
|
public leaderboard_fetch(data) {
|
|
|
|
this.http.post(
|
2017-06-15 16:32:13 +01:00
|
|
|
this.apiUrl + '/stats/leaderboard',
|
|
|
|
{
|
|
|
|
session_key : this.sessionKey,
|
2017-06-15 16:58:06 +01:00
|
|
|
type : data
|
2017-06-15 16:32:13 +01:00
|
|
|
}
|
|
|
|
).map( response => response.json() );
|
|
|
|
}
|
|
|
|
|
2017-05-09 13:39:48 +01:00
|
|
|
}
|