Correction sur la mise en cache des

données référentiel et leurs utilisation
sur la liste des sauts.
This commit is contained in:
Sébastien André
2021-05-28 17:19:12 +02:00
parent dba69b938e
commit ba99a0f047
14 changed files with 143 additions and 79 deletions

View File

@@ -40,8 +40,12 @@ export class AircraftService extends BaseService {
public getById(id: number) : Observable<AircraftResp> {
return this.serviceCacheApi.getByKey<Array<AircraftResp>>(CacheApiKey.Aircraft)
.pipe(map(data => {
return data.find(f => f.id === id);
}));
.pipe(map(data => {
return data.find(f => f.id === id);
}));
}
public getFromCache(): Observable<Array<AircraftResp>> {
return this.serviceCacheApi.getByKey<Array<AircraftResp>>(CacheApiKey.Aircraft);
}
}

View File

@@ -22,10 +22,6 @@ export class AuthenticationService extends BaseService {
this.currentUser = this.currentUserSubject.asObservable();
}
public get currentUserValue(): User {
return this.currentUserSubject.value;
}
public login(username: string, password: string) {
const bodyLogin = {
login: username,
@@ -61,6 +57,24 @@ export class AuthenticationService extends BaseService {
}));
}
public alwaysLogin() {
this.http.get(`${this.apiUrl}/User/AlwaysLogin`,
{ headers: this.headers })
.subscribe();
}
public logout() {
localStorage.removeItem("currentUser");
this.currentUserSubject.next(null);
}
public get currentUserValue(): User {
return this.currentUserSubject.value;
}
public set currentUserValue(value: User) {
this.currentUserSubject.next(value);
}
private pushToken(login: string, password: string, user: User){
if (user && user.token) {
user.authdata = window.btoa(login + ":" + password);
@@ -72,15 +86,4 @@ export class AuthenticationService extends BaseService {
this.currentUserSubject.next(user);
}
}
public alwaysLogin() {
this.http.get(`${this.apiUrl}/User/AlwaysLogin`,
{ headers: this.headers })
.subscribe();
}
public logout() {
localStorage.removeItem("currentUser");
this.currentUserSubject.next(null);
}
}

View File

@@ -80,8 +80,12 @@ export class DropzoneService extends BaseService {
public getById(id: number) : Observable<DropZoneResp> {
return this.serviceCacheApi.getByKey<Array<DropZoneResp>>(CacheApiKey.Dropzone)
.pipe(map(data => {
return data.find(f => f.id === id);
}));
.pipe(map(data => {
return data.find(f => f.id === id);
}));
}
public getFromCache(): Observable<Array<DropZoneResp>> {
return this.serviceCacheApi.getByKey<Array<DropZoneResp>>(CacheApiKey.Dropzone);
}
}

View File

@@ -44,8 +44,12 @@ export class GearService extends BaseService {
public getById(id: number) : Observable<GearResp> {
return this.serviceCacheApi.getByKey<Array<GearResp>>(CacheApiKey.Gear)
.pipe(map(data => {
return data.find(f => f.id === id);
}));
.pipe(map(data => {
return data.find(f => f.id === id);
}));
}
public getFromCache(): Observable<Array<GearResp>> {
return this.serviceCacheApi.getByKey<Array<GearResp>>(CacheApiKey.Gear);
}
}

View File

@@ -32,8 +32,12 @@ export class JumpTypeService extends BaseService {
public getById(id: number) : Observable<JumpTypeResp> {
return this.serviceCacheApi.getByKey<Array<JumpTypeResp>>(CacheApiKey.JumpType)
.pipe(map(data => {
return data.find(f => f.id === id);
}));
.pipe(map(data => {
return data.find(f => f.id === id);
}));
}
public getFromCache(): Observable<Array<JumpTypeResp>> {
return this.serviceCacheApi.getByKey<Array<JumpTypeResp>>(CacheApiKey.JumpType);
}
}

View File

@@ -5,6 +5,10 @@ import { Observable } from "rxjs";
import { map } from "rxjs/operators";
import { JumpResp, JumpReq, Jump } from "../models/jump";
import { GearResp } from "../models/gear";
import { DropZoneResp } from "../models/dropzone";
import { AircraftResp } from "../models/aircraft";
import { JumpTypeResp } from "../models/jumpType";
import { DateService } from "./date.service";
import { BaseService } from "./base.service";
@@ -29,16 +33,7 @@ export class JumpService extends BaseService {
return this.http.get<Array<JumpResp>>(`${this.apiUrl}/Jump`,
{ headers: this.headers })
.pipe(map((response) => {
let details = response.map((data) =>
{
let tmp = new Jump(data);
this.dropzoneService.getById(data.dropZoneId).subscribe((d)=> tmp.dropZone = d );
this.aircraftService.getById(data.aircraftId).subscribe((d)=> tmp.aircraft = d );
this.jumpTypeService.getById(data.jumpTypeId).subscribe((d)=> tmp.jumpType = d );
this.gearService.getById(data.gearId).subscribe((d)=> tmp.gear = d );
return tmp;
});
return details;
return this.MapWithDataInCache(response);
}));
}
@@ -145,4 +140,27 @@ export class JumpService extends BaseService {
.subscribe();
}
}
private MapWithDataInCache(apiResp: Array<JumpResp>) : Array<Jump> {
let allDropzones: Array<DropZoneResp>;
this.dropzoneService.getFromCache().subscribe(data => { allDropzones = data; });
let allAircrafts: Array<AircraftResp>;
this.aircraftService.getFromCache().subscribe(data => { allAircrafts = data; });
let allJumpType: Array<JumpTypeResp>;
this.jumpTypeService.getFromCache().subscribe(data => { allJumpType = data; });
let allGears: Array<GearResp>;
this.gearService.getFromCache().subscribe(data => { allGears = data; });
return apiResp.map((data) =>
{
let tmp = new Jump(data);
tmp.dropZone = allDropzones.find(d => d.id == data.dropZoneId);
tmp.aircraft = allAircrafts.find(d => d.id == data.aircraftId);
tmp.jumpType = allJumpType.find(d => d.id == data.jumpTypeId);
tmp.gear = allGears.find(d => d.id == data.gearId);
return tmp;
});
}
}

View File

@@ -15,6 +15,7 @@ export class ServiceCacheApi {
}
public get<T>(key: CacheApiKey, callToApi: Observable<T>) : Observable<T> {
console.log(`Get cache : ${CacheApiKey[key]}`);
const cached = this.cache.get(key);
if (cached) {
@@ -28,10 +29,12 @@ export class ServiceCacheApi {
}
public delete(key: CacheApiKey) {
console.log(`Delete cache : ${CacheApiKey[key]}`);
this.cache.delete(key);
}
public getByKey<T>(key: CacheApiKey) : Observable<T> {
console.log(`Get cache by key : ${CacheApiKey[key]}`);
return this.cache.get(key);
}
}