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

157 lines
3.5 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');
}
}
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
private leaderboard_daily_total() {
return this.http.post(
this.apiUrl + '/stats/leaderboard',
{
session_key : this.sessionKey,
type : 'daily_total'
}
).map( response => response.json() );
}
private leaderboard_daily_count() {
return this.http.post(
this.apiUrl + '/stats/leaderboard',
{
session_key : this.sessionKey,
type : 'daily_count'
}
).map( response => response.json() );
}
private leaderboard_weekly_total() {
return this.http.post(
this.apiUrl + '/stats/leaderboard',
{
session_key : this.sessionKey,
type : 'weekly_total'
}
).map( response => response.json() );
}
private leaderboard_weekly_count() {
return this.http.post(
this.apiUrl + '/stats/leaderboard',
{
session_key : this.sessionKey,
type : 'weekly_count'
}
).map( response => response.json() );
}
private leaderboard_monthly_total() {
return this.http.post(
this.apiUrl + '/stats/leaderboard',
{
session_key : this.sessionKey,
type : 'monthly_total'
}
).map( response => response.json() );
}
private leaderboard_monthly_count() {
return this.http.post(
this.apiUrl + '/stats/leaderboard',
{
session_key : this.sessionKey,
type : 'monthly_count'
}
).map( response => response.json() );
}
private leaderboard_all_time_total() {
return this.http.post(
this.apiUrl + '/stats/leaderboard',
{
session_key : this.sessionKey,
type : 'all_time_total'
}
).map( response => response.json() );
}
private leaderboard_all_time_count() {
return this.http.post(
this.apiUrl + '/stats/leaderboard',
{
session_key : this.sessionKey,
type : 'all_time_count'
}
).map( response => response.json() );
}
2017-05-09 13:39:48 +01:00
}