New service and model for the tunnel flights
This commit is contained in:
@@ -38,6 +38,7 @@ import { AuthGuardService } from "../services/auth-guard.service";
|
||||
import { ImageService } from "../services/image.service";
|
||||
import { ConfigurationHelper } from "../services/configuration-helper";
|
||||
import { ServiceCacheApi } from "../services/service-cache-api.service";
|
||||
import { TunnelService } from "../services/tunnel.service";
|
||||
|
||||
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
|
||||
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
|
||||
@@ -193,6 +194,7 @@ export function initConfig(configService: ConfigurationHelper) {
|
||||
],
|
||||
exports: [HttpClientModule],
|
||||
providers: [
|
||||
TunnelService,
|
||||
ImageService,
|
||||
AircraftService,
|
||||
DropzoneService,
|
||||
|
||||
@@ -3,16 +3,38 @@ import { formatDate } from '@angular/common';
|
||||
import { DateAdapter, MAT_DATE_FORMATS, NativeDateAdapter } from "@angular/material/core";
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
|
||||
import { TunnelResp } from "../../models/tunnel";
|
||||
|
||||
import { DateService } from 'src/services/date.service';
|
||||
import { TunnelService } from 'src/services/tunnel.service';
|
||||
import { ServiceComm } from 'src/services/service-comm.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({
|
||||
selector: 'app-new-tunnel-flight',
|
||||
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 endDate: Date;
|
||||
public minutesOfFlight: number;
|
||||
@@ -24,19 +46,19 @@ export class NewTunnelFlightComponent {
|
||||
private pendingAddRequest: boolean;
|
||||
|
||||
constructor(private serviceComm: ServiceComm,
|
||||
private serviceTunnel: TunnelService,
|
||||
private dateService: DateService,
|
||||
private translateService: TranslateService,
|
||||
private statsService : StatsService) {}
|
||||
private serviceTunnel: TunnelService,
|
||||
private dateService: DateService,
|
||||
private translateService: TranslateService,
|
||||
private statsService: StatsService) { }
|
||||
|
||||
ngOnInit() {
|
||||
this.serviceComm.forceTranslateTitle.subscribe((data)=> {
|
||||
if (data === true){
|
||||
this.serviceComm.forceTranslateTitle.subscribe((data) => {
|
||||
if (data === true) {
|
||||
this.updateTitle();
|
||||
}
|
||||
});
|
||||
this.updateTitle();
|
||||
|
||||
|
||||
this.pendingAddRequest = false;
|
||||
this.initForm();
|
||||
this.getListOfTunnels();
|
||||
@@ -45,7 +67,20 @@ export class NewTunnelFlightComponent {
|
||||
onFormSubmit() {
|
||||
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 {
|
||||
@@ -56,7 +91,7 @@ export class NewTunnelFlightComponent {
|
||||
}
|
||||
|
||||
private getListOfTunnels() {
|
||||
this.serviceTunnel.getListOfTunnels(true).subscribe((data) => {
|
||||
this.serviceTunnel.getListOfTunnels().subscribe((data) => {
|
||||
data.sort((a, b) => a.name.localeCompare(b.name));
|
||||
this.listOfTunnel = data;
|
||||
this.countDatasLoaded++;
|
||||
|
||||
@@ -10,5 +10,6 @@ export enum CacheApiKey {
|
||||
StatsByGear,
|
||||
StatsOfLastYear,
|
||||
StatsOfLastMonth,
|
||||
StatsByYear
|
||||
StatsByYear,
|
||||
Tunnel
|
||||
}
|
||||
|
||||
25
Front/skydivelogs-app/src/models/tunnel.ts
Normal file
25
Front/skydivelogs-app/src/models/tunnel.ts
Normal 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;
|
||||
}
|
||||
@@ -20,37 +20,37 @@ import { GearService } from "./gear.service";
|
||||
|
||||
@Injectable()
|
||||
export class JumpService extends BaseService {
|
||||
private callsToAdd : Array<Observable<any>>;
|
||||
private callsToAdd: Array<Observable<any>>;
|
||||
|
||||
constructor(private http: HttpClient,
|
||||
private dateService: DateService,
|
||||
private datePipe: DatePipe,
|
||||
private dropzoneService: DropzoneService,
|
||||
private aircraftService: AircraftService,
|
||||
private jumpTypeService: JumpTypeService,
|
||||
private gearService: GearService) {
|
||||
constructor(private http: HttpClient,
|
||||
private dateService: DateService,
|
||||
private datePipe: DatePipe,
|
||||
private dropzoneService: DropzoneService,
|
||||
private aircraftService: AircraftService,
|
||||
private jumpTypeService: JumpTypeService,
|
||||
private gearService: GearService) {
|
||||
super();
|
||||
}
|
||||
|
||||
public GetListOfJumps(): Observable<Array<Jump>> {
|
||||
return this.http.get<Array<JumpResp>>(`${this.apiUrl}/Jump`,
|
||||
{ headers: this.headers })
|
||||
.pipe(map((response) => {
|
||||
return this.MapWithDataInCache(response);
|
||||
}));
|
||||
return this.http.get<Array<JumpResp>>(`${this.apiUrl}/Jump`,
|
||||
{ headers: this.headers })
|
||||
.pipe(map((response) => {
|
||||
return this.MapWithDataInCache(response);
|
||||
}));
|
||||
}
|
||||
|
||||
public GetJumps(beginIndex: number, endIndex: number): Observable<JumpList> {
|
||||
return this.http.get<JumpListResp>(`${this.apiUrl}/Jump/${beginIndex}/${endIndex}`,
|
||||
{ headers: this.headers })
|
||||
.pipe(map((response) => {
|
||||
let result: JumpList = {
|
||||
rows : this.MapWithDataInCache(response.rows),
|
||||
count : response.count
|
||||
};
|
||||
return this.http.get<JumpListResp>(`${this.apiUrl}/Jump/${beginIndex}/${endIndex}`,
|
||||
{ headers: this.headers })
|
||||
.pipe(map((response) => {
|
||||
let result: JumpList = {
|
||||
rows: this.MapWithDataInCache(response.rows),
|
||||
count: response.count
|
||||
};
|
||||
|
||||
return new JumpList(result);
|
||||
}));
|
||||
return new JumpList(result);
|
||||
}));
|
||||
}
|
||||
|
||||
public AddListOfJump(selectedJumpType: number,
|
||||
@@ -64,8 +64,7 @@ export class JumpService extends BaseService {
|
||||
defaultDeployAltitude: number,
|
||||
countOfJumps: number,
|
||||
notes: string,
|
||||
isSpecial: boolean): Observable<any[]>
|
||||
{
|
||||
isSpecial: boolean): Observable<any[]> {
|
||||
this.callsToAdd = new Array<Observable<any>>();
|
||||
const diffInDays = this.dateService.DiffBetweenDates(beginDate, endDate) + 1;
|
||||
const countOfJumpsPerDay = Math.trunc(countOfJumps / diffInDays);
|
||||
@@ -105,25 +104,25 @@ export class JumpService extends BaseService {
|
||||
|
||||
public DeleteJump(item: Jump) {
|
||||
this.http.delete(`${this.apiUrl}/Jump/${item.id}`,
|
||||
{ headers: this.headers, })
|
||||
.subscribe();
|
||||
{ headers: this.headers, })
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
public UpdateJump(id: number,
|
||||
isSpecial: boolean,
|
||||
withCutaway: boolean,
|
||||
notes: string) {
|
||||
isSpecial: boolean,
|
||||
withCutaway: boolean,
|
||||
notes: string) {
|
||||
const jumpData = {
|
||||
id: id,
|
||||
isSpecial: isSpecial,
|
||||
withCutaway: withCutaway,
|
||||
notes: notes
|
||||
};
|
||||
};
|
||||
const bodyUpdatedJump = new JumpReq(jumpData);
|
||||
|
||||
|
||||
return this.http.put(`${this.apiUrl}/Jump/${id}`,
|
||||
bodyUpdatedJump,
|
||||
{ headers: this.headers, });
|
||||
bodyUpdatedJump,
|
||||
{ headers: this.headers, });
|
||||
}
|
||||
|
||||
private AddJumps(selectedJumpType: number,
|
||||
@@ -136,8 +135,7 @@ export class JumpService extends BaseService {
|
||||
defaultDeployAltitude: number,
|
||||
countOfJumps: number,
|
||||
notes: string,
|
||||
isSpecial: boolean)
|
||||
{
|
||||
isSpecial: boolean) {
|
||||
for (let i = 0; i < countOfJumps; i++) {
|
||||
const bodyNewjump: JumpReq = {
|
||||
jumpTypeId: selectedJumpType,
|
||||
@@ -153,15 +151,13 @@ export class JumpService extends BaseService {
|
||||
isSpecial: isSpecial
|
||||
};
|
||||
|
||||
let call = this.http.post(`${this.apiUrl}/Jump`,
|
||||
bodyNewjump,
|
||||
{ headers: this.headers });
|
||||
let call = this.http.post(`${this.apiUrl}/Jump`, bodyNewjump, { headers: this.headers });
|
||||
|
||||
this.callsToAdd.push(call);
|
||||
}
|
||||
}
|
||||
|
||||
private MapWithDataInCache(apiResp: Array<JumpResp>) : Array<Jump> {
|
||||
private MapWithDataInCache(apiResp: Array<JumpResp>): Array<Jump> {
|
||||
let allDropzones: Array<DropZoneResp>;
|
||||
this.dropzoneService.getFromCache().subscribe(data => { allDropzones = data; });
|
||||
let allAircrafts: Array<AircraftResp>;
|
||||
@@ -171,8 +167,7 @@ export class JumpService extends BaseService {
|
||||
let allGears: Array<GearResp>;
|
||||
this.gearService.getFromCache().subscribe(data => { allGears = data; });
|
||||
|
||||
return apiResp.map((data) =>
|
||||
{
|
||||
return apiResp.map((data) => {
|
||||
let tmp = new Jump(data);
|
||||
|
||||
tmp.dropZone = allDropzones.find(d => d.id == data.dropZoneId);
|
||||
|
||||
52
Front/skydivelogs-app/src/services/tunnel.service.ts
Normal file
52
Front/skydivelogs-app/src/services/tunnel.service.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user