Ajout du cache sur les appels pour les stats

Et appel à l'API sur l'affichage de l'onglet
This commit is contained in:
Sébastien André
2021-05-02 12:50:47 +02:00
parent d020646831
commit 97e6153cef
15 changed files with 336 additions and 350 deletions

View File

@@ -5,7 +5,7 @@ import { Observable } from 'rxjs';
import { AircraftResp, AircraftReq } from '../models/aircraft';
import { BaseService } from './base.service';
import { RefData } from '../models/ref-data.enum';
import { CacheApiKey } from '../models/cache-api-key.enum';
@Injectable()
export class AircraftService extends BaseService {
@@ -22,7 +22,7 @@ export class AircraftService extends BaseService {
}
let callToApi = this.http.get<Array<AircraftResp>>(url, { headers: this.headers });
return this.serviceRefData.get<Array<AircraftResp>>(RefData.Aircraft, callToApi);
return this.serviceCacheApi.get<Array<AircraftResp>>(CacheApiKey.Aircraft, callToApi);
}
public AddAircraft(aircraftName: string, dataImg: string) {

View File

@@ -1,12 +1,12 @@
import { HttpHeaders } from '@angular/common/http';
import { ConfigurationHelper } from './configuration-helper';
import { ServiceRefData } from './service-ref-data.service';
import { ServiceCacheApi } from './service-cache-api.service';
export class BaseService {
protected headers: HttpHeaders;
protected apiUrl: string;
protected serviceRefData : ServiceRefData;
protected serviceCacheApi : ServiceCacheApi;
constructor() {
ConfigurationHelper.settings.subscribe(settings =>
@@ -21,6 +21,6 @@ export class BaseService {
}
});
this.serviceRefData = new ServiceRefData();
this.serviceCacheApi = new ServiceCacheApi();
}
}

View File

@@ -6,7 +6,7 @@ import { map } from "rxjs/operators";
import { DropZoneResp, DropZoneReq } from "../models/dropzone";
import { BaseService } from "./base.service";
import { RefData } from "../models/ref-data.enum";
import { CacheApiKey } from "../models/cache-api-key.enum";
@Injectable()
export class DropzoneService extends BaseService {
@@ -27,7 +27,7 @@ export class DropzoneService extends BaseService {
const details = response.map(data => new DropZoneResp(data));
return details;
}));
return this.serviceRefData.get<Array<DropZoneResp>>(RefData.Dropzone, callToApi);
return this.serviceCacheApi.get<Array<DropZoneResp>>(CacheApiKey.Dropzone, callToApi);
}
public SetFavoriteDropZone(selectedDz: DropZoneResp) {
@@ -37,7 +37,7 @@ export class DropzoneService extends BaseService {
selectedDz,
{ headers: this.headers })
.subscribe(() => {
this.serviceRefData.delete(RefData.Dropzone);
this.serviceCacheApi.delete(CacheApiKey.Dropzone);
});
}
@@ -48,7 +48,7 @@ export class DropzoneService extends BaseService {
selectedDz,
{ headers: this.headers })
.subscribe(() => {
this.serviceRefData.delete(RefData.Dropzone);
this.serviceCacheApi.delete(CacheApiKey.Dropzone);
});
}
@@ -72,7 +72,7 @@ export class DropzoneService extends BaseService {
isFavorite: isFavorite
};
this.serviceRefData.delete(RefData.Dropzone);
this.serviceCacheApi.delete(CacheApiKey.Dropzone);
return this.http.post(`${this.apiUrl}/DropZone`,
bodyNewDropZone,
{ headers: this.headers });

View File

@@ -5,7 +5,7 @@ import { Observable } from "rxjs";
import { GearResp, GearReq } from "../models/gear";
import { BaseService } from "./base.service";
import { RefData } from "../models/ref-data.enum";
import { CacheApiKey } from "../models/cache-api-key.enum";
@Injectable()
export class GearService extends BaseService {
@@ -15,7 +15,7 @@ export class GearService extends BaseService {
public getListOfGears(): Observable<Array<GearResp>> {
let callToApi = this.http.get<Array<GearResp>>(`${this.apiUrl}/Gear`, { headers: this.headers });
return this.serviceRefData.get<Array<GearResp>>(RefData.Gear, callToApi);
return this.serviceCacheApi.get<Array<GearResp>>(CacheApiKey.Gear, callToApi);
}
public AddGear(name: string,
@@ -37,7 +37,7 @@ export class GearService extends BaseService {
reserveCanopy: reserveCanopy
};
this.serviceRefData.delete(RefData.Gear);
this.serviceCacheApi.delete(CacheApiKey.Gear);
return this.http.post(`${this.apiUrl}/Gear`, bodyNewGear, { headers: this.headers});
}
}

View File

@@ -5,7 +5,7 @@ import { Observable } from "rxjs";
import { JumpTypeResp, JumpTypeReq } from "../models/jumpType";
import { BaseService } from "./base.service";
import { RefData } from "../models/ref-data.enum";
import { CacheApiKey } from "../models/cache-api-key.enum";
@Injectable()
export class JumpTypeService extends BaseService {
@@ -15,7 +15,7 @@ export class JumpTypeService extends BaseService {
public getListOfJumpTypes(): Observable<Array<JumpTypeResp>> {
let callToApi = this.http.get<Array<JumpTypeResp>>(`${this.apiUrl}/JumpType`, { headers: this.headers });
return this.serviceRefData.get<Array<JumpTypeResp>>(RefData.JumpType, callToApi);
return this.serviceCacheApi.get<Array<JumpTypeResp>>(CacheApiKey.JumpType, callToApi);
}
public AddJumpType(jumptypetName: string) {

View File

@@ -1,21 +1,20 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { tap } from 'rxjs/operators';
import { RefData } from '../models/ref-data.enum';
import { CacheApiKey } from '../models/cache-api-key.enum';
import { of } from 'rxjs';
//import 'rxjs/add/observable/of';
@Injectable({
providedIn: 'root',
})
export class ServiceRefData {
private cache: Map<RefData, Observable<any>>;
export class ServiceCacheApi {
private cache: Map<CacheApiKey, Observable<any>>;
constructor() {
this.cache = new Map<RefData, Observable<any>>();
this.cache = new Map<CacheApiKey, Observable<any>>();
}
public get<T>(key: RefData, callToApi: Observable<T>, withResetCache: boolean = false) : Observable<T> {
public get<T>(key: CacheApiKey, callToApi: Observable<T>, withResetCache: boolean = false) : Observable<T> {
if (withResetCache === true) {
this.cache.delete(key);
}
@@ -32,7 +31,7 @@ export class ServiceRefData {
}
}
public delete(key: RefData) {
public delete(key: CacheApiKey) {
this.cache.delete(key);
}
}

View File

@@ -1,21 +1,14 @@
import { Injectable, Injector } from '@angular/core';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import {
StatsResp,
StatsByDzResp,
StatsByAircraftResp,
StatsByJumpTypeResp,
StatsByGearResp,
StatsByYearResp,
StatsForLastMonthResp,
StatsForLastYearResp,
SimpleSummary
} from '../models/stats';
import { StatsByDzResp, StatsByAircraftResp, StatsByJumpTypeResp,
StatsByGearResp, StatsByYearResp, StatsForLastMonthResp,
StatsForLastYearResp, SimpleSummary } from '../models/stats';
import { BaseService } from './base.service';
import { CacheApiKey } from '../models/cache-api-key.enum';
@Injectable()
@@ -24,136 +17,117 @@ export class StatsService extends BaseService {
super();
}
public getStatsOfJumps(): StatsResp {
const resultats = new StatsResp();
resultats.simpleSummary = this.getSimpleSummary();
resultats.statsByDz = this.getStatsByDz();
resultats.statsByAircraft = this.getStatsByAircraft();
resultats.statsByJumpType = this.getStatsByJumpType();
resultats.statsByGear = this.getStatsByGear();
resultats.statsByYear = this.getStatsByYear();
resultats.statsForLastYear = this.getStatsOfLastYear();
resultats.statsForLastMonth = this.getStatsOfLastMonth();
return resultats;
public deleteAllCache() {
this.serviceCacheApi.delete(CacheApiKey.SimpleSummary);
this.serviceCacheApi.delete(CacheApiKey.StatsByDz);
this.serviceCacheApi.delete(CacheApiKey.StatsByAircraft);
this.serviceCacheApi.delete(CacheApiKey.StatsByJumpType);
this.serviceCacheApi.delete(CacheApiKey.StatsByGear);
this.serviceCacheApi.delete(CacheApiKey.StatsOfLastYear);
this.serviceCacheApi.delete(CacheApiKey.StatsOfLastMonth);
}
private getSimpleSummary(): Observable<SimpleSummary> {
return this.http
.get<Array<SimpleSummary>>(`${this.apiUrl}/Stats/Simple`, {
headers: this.headers
})
.pipe(
map(response => {
const stats = new SimpleSummary(response);
return stats;
})
);
public getSimpleSummary(): Observable<SimpleSummary> {
let callToApi = this.http.get<Array<SimpleSummary>>(`${this.apiUrl}/Stats/Simple`, { headers: this.headers })
.pipe(
map(response => {
const stats = new SimpleSummary(response);
return stats;
})
);
return this.serviceCacheApi.get<SimpleSummary>(CacheApiKey.SimpleSummary, callToApi);
}
private getStatsByDz(): Observable<Array<StatsByDzResp>> {
return this.http
.get<Array<StatsByDzResp>>(`${this.apiUrl}/Stats/ByDz`, {
headers: this.headers
})
.pipe(
map(response => {
const stats = response.map(data => new StatsByDzResp(data));
return stats;
})
);
public getStatsByDz(): Observable<Array<StatsByDzResp>> {
let callToApi = this.http.get<Array<StatsByDzResp>>(`${this.apiUrl}/Stats/ByDz`, { headers: this.headers })
.pipe(
map(response => {
const stats = response.map(data => new StatsByDzResp(data));
return stats;
})
);
return this.serviceCacheApi.get<Array<StatsByDzResp>>(CacheApiKey.StatsByDz, callToApi);
}
private getStatsByAircraft(): Observable<Array<StatsByAircraftResp>> {
return this.http
.get<Array<StatsByAircraftResp>>(
`${this.apiUrl}/Stats/ByAircraft`,
{ headers: this.headers }
)
.pipe(
map(response => {
const stats = response.map(data => new StatsByAircraftResp(data));
return stats;
})
);
public getStatsByAircraft(): Observable<Array<StatsByAircraftResp>> {
let callToApi = this.http.get<Array<StatsByAircraftResp>>(`${this.apiUrl}/Stats/ByAircraft`, { headers: this.headers })
.pipe(
map(response => {
const stats = response.map(data => new StatsByAircraftResp(data));
return stats;
})
);
return this.serviceCacheApi.get<Array<StatsByAircraftResp>>(CacheApiKey.StatsByAircraft, callToApi);
}
private getStatsByJumpType(): Observable<Array<StatsByJumpTypeResp>> {
return this.http
.get<Array<StatsByJumpTypeResp>>(
`${this.apiUrl}/Stats/ByJumpType`,
{ headers: this.headers }
)
.pipe(
map(response => {
const stats = response.map(data => new StatsByJumpTypeResp(data));
return stats;
})
);
public getStatsByJumpType(): Observable<Array<StatsByJumpTypeResp>> {
let callToApi = this.http.get<Array<StatsByJumpTypeResp>>(`${this.apiUrl}/Stats/ByJumpType`,{ headers: this.headers })
.pipe(
map(response => {
const stats = response.map(data => new StatsByJumpTypeResp(data));
return stats;
})
);
return this.serviceCacheApi.get<Array<StatsByJumpTypeResp>>(CacheApiKey.StatsByJumpType, callToApi);
}
private getStatsByGear(): Observable<Array<StatsByGearResp>> {
return this.http
.get<Array<StatsByGearResp>>(`${this.apiUrl}/Stats/ByGear`, {
headers: this.headers
})
.pipe(
map(response => {
const stats = response.map(data => new StatsByGearResp(data));
return stats;
})
);
public getStatsByGear(): Observable<Array<StatsByGearResp>> {
let callToApi = this.http.get<Array<StatsByGearResp>>(`${this.apiUrl}/Stats/ByGear`, { headers: this.headers })
.pipe(
map(response => {
const stats = response.map(data => new StatsByGearResp(data));
return stats;
})
);
return this.serviceCacheApi.get<Array<StatsByGearResp>>(CacheApiKey.StatsByGear, callToApi);
}
private getStatsByYear(): Observable<Array<StatsByYearResp>> {
return this.http
.get<Array<StatsByYearResp>>(`${this.apiUrl}/Stats/ByYear`, {
headers: this.headers
})
.pipe(
map(response => {
const stats = response.map(data => new StatsByYearResp(data));
return stats;
})
);
public getStatsByYear(): Observable<Array<StatsByYearResp>> {
let callToApi = this.http.get<Array<StatsByYearResp>>(`${this.apiUrl}/Stats/ByYear`, { headers: this.headers })
.pipe(
map(response => {
const stats = response.map(data => new StatsByYearResp(data));
return stats;
})
);
return this.serviceCacheApi.get<Array<StatsByYearResp>>(CacheApiKey.StatsByGear, callToApi);
}
private getStatsOfLastYear(): Observable<StatsForLastYearResp> {
return this.http
.get<StatsForLastYearResp>(
`${this.apiUrl}/Stats/ForLastYear`,
{ headers: this.headers }
)
.pipe(
map(response => {
const statsByDz = response.byDz.map(data => new StatsByDzResp(data));
const statsByJumpType = response.byJumpType.map(
data => new StatsByDzResp(data)
);
public getStatsOfLastYear(): Observable<StatsForLastYearResp> {
let callToApi = this.http.get<StatsForLastYearResp>(`${this.apiUrl}/Stats/ForLastYear`, { headers: this.headers })
.pipe(
map(response => {
const statsByDz = response.byDz.map(data => new StatsByDzResp(data));
const statsByJumpType = response.byJumpType.map(
data => new StatsByDzResp(data)
);
return new StatsForLastYearResp(statsByDz, statsByJumpType);
})
);
return new StatsForLastYearResp(statsByDz, statsByJumpType);
})
);
return this.serviceCacheApi.get<StatsForLastYearResp>(CacheApiKey.StatsOfLastYear, callToApi);
}
private getStatsOfLastMonth(): Observable<StatsForLastMonthResp> {
return this.http
.get<StatsForLastYearResp>(
`${this.apiUrl}/Stats/ForLastMonth`,
{ headers: this.headers }
)
.pipe(
map(response => {
const statsByDz = response.byDz.map(data => new StatsByDzResp(data));
const statsByJumpType = response.byJumpType.map(
data => new StatsByDzResp(data)
);
public getStatsOfLastMonth(): Observable<StatsForLastMonthResp> {
let callToApi = this.http.get<StatsForLastYearResp>(`${this.apiUrl}/Stats/ForLastMonth`, { headers: this.headers })
.pipe(
map(response => {
const statsByDz = response.byDz.map(data => new StatsByDzResp(data));
const statsByJumpType = response.byJumpType.map(
data => new StatsByDzResp(data)
);
return new StatsForLastMonthResp(statsByDz, statsByJumpType);
})
);
return new StatsForLastMonthResp(statsByDz, statsByJumpType);
})
);
return this.serviceCacheApi.get<StatsForLastMonthResp>(CacheApiKey.StatsOfLastMonth, callToApi);
}
}