From 0fa5b080d8c656e0b6beba32256f96d6f95a6d60 Mon Sep 17 00:00:00 2001 From: ThompsonNye Date: Mon, 16 Jun 2025 21:05:07 +0200 Subject: [PATCH] Add API models and clients manually --- src/Vegasco-Web/src/app/api/api-base-path.ts | 6 ++++ .../src/app/api/cars/car-client.ts | 35 +++++++++++++++++++ src/Vegasco-Web/src/app/api/cars/car.ts | 4 +++ .../src/app/api/cars/create-car-request.ts | 3 ++ .../src/app/api/cars/get-cars-response.ts | 3 ++ .../src/app/api/cars/update-car-request.ts | 3 ++ .../api/consumptions/consumption-client.ts | 35 +++++++++++++++++++ .../consumption-entry.ts | 0 .../consumptions/create-consumption-entry.ts | 7 ++++ .../get-consumption-entries-response.ts | 0 .../consumptions/update-consumption-entry.ts | 7 ++++ src/Vegasco-Web/src/app/app.config.ts | 5 +++ 12 files changed, 108 insertions(+) create mode 100644 src/Vegasco-Web/src/app/api/api-base-path.ts create mode 100644 src/Vegasco-Web/src/app/api/cars/car-client.ts create mode 100644 src/Vegasco-Web/src/app/api/cars/car.ts create mode 100644 src/Vegasco-Web/src/app/api/cars/create-car-request.ts create mode 100644 src/Vegasco-Web/src/app/api/cars/get-cars-response.ts create mode 100644 src/Vegasco-Web/src/app/api/cars/update-car-request.ts create mode 100644 src/Vegasco-Web/src/app/api/consumptions/consumption-client.ts rename src/Vegasco-Web/src/app/api/{models => consumptions}/consumption-entry.ts (100%) create mode 100644 src/Vegasco-Web/src/app/api/consumptions/create-consumption-entry.ts rename src/Vegasco-Web/src/app/api/{models => consumptions}/get-consumption-entries-response.ts (100%) create mode 100644 src/Vegasco-Web/src/app/api/consumptions/update-consumption-entry.ts diff --git a/src/Vegasco-Web/src/app/api/api-base-path.ts b/src/Vegasco-Web/src/app/api/api-base-path.ts new file mode 100644 index 0000000..173a941 --- /dev/null +++ b/src/Vegasco-Web/src/app/api/api-base-path.ts @@ -0,0 +1,6 @@ +import { InjectionToken } from "@angular/core"; + +/** + * The base path for all API requests, e.g. when using a proxy on the origin's address. + */ +export const API_BASE_PATH = new InjectionToken('API_BASE_PATH'); diff --git a/src/Vegasco-Web/src/app/api/cars/car-client.ts b/src/Vegasco-Web/src/app/api/cars/car-client.ts new file mode 100644 index 0000000..8039ccd --- /dev/null +++ b/src/Vegasco-Web/src/app/api/cars/car-client.ts @@ -0,0 +1,35 @@ +import {inject, Injectable} from "@angular/core"; +import {HttpClient} from '@angular/common/http'; +import {map, Observable} from 'rxjs'; +import {API_BASE_PATH} from '../api-base-path'; + +@Injectable({ + providedIn: 'root', +}) +export class CarClient { + private readonly http = inject(HttpClient); + private readonly apiBasePath = inject(API_BASE_PATH, {optional: true}); + + getAll(): Observable { + return this.http.get(`${this.apiBasePath}/v1/cars`); + } + + getSingle(id: string): Observable { + return this.http.get(`${this.apiBasePath}/v1/cars/${id}`); + } + + create(request: CreateCarRequest): Observable { + return this.http.post(`${this.apiBasePath}/v1/cars`, request); + } + + update(request: UpdateCarRequest): Observable { + return this.http.put(`${this.apiBasePath}/v1/cars`, request); + } + + delete(id: string): Observable { + return this.http.delete(`${this.apiBasePath}/v1/cars/${id}`) + .pipe( + map(_ => undefined) + ); + } +} diff --git a/src/Vegasco-Web/src/app/api/cars/car.ts b/src/Vegasco-Web/src/app/api/cars/car.ts new file mode 100644 index 0000000..ecd122a --- /dev/null +++ b/src/Vegasco-Web/src/app/api/cars/car.ts @@ -0,0 +1,4 @@ +interface Car { + id: string; + name: string; +} diff --git a/src/Vegasco-Web/src/app/api/cars/create-car-request.ts b/src/Vegasco-Web/src/app/api/cars/create-car-request.ts new file mode 100644 index 0000000..2d462f3 --- /dev/null +++ b/src/Vegasco-Web/src/app/api/cars/create-car-request.ts @@ -0,0 +1,3 @@ +interface CreateCarRequest { + name: string; +} diff --git a/src/Vegasco-Web/src/app/api/cars/get-cars-response.ts b/src/Vegasco-Web/src/app/api/cars/get-cars-response.ts new file mode 100644 index 0000000..6287ec1 --- /dev/null +++ b/src/Vegasco-Web/src/app/api/cars/get-cars-response.ts @@ -0,0 +1,3 @@ +interface GetCarsResponse { + cars: Car[]; +} diff --git a/src/Vegasco-Web/src/app/api/cars/update-car-request.ts b/src/Vegasco-Web/src/app/api/cars/update-car-request.ts new file mode 100644 index 0000000..f883f34 --- /dev/null +++ b/src/Vegasco-Web/src/app/api/cars/update-car-request.ts @@ -0,0 +1,3 @@ +interface UpdateCarRequest { + name: string; +} diff --git a/src/Vegasco-Web/src/app/api/consumptions/consumption-client.ts b/src/Vegasco-Web/src/app/api/consumptions/consumption-client.ts new file mode 100644 index 0000000..31aee97 --- /dev/null +++ b/src/Vegasco-Web/src/app/api/consumptions/consumption-client.ts @@ -0,0 +1,35 @@ +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'; + +@Injectable({ + providedIn: 'root', +}) +export class ConsumptionClient { + private readonly http = inject(HttpClient); + private readonly apiBasePath = inject(API_BASE_PATH, {optional: true}); + + getAll(): Observable { + return this.http.get(`${this.apiBasePath}/v1/consumptions`); + } + + getSingle(id: string): Observable { + return this.http.get(`${this.apiBasePath}/v1/consumptions/${id}`); + } + + create(request: CreateCarRequest): Observable { + return this.http.post(`${this.apiBasePath}/v1/consumptions`, request); + } + + update(request: UpdateCarRequest): Observable { + return this.http.put(`${this.apiBasePath}/v1/consumptions`, request); + } + + delete(id: string): Observable { + return this.http.delete(`${this.apiBasePath}/v1/consumptions/${id}`) + .pipe( + map(_ => undefined), + ); + } +} diff --git a/src/Vegasco-Web/src/app/api/models/consumption-entry.ts b/src/Vegasco-Web/src/app/api/consumptions/consumption-entry.ts similarity index 100% rename from src/Vegasco-Web/src/app/api/models/consumption-entry.ts rename to src/Vegasco-Web/src/app/api/consumptions/consumption-entry.ts diff --git a/src/Vegasco-Web/src/app/api/consumptions/create-consumption-entry.ts b/src/Vegasco-Web/src/app/api/consumptions/create-consumption-entry.ts new file mode 100644 index 0000000..fadf32f --- /dev/null +++ b/src/Vegasco-Web/src/app/api/consumptions/create-consumption-entry.ts @@ -0,0 +1,7 @@ +interface CreateConsumptionEntry { + dateTime: string; + distance: number; + amount: number; + ignoreInCalculation: boolean; + carId: string; +} diff --git a/src/Vegasco-Web/src/app/api/models/get-consumption-entries-response.ts b/src/Vegasco-Web/src/app/api/consumptions/get-consumption-entries-response.ts similarity index 100% rename from src/Vegasco-Web/src/app/api/models/get-consumption-entries-response.ts rename to src/Vegasco-Web/src/app/api/consumptions/get-consumption-entries-response.ts diff --git a/src/Vegasco-Web/src/app/api/consumptions/update-consumption-entry.ts b/src/Vegasco-Web/src/app/api/consumptions/update-consumption-entry.ts new file mode 100644 index 0000000..fcdd0f9 --- /dev/null +++ b/src/Vegasco-Web/src/app/api/consumptions/update-consumption-entry.ts @@ -0,0 +1,7 @@ +interface UpdateConsumptionEntry { + dateTime: string; + distance: number; + amount: number; + ignoreInCalculation: boolean; + carId: string; +} diff --git a/src/Vegasco-Web/src/app/app.config.ts b/src/Vegasco-Web/src/app/app.config.ts index 9749717..ad64235 100644 --- a/src/Vegasco-Web/src/app/app.config.ts +++ b/src/Vegasco-Web/src/app/app.config.ts @@ -7,6 +7,7 @@ import { includeBearerTokenInterceptor } from 'keycloak-angular'; import { providePrimeNG } from 'primeng/config'; import { routes } from './app.routes'; import { provideKeycloakAngular } from './auth/auth.config'; +import {API_BASE_PATH} from './api/api-base-path'; export const appConfig: ApplicationConfig = { providers: [ @@ -21,5 +22,9 @@ export const appConfig: ApplicationConfig = { }, ripple: true }), + { + provide: API_BASE_PATH, + useValue: '/api' + } ] };