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

154 lines
4.2 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
public leaderboard_fetch(data) {
return this.http.post(
2017-06-15 16:32:13 +01:00
this.apiUrl + '/stats/leaderboard',
{
session_key : this.sessionKey,
type : data
2017-06-15 16:32:13 +01:00
}
).map( response => response.json() );
}
// Fake Breadcrumb data
public breadcrumb_data(data) {
return Observable.of(
{
"customersthismonth" : 196,
"moneyspentthismonth" : 156.02,
"pointstotal" : 506,
"averagetransactiontoday" : 3.69
2017-07-04 17:19:26 +01:00
}
)
}
// Fake chart data to mimic
2017-07-04 17:19:26 +01:00
public graph_data(data) {
return Observable.of(
2017-06-22 12:40:36 +01:00
{
2017-07-05 18:08:23 +01:00
// graphstoshow is on server and changes every hour, listing what graphs to display
"graphstoshow" :
{
customersthisweekgraph : true,
customerslastweekgraph : true,
customerslastmonthgraph : true,
customerslastyeargraph : true,
percentofcustomerssectorgraph : true,
pointsthisweekgraph : true,
pointslastweekgraph : true,
},
"customersthisweek" :
{
day : ['Monday', 'tuesday','wednesday','thursday','friday','saturday','sunday'],
customerno : [1,2,3,4,5,6,7],
},
"customerslastweek" :
{
day : ['Monday', 'tuesday','wednesday','thursday','friday','saturday','sunday'],
customerno : [7,6,5,4,3,2,1],
},
// can take differing size arrays, so any month works. Example here is for April
"customerslastmonth" :
{
day : ['April 1','April 2','April 3','April 4','April 5','April 6','April 7','April 8',
'April 9','April 10','April 11','April 12','April 13','April 14','April 15','April 16',
'April 17','April 18','April 19','April 20','April 21','April 22','April 23','April 24',
'April 25','April 26','April 27','April 28','April 29','April 30'],
customerno : [7,6,5,4,3,2,1,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],
},
"customerslastyear" :
{
day : ['January','February','March','April','May','June','July','August','September','October','November','December'],
customerno : [7,6,5,4,3,2,1,8,9,10,11,12],
},
percentofcustomerssector : 76,
2017-06-22 12:40:36 +01:00
"pointsthisweek" :
{
day : ['Monday', 'tuesday','wednesday','thursday','friday','saturday','sunday'],
points : [1,2,3,4,5,6,7],
},
"pointslastweek" :
{
day : ['Monday', 'tuesday','wednesday','thursday','friday','saturday','sunday'],
points : [1,2,3,4,5,6,7],
}
}
)
}
2017-05-09 13:39:48 +01:00
}