added catering service

This commit is contained in:
Gansner Pascal
2020-05-05 17:07:57 +02:00
parent 77fdcc3155
commit f112aa9b15
6 changed files with 219 additions and 0 deletions
+8
View File
@@ -22,5 +22,13 @@
"^/coronabusapi": ""
},
"changeOrigin": true
},
"/coronacateringapi": {
"target": "https://corona-api-dmz.psi.ch/catering.asmx",
"secure": false,
"pathRewrite": {
"^/coronacateringapi": ""
},
"changeOrigin": true
}
}
+17
View File
@@ -0,0 +1,17 @@
export enum Timeframe {
slot_11_00_11_45 = 0,
slot_11_45_12_30 = 1,
slot_12_30_13_15 = 2,
slot_13_15_13_45 = 3,
}
export enum Restaurant {
OASE = 0,
Timeout = 1,
}
export interface TimeframeSelection {
restaurant: Restaurant;
timeframe: Timeframe;
date: Date;
}
@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { CoronaCateringService } from './corona-catering.service';
describe('CoronaCateringService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: CoronaCateringService = TestBed.get(CoronaCateringService);
expect(service).toBeTruthy();
});
});
+180
View File
@@ -0,0 +1,180 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Timeframe, Restaurant, TimeframeSelection } from '../interfaces/catering-schedule';
import { Observable } from 'rxjs';
import { SoapApiHelper } from '../classes/api-helper';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class CoronaCateringService {
public static readonly STORAGE_NAME = 'catering-selection';
constructor(private http: HttpClient) { }
/**
* Sets a timeframe and restaurant selection for the specified date.
*
* @param restaruant restaurant
* @param timeframe timeframe of visit
* @param date date of visit
*/
public async selectTimeframeAsync(restaruant: Restaurant, timeframe: Timeframe, date: Date): Promise<void> {
date = new Date(date.toDateString()); // make sure hours, minutes and seconds are removed
return new Promise((resolve, reject) => {
if (this.isTimeframeSelected(date)) {
reject('You already selected a timeframe for this date.');
return;
}
this.addGuest(restaruant, timeframe, date)
.subscribe(
_ => {
this.saveSelection(restaruant, timeframe, date);
resolve();
},
error => reject(error)
);
});
}
/**
* Changes the timeframe and restaurant selection of a specific date.
*
* @param restaurant restaurant
* @param timeframe timeframe of visit
* @param date date of visit
*/
public async changeTimeframeAsync(restaurant: Restaurant, timeframe: Timeframe, date: Date): Promise<void> {
return new Promise((reolve, reject) => {
if (!this.isTimeframeSelected(date)) {
reject('Can not change selection. No timeframe is selected yet for this date.');
return;
}
let selection = this.getSelection(date);
this.removeGuest(selection.restaurant, selection.timeframe, selection.date)
.subscribe(
_ => {
this.addGuest(restaurant, timeframe, date)
.subscribe(
__ => this.saveSelection(restaurant, timeframe, date),
error => reject(error)
);
},
error => reject(error)
);
});
}
/**
* Loads all selections of the user.
*
*/
public getSelections(): TimeframeSelection[] {
return this.loadSelections();
}
/**
* Loads the selection of a specific date. Returns null if no timeframe is selected.
*
* @param date date of visit
*/
public getSelection(date: Date): TimeframeSelection {
date = new Date(date.toDateString());
return this.loadSelections().find(selection => selection.date.getTime() === date.getTime());
}
/**
* Saves a timeframe selection to the local storage.
*
* @param restaurant restaurant
* @param timeframe timeframe of visit
* @param date date of visit
*/
private saveSelection(restaurant: Restaurant, timeframe: Timeframe, date: Date): void {
const today = new Date(new Date().toDateString());
let selections = this.loadSelections();
selections = selections.filter(selection => selection.date.getTime() >= today.getTime());
let index = this.loadSelections().findIndex(selection => selection.date.getTime() === date.getTime());
if (index >= 0) {
selections[index] = { date, restaurant, timeframe };
} else {
selections.push({ date, restaurant, timeframe });
}
localStorage.setItem(CoronaCateringService.STORAGE_NAME, JSON.stringify(selections));
}
/**
* Loads all tiemframe selections done by the user.
*/
private loadSelections(): TimeframeSelection[] {
let storage = localStorage.getItem(CoronaCateringService.STORAGE_NAME);
if (!storage) {
return [];
}
let selections = <TimeframeSelection[]>JSON.parse(storage);
selections = selections.map(row => <TimeframeSelection>{
date: new Date(row.date),
timeframe: row.timeframe,
restaurant: row.restaurant,
});
return selections;
}
/**
* Checks if the user already selected a timeframe for the specified date.
*
* @param date date of visit
*/
private isTimeframeSelected(date: Date): boolean {
return this.loadSelections().findIndex(selection => selection.date.getTime() === date.getTime()) >= 0;
}
/**
* Increments the counter on the backend for the specified date, timeframe and restaurant.
*
* @param restaruant restaurant
* @param timeframe timeframe of visit
* @param date date of visit
*/
private addGuest(restaurant: Restaurant, timeframe: Timeframe, date: Date): Observable<void> {
return SoapApiHelper.call(this.http, environment.corona_catering_api + '/AddGuest', { restaurant, timeframe, date });
}
/**
* Decrements the counter on the backend for the specified date, timeframe and restaurant.
*
* @param restaruant restaurant
* @param timeframe timeframe of visit
* @param date date of visit
*/
private removeGuest(restaurant: Restaurant, timeframe: Timeframe, date: Date): Observable<void> {
return SoapApiHelper.call(this.http, environment.corona_catering_api + '/RemoveGuest', { restaurant, timeframe, date });
}
/**
* Loads the max visitors per timeframe for the OASE.
*/
public getMaxNbOase(): Observable<number> {
return SoapApiHelper.call<number>(this.http, environment.corona_catering_api + '/GetMaxNbOase', {});
}
/**
* Loads the max visitors per timeframe for the timeout.
*/
public getMaxNbTimeout(): Observable<number> {
return SoapApiHelper.call<number>(this.http, environment.corona_catering_api + '/GetMaxNbTimeout', {});
}
/**
* Loads the queue size of the specified timeframe.
*
* @param restaurant restaurant
* @param timeframe timeframe of visit
* @param date date of visit
*/
public getQueue(restaurant: Restaurant, timeframe: Timeframe, date: Date): Observable<number> {
return SoapApiHelper.call<number>(this.http, environment.corona_catering_api + '/GetQueue', { restaurant, timeframe, date });
}
}
+1
View File
@@ -20,4 +20,5 @@ export const environment = {
zptcalendar: 'http://spotscan.ch/CPTcalendar/',
// corona
corona_bus_api: 'https://corona-api-dmz.psi.ch/bus.asmx',
corona_catering_api: 'https://corona-api-dmz.psi.ch/catering.asmx',
};
+1
View File
@@ -24,6 +24,7 @@ export const environment = {
zptcalendar: 'http://spotscan.ch/CPTcalendar/',
// corona
corona_bus_api: 'coronabusapi',
corona_catering_api: 'coronacateringapi',
};
/*