Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules/
42 changes: 42 additions & 0 deletions src/app/core/interfaces/vehicle.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export interface VehicleMedia {
name: string;
url: string;
}

export interface VehicleEmissions {
template: string;
value: number;
}

export interface VehicleMeta {
passengers: number;
drivetrain: string[];
bodystyles: string[];
emissions: VehicleEmissions;
}

/** Shape returned by GET /api/vehicles/ */
export interface VehicleSummary {
id: string;
name: string;
modelYear: string;
apiUrl: string;
media: VehicleMedia[];
}

/** Shape returned by GET /api/vehicles/:id */
export interface VehicleDetail {
id: string;
description: string;
price: string;
meta: VehicleMeta;
}

/** Merged type used throughout the application */
export interface Vehicle extends VehicleSummary, VehicleDetail {}

/**
* Discriminated union — extend with Motorbike when the API supports it.
* e.g. type AnyVehicle = Car | Motorbike;
*/
export type AnyVehicle = Vehicle;
66 changes: 66 additions & 0 deletions src/app/core/services/vehicle.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { HttpClient } from '@angular/common/http';
import { Injectable, signal } from '@angular/core';
import {
EMPTY,
Observable,
catchError,
forkJoin,
map,
switchMap,
} from 'rxjs';
import {
AnyVehicle,
VehicleDetail,
VehicleSummary,
} from '../interfaces/vehicle.interface';

const API_BASE =
'https://frontend-code-test-api-1023992580432.europe-west2.run.app';

@Injectable({ providedIn: 'root' })
export class VehicleService {
readonly loading = signal(false);
readonly error = signal<string | null>(null);

constructor(private readonly http: HttpClient) {}

/**
* Fetches the vehicle list then retrieves all detail records in parallel.
* Uses switchMap so any re-trigger cancels the previous in-flight request,
* and forkJoin because HTTP calls are one-shot (complete after one emission).
*/
fetchAll(): Observable<AnyVehicle[]> {
this.loading.set(true);
this.error.set(null);

return this.http.get<VehicleSummary[]>(`${API_BASE}/api/vehicles/`).pipe(
switchMap((summaries) =>
forkJoin(
summaries.map((summary) =>
this.http
.get<VehicleDetail>(`${API_BASE}${summary.apiUrl}`)
.pipe(
map((detail) => this.merge(summary, detail)),
// Per-vehicle errors are swallowed so one bad record
// (e.g. "problematic") doesn't kill the entire list.
catchError(() => EMPTY),
),
),
),
),
map((vehicles) => {
this.loading.set(false);
return vehicles;
}),
catchError((err: Error) => {
this.loading.set(false);
this.error.set(err.message ?? 'Failed to load vehicles');
return EMPTY;
}),
);
}

private merge(summary: VehicleSummary, detail: VehicleDetail): AnyVehicle {
return { ...summary, ...detail };
}
}