Include necessary info directly in get consumption entries response dto
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-06-22 11:51:38 +02:00
parent a997a3b825
commit cb3c8c0d18
10 changed files with 114 additions and 107 deletions

View File

@@ -1,14 +1,16 @@
import {inject, Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {API_BASE_PATH} from '../api-base-path';
import {map, Observable} from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';
import { GetConsumptionEntriesResponse } from '@vegasco-web/api/consumptions/get-consumption-entries-response';
import { map, Observable } from 'rxjs';
import { API_BASE_PATH } from '../api-base-path';
import { ConsumptionEntry } from './consumption-entry';
@Injectable({
providedIn: 'root',
})
export class ConsumptionClient {
private readonly http = inject(HttpClient);
private readonly apiBasePath = inject(API_BASE_PATH, {optional: true});
private readonly apiBasePath = inject(API_BASE_PATH, { optional: true });
getAll(): Observable<GetConsumptionEntriesResponse> {
return this.http.get<GetConsumptionEntriesResponse>(`${this.apiBasePath}/v1/consumptions`);

View File

@@ -1,4 +1,4 @@
interface ConsumptionEntry {
export interface ConsumptionEntry {
id: string;
dateTime: string;
distance: number;

View File

@@ -0,0 +1,10 @@
export interface GetConsumptionEntriesEntry {
id: string;
dateTime: string;
distance: number;
amount: number;
car: {
id: string;
name: string;
};
}

View File

@@ -1,3 +1,5 @@
interface GetConsumptionEntriesResponse {
consumptions: ConsumptionEntry[];
}
import { GetConsumptionEntriesEntry } from "./get-consumption-entries-entry";
export interface GetConsumptionEntriesResponse {
consumptions: GetConsumptionEntriesEntry[];
}

View File

@@ -1,9 +0,0 @@
export interface Consumption {
id: string;
dateTime: string;
distance: number;
amount: number;
ignoreInCalculation: boolean;
carId: string;
car: Car;
}

View File

@@ -3,17 +3,17 @@ import { HttpErrorResponse } from '@angular/common/http';
import { Component, DestroyRef, inject, input, output } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { NgIconComponent, provideIcons } from '@ng-icons/core';
import {
matCalendarMonthSharp,
matDeleteSharp,
matStraightenSharp,
matLocalGasStationSharp,
} from '@ng-icons/material-icons/sharp';
import {
matDirectionsCarOutline,
} from '@ng-icons/material-icons/outline';
import {
matCalendarMonthSharp,
matDeleteSharp,
matLocalGasStationSharp,
matStraightenSharp,
} from '@ng-icons/material-icons/sharp';
import { ConsumptionClient } from '@vegasco-web/api/consumptions/consumption-client';
import { Consumption } from '@vegasco-web/api/models/consumption';
import { GetConsumptionEntriesEntry } from '@vegasco-web/api/consumptions/get-consumption-entries-entry';
import { RoutingService } from '@vegasco-web/services/routing.service';
import { ConfirmationService, MessageService } from 'primeng/api';
import { ButtonModule } from 'primeng/button';
@@ -45,9 +45,9 @@ import { catchError, EMPTY, Observable, tap, throwError } from 'rxjs';
styleUrl: './entry-card.component.scss'
})
export class EntryCardComponent {
readonly entry = input.required<Consumption>();
readonly entry = input.required<GetConsumptionEntriesEntry>();
readonly entryDeleted = output<Consumption>();
readonly entryDeleted = output<GetConsumptionEntriesEntry>();
private readonly routingService = inject(RoutingService);
private readonly consumptionClient = inject(ConsumptionClient);

View File

@@ -1,8 +1,16 @@
import { AsyncPipe, CommonModule } from '@angular/common';
import { HttpErrorResponse } from '@angular/common/http';
import { Component, DestroyRef, inject, OnInit } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { RouterLink } from '@angular/router';
import { NgIconComponent, provideIcons } from '@ng-icons/core';
import {
matAddSharp,
} from '@ng-icons/material-icons/sharp';
import { CarClient } from '@vegasco-web/api/cars/car-client';
import { ConsumptionClient } from '@vegasco-web/api/consumptions/consumption-client';
import { GetConsumptionEntriesEntry } from '@vegasco-web/api/consumptions/get-consumption-entries-entry';
import { MessageService } from 'primeng/api';
import { ButtonModule } from 'primeng/button';
import { DataViewModule } from 'primeng/dataview';
@@ -20,14 +28,9 @@ import {
tap,
throwError
} from 'rxjs';
import { EntriesOverviewService } from './services/entries-overview.service';
import { EntryCardComponent } from './components/entry-card/entry-card.component';
import {
matAddSharp,
} from '@ng-icons/material-icons/sharp';
import { NgIconComponent, provideIcons } from '@ng-icons/core';
import { HttpErrorResponse } from '@angular/common/http';
import { SelectedCarService } from '../services/selected-car.service';
import { EntryCardComponent } from './components/entry-card/entry-card.component';
import { ConsumptionEntry } from '@vegasco-web/api/consumptions/consumption-entry';
@Component({
selector: 'app-entries',
@@ -48,18 +51,18 @@ import { SelectedCarService } from '../services/selected-car.service';
provideIcons({
matAddSharp,
}),
EntriesOverviewService,
],
templateUrl: './entries.component.html',
styleUrl: './entries.component.scss'
})
export class EntriesComponent implements OnInit {
private readonly entriesOverviewService = inject(EntriesOverviewService);
private readonly carClient = inject(CarClient);
private readonly consumptionClient = inject(ConsumptionClient);
private readonly messageService = inject(MessageService);
private readonly selectedCarService = inject(SelectedCarService);
private readonly destroyRef = inject(DestroyRef);
protected readonly consumptionEntries$: Observable<ConsumptionEntry[]>;
protected readonly consumptionEntries$: Observable<GetConsumptionEntriesEntry[]>;
protected readonly cars$: Observable<Car[]>;
protected readonly skeletonsIterationSource = Array(10).fill(0);
@@ -69,9 +72,10 @@ export class EntriesComponent implements OnInit {
private readonly deletedEntries$ = new BehaviorSubject(<string[]>[]);
constructor() {
const entries = this.entriesOverviewService.getEntries()
const entries = this.consumptionClient.getAll()
.pipe(
takeUntilDestroyed(),
map(response => response.consumptions),
catchError((error) => this.handleGetEntriesError(error))
);
@@ -92,13 +96,14 @@ export class EntriesComponent implements OnInit {
return nonDeletedEntries;
}
return nonDeletedEntries.filter(entry => entry.carId === selectedCar.id);
return nonDeletedEntries.filter(entry => entry.car.id === selectedCar.id);
})
);
this.cars$ = this.entriesOverviewService.getCars()
this.cars$ = this.carClient.getAll()
.pipe(
takeUntilDestroyed(),
map(response => response.cars),
map((cars) => cars
.sort((a, b) => a.name.localeCompare(b.name))),
tap((cars) => {
@@ -129,7 +134,7 @@ export class EntriesComponent implements OnInit {
.subscribe();
}
onEntryDeleted(entry: ConsumptionEntry): void {
onEntryDeleted(entry: GetConsumptionEntriesEntry): void {
this.deletedEntries$.next([...this.deletedEntries$.value, entry.id]);
this.messageService.add({
severity: 'success',

View File

@@ -1,50 +0,0 @@
import { inject, Injectable } from "@angular/core";
import { CarClient } from "@vegasco-web/api/cars/car-client";
import { ConsumptionClient } from "@vegasco-web/api/consumptions/consumption-client";
import { Consumption } from "@vegasco-web/api/models/consumption";
import { RoutingService } from "@vegasco-web/services/routing.service";
import { combineLatest, map, Observable, shareReplay } from "rxjs";
@Injectable()
export class EntriesOverviewService {
private readonly carClient = inject(CarClient);
private readonly consumptionClient = inject(ConsumptionClient);
private cachedCars$: Observable<Car[]> | null = null;
private ensureCarsAreCached(): void {
if (this.cachedCars$ !== null) {
return;
}
this.cachedCars$ = this.carClient.getAll()
.pipe(
map(response => response.cars),
shareReplay(1)
);
}
getEntries(): Observable<Consumption[]> {
this.ensureCarsAreCached();
const entries$ = this.consumptionClient.getAll()
.pipe(map(response => response.consumptions));
return combineLatest([this.cachedCars$!, entries$])
.pipe(
map(([cars, entries]) => {
return entries
.sort((a, b) => b.dateTime.localeCompare(a.dateTime))
.map((entry): Consumption => ({
...entry,
car: cars.find(car => car.id === entry.carId)!
}));
})
)
}
getCars(): Observable<Car[]> {
this.ensureCarsAreCached();
return this.cachedCars$!;
}
}