New service and model for the tunnel flights

This commit is contained in:
Sébastien ANDRE
2023-05-05 17:59:11 +02:00
parent 487b287924
commit e41e7a1fe4
6 changed files with 163 additions and 53 deletions

View File

@@ -38,6 +38,7 @@ import { AuthGuardService } from "../services/auth-guard.service";
import { ImageService } from "../services/image.service"; import { ImageService } from "../services/image.service";
import { ConfigurationHelper } from "../services/configuration-helper"; import { ConfigurationHelper } from "../services/configuration-helper";
import { ServiceCacheApi } from "../services/service-cache-api.service"; import { ServiceCacheApi } from "../services/service-cache-api.service";
import { TunnelService } from "../services/tunnel.service";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { FormsModule, ReactiveFormsModule } from "@angular/forms"; import { FormsModule, ReactiveFormsModule } from "@angular/forms";
@@ -193,6 +194,7 @@ export function initConfig(configService: ConfigurationHelper) {
], ],
exports: [HttpClientModule], exports: [HttpClientModule],
providers: [ providers: [
TunnelService,
ImageService, ImageService,
AircraftService, AircraftService,
DropzoneService, DropzoneService,

View File

@@ -3,16 +3,38 @@ import { formatDate } from '@angular/common';
import { DateAdapter, MAT_DATE_FORMATS, NativeDateAdapter } from "@angular/material/core"; import { DateAdapter, MAT_DATE_FORMATS, NativeDateAdapter } from "@angular/material/core";
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { TunnelResp } from "../../models/tunnel";
import { DateService } from 'src/services/date.service'; import { DateService } from 'src/services/date.service';
import { TunnelService } from 'src/services/tunnel.service';
import { ServiceComm } from 'src/services/service-comm.service'; import { ServiceComm } from 'src/services/service-comm.service';
import { StatsService } from 'src/services/stats.service'; import { StatsService } from 'src/services/stats.service';
export const PICK_FORMATS = {
parse: { dateInput: 'yy MM dd' },
display: {
dateInput: 'yyyy-MM-dd',
monthYearLabel: 'yyyy MMM',
dateA11yLabel: 'yyyy MM dd',
monthYearA11yLabel: 'yyyy MMMM',
}
};
class PickDateAdapter extends NativeDateAdapter {
format(date: Date, displayFormat: Object): string {
return formatDate(date, displayFormat.toString(), "en");
}
}
@Component({ @Component({
selector: 'app-new-tunnel-flight', selector: 'app-new-tunnel-flight',
templateUrl: './new-tunnel-flight.component.html', templateUrl: './new-tunnel-flight.component.html',
styleUrls: ['./new-tunnel-flight.component.css'] styleUrls: ['./new-tunnel-flight.component.css'],
providers: [
{ provide: DateAdapter, useClass: PickDateAdapter },
{ provide: MAT_DATE_FORMATS, useValue: PICK_FORMATS }
]
}) })
export class NewTunnelFlightComponent { export class NewTunnelFlightComponent implements OnInit {
public beginDate: Date; public beginDate: Date;
public endDate: Date; public endDate: Date;
public minutesOfFlight: number; public minutesOfFlight: number;
@@ -24,19 +46,19 @@ export class NewTunnelFlightComponent {
private pendingAddRequest: boolean; private pendingAddRequest: boolean;
constructor(private serviceComm: ServiceComm, constructor(private serviceComm: ServiceComm,
private serviceTunnel: TunnelService, private serviceTunnel: TunnelService,
private dateService: DateService, private dateService: DateService,
private translateService: TranslateService, private translateService: TranslateService,
private statsService : StatsService) {} private statsService: StatsService) { }
ngOnInit() { ngOnInit() {
this.serviceComm.forceTranslateTitle.subscribe((data)=> { this.serviceComm.forceTranslateTitle.subscribe((data) => {
if (data === true){ if (data === true) {
this.updateTitle(); this.updateTitle();
} }
}); });
this.updateTitle(); this.updateTitle();
this.pendingAddRequest = false; this.pendingAddRequest = false;
this.initForm(); this.initForm();
this.getListOfTunnels(); this.getListOfTunnels();
@@ -45,7 +67,20 @@ export class NewTunnelFlightComponent {
onFormSubmit() { onFormSubmit() {
this.pendingAddRequest = true; this.pendingAddRequest = true;
this.statsService.resetStats(); this.serviceTunnel.AddFlight(this.selectedTunnel.id,
this.beginDate,
this.minutesOfFlight,
this.comments)
.subscribe(() => {
this.statsService.resetStats();
this.comments = undefined;
if (this.resetForm === true) {
this.initForm();
}
this.pendingAddRequest = false;
});
} }
public isValidatedForm(): boolean { public isValidatedForm(): boolean {
@@ -56,7 +91,7 @@ export class NewTunnelFlightComponent {
} }
private getListOfTunnels() { private getListOfTunnels() {
this.serviceTunnel.getListOfTunnels(true).subscribe((data) => { this.serviceTunnel.getListOfTunnels().subscribe((data) => {
data.sort((a, b) => a.name.localeCompare(b.name)); data.sort((a, b) => a.name.localeCompare(b.name));
this.listOfTunnel = data; this.listOfTunnel = data;
this.countDatasLoaded++; this.countDatasLoaded++;

View File

@@ -10,5 +10,6 @@ export enum CacheApiKey {
StatsByGear, StatsByGear,
StatsOfLastYear, StatsOfLastYear,
StatsOfLastMonth, StatsOfLastMonth,
StatsByYear StatsByYear,
Tunnel
} }

View File

@@ -0,0 +1,25 @@
export class TunnelReq {
constructor(data: any) {
Object.assign(this, data);
}
public id: number;
public comment: string;
public tunnelId: number;
public nbMinutes: number;
public flightDate: string;
}
export class TunnelResp {
constructor(data: any) {
Object.assign(this, data);
}
public id: number;
public latitude: string;
public longitude: string;
public name: string;
public address: string;
public website: string;
public email: string;
}

View File

@@ -20,37 +20,37 @@ import { GearService } from "./gear.service";
@Injectable() @Injectable()
export class JumpService extends BaseService { export class JumpService extends BaseService {
private callsToAdd : Array<Observable<any>>; private callsToAdd: Array<Observable<any>>;
constructor(private http: HttpClient, constructor(private http: HttpClient,
private dateService: DateService, private dateService: DateService,
private datePipe: DatePipe, private datePipe: DatePipe,
private dropzoneService: DropzoneService, private dropzoneService: DropzoneService,
private aircraftService: AircraftService, private aircraftService: AircraftService,
private jumpTypeService: JumpTypeService, private jumpTypeService: JumpTypeService,
private gearService: GearService) { private gearService: GearService) {
super(); super();
} }
public GetListOfJumps(): Observable<Array<Jump>> { public GetListOfJumps(): Observable<Array<Jump>> {
return this.http.get<Array<JumpResp>>(`${this.apiUrl}/Jump`, return this.http.get<Array<JumpResp>>(`${this.apiUrl}/Jump`,
{ headers: this.headers }) { headers: this.headers })
.pipe(map((response) => { .pipe(map((response) => {
return this.MapWithDataInCache(response); return this.MapWithDataInCache(response);
})); }));
} }
public GetJumps(beginIndex: number, endIndex: number): Observable<JumpList> { public GetJumps(beginIndex: number, endIndex: number): Observable<JumpList> {
return this.http.get<JumpListResp>(`${this.apiUrl}/Jump/${beginIndex}/${endIndex}`, return this.http.get<JumpListResp>(`${this.apiUrl}/Jump/${beginIndex}/${endIndex}`,
{ headers: this.headers }) { headers: this.headers })
.pipe(map((response) => { .pipe(map((response) => {
let result: JumpList = { let result: JumpList = {
rows : this.MapWithDataInCache(response.rows), rows: this.MapWithDataInCache(response.rows),
count : response.count count: response.count
}; };
return new JumpList(result); return new JumpList(result);
})); }));
} }
public AddListOfJump(selectedJumpType: number, public AddListOfJump(selectedJumpType: number,
@@ -64,8 +64,7 @@ export class JumpService extends BaseService {
defaultDeployAltitude: number, defaultDeployAltitude: number,
countOfJumps: number, countOfJumps: number,
notes: string, notes: string,
isSpecial: boolean): Observable<any[]> isSpecial: boolean): Observable<any[]> {
{
this.callsToAdd = new Array<Observable<any>>(); this.callsToAdd = new Array<Observable<any>>();
const diffInDays = this.dateService.DiffBetweenDates(beginDate, endDate) + 1; const diffInDays = this.dateService.DiffBetweenDates(beginDate, endDate) + 1;
const countOfJumpsPerDay = Math.trunc(countOfJumps / diffInDays); const countOfJumpsPerDay = Math.trunc(countOfJumps / diffInDays);
@@ -105,25 +104,25 @@ export class JumpService extends BaseService {
public DeleteJump(item: Jump) { public DeleteJump(item: Jump) {
this.http.delete(`${this.apiUrl}/Jump/${item.id}`, this.http.delete(`${this.apiUrl}/Jump/${item.id}`,
{ headers: this.headers, }) { headers: this.headers, })
.subscribe(); .subscribe();
} }
public UpdateJump(id: number, public UpdateJump(id: number,
isSpecial: boolean, isSpecial: boolean,
withCutaway: boolean, withCutaway: boolean,
notes: string) { notes: string) {
const jumpData = { const jumpData = {
id: id, id: id,
isSpecial: isSpecial, isSpecial: isSpecial,
withCutaway: withCutaway, withCutaway: withCutaway,
notes: notes notes: notes
}; };
const bodyUpdatedJump = new JumpReq(jumpData); const bodyUpdatedJump = new JumpReq(jumpData);
return this.http.put(`${this.apiUrl}/Jump/${id}`, return this.http.put(`${this.apiUrl}/Jump/${id}`,
bodyUpdatedJump, bodyUpdatedJump,
{ headers: this.headers, }); { headers: this.headers, });
} }
private AddJumps(selectedJumpType: number, private AddJumps(selectedJumpType: number,
@@ -136,8 +135,7 @@ export class JumpService extends BaseService {
defaultDeployAltitude: number, defaultDeployAltitude: number,
countOfJumps: number, countOfJumps: number,
notes: string, notes: string,
isSpecial: boolean) isSpecial: boolean) {
{
for (let i = 0; i < countOfJumps; i++) { for (let i = 0; i < countOfJumps; i++) {
const bodyNewjump: JumpReq = { const bodyNewjump: JumpReq = {
jumpTypeId: selectedJumpType, jumpTypeId: selectedJumpType,
@@ -153,15 +151,13 @@ export class JumpService extends BaseService {
isSpecial: isSpecial isSpecial: isSpecial
}; };
let call = this.http.post(`${this.apiUrl}/Jump`, let call = this.http.post(`${this.apiUrl}/Jump`, bodyNewjump, { headers: this.headers });
bodyNewjump,
{ headers: this.headers });
this.callsToAdd.push(call); this.callsToAdd.push(call);
} }
} }
private MapWithDataInCache(apiResp: Array<JumpResp>) : Array<Jump> { private MapWithDataInCache(apiResp: Array<JumpResp>): Array<Jump> {
let allDropzones: Array<DropZoneResp>; let allDropzones: Array<DropZoneResp>;
this.dropzoneService.getFromCache().subscribe(data => { allDropzones = data; }); this.dropzoneService.getFromCache().subscribe(data => { allDropzones = data; });
let allAircrafts: Array<AircraftResp>; let allAircrafts: Array<AircraftResp>;
@@ -171,8 +167,7 @@ export class JumpService extends BaseService {
let allGears: Array<GearResp>; let allGears: Array<GearResp>;
this.gearService.getFromCache().subscribe(data => { allGears = data; }); this.gearService.getFromCache().subscribe(data => { allGears = data; });
return apiResp.map((data) => return apiResp.map((data) => {
{
let tmp = new Jump(data); let tmp = new Jump(data);
tmp.dropZone = allDropzones.find(d => d.id == data.dropZoneId); tmp.dropZone = allDropzones.find(d => d.id == data.dropZoneId);

View File

@@ -0,0 +1,52 @@
import { Injectable } from "@angular/core";
import { DatePipe } from "@angular/common";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs";
import { map } from "rxjs/operators";
import { TunnelResp, TunnelReq } from "../models/tunnel";
import { BaseService } from "./base.service";
import { CacheApiKey } from "../models/cache-api-key.enum";
@Injectable()
export class TunnelService extends BaseService {
private datePipe: DatePipe;
constructor(private http: HttpClient) {
super();
}
public getListOfTunnels(): Observable<Array<TunnelResp>> {
let callToApi = this.http.get<Array<TunnelResp>>(`${this.apiUrl}/Tunnel`, { headers: this.headers });
return this.serviceCacheApi.get<Array<TunnelResp>>(CacheApiKey.Gear, callToApi);
}
public AddFlight(selectedTunnel: number,
flightDate: Date,
nbMinutes: number,
comment: string)
{
const bodyNewFlight: TunnelReq = {
id: 0,
tunnelId: selectedTunnel,
flightDate: this.datePipe.transform(flightDate, "yyyy-MM-dd"),
comment: comment,
nbMinutes: nbMinutes
};
return this.http.post(`${this.apiUrl}/Tunnel`, bodyNewFlight, { headers: this.headers});
}
public getById(id: number) : Observable<TunnelResp> {
return this.serviceCacheApi.getByKey<Array<TunnelResp>>(CacheApiKey.Tunnel)
.pipe(map(data => {
return data.find(f => f.id === id);
}));
}
public getFromCache(): Observable<Array<TunnelResp>> {
return this.serviceCacheApi.getByKey<Array<TunnelResp>>(CacheApiKey.Tunnel);
}
}