Skip to content

Commit 6d564fe

Browse files
committed
Added dtos to frontend app
1 parent b66daf1 commit 6d564fe

File tree

9 files changed

+63
-17
lines changed

9 files changed

+63
-17
lines changed

apps/car-rental-backend/src/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ async function bootstrap() {
88
const config = new DocumentBuilder().setTitle('Car Rental API').build();
99
const documentFactory = () => SwaggerModule.createDocument(app, config);
1010
const globalPrefix = 'api';
11+
app.enableCors();
1112
app.useGlobalPipes(new ValidationPipe({ transform: true }));
1213
app.setGlobalPrefix(globalPrefix);
1314
const port = process.env.PORT || 3000;

apps/car-rental/src/components/vehicles/vehicles-list/vehicles-list.component.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ <h2>Vehicle List</h2>
33
<ul>
44
<li *ngFor="let vehicle of vehicles">
55
{{ vehicle.registrationNumber }} - {{ vehicle.brand }}
6-
{{ vehicle.model }} ({{ vehicle.year }})
76
</li>
87
</ul>
98
</div>
109
<div *ngIf="vehicleBrands">
1110
<h2>Vehicle Brands List</h2>
1211
<ul>
1312
<li *ngFor="let brand of vehicleBrands">
14-
{{ brand }}
13+
{{ brand.id }} - {{ brand.name }}
1514
</li>
1615
</ul>
1716
</div>

apps/car-rental/src/components/vehicles/vehicles-list/vehicles-list.component.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Component, OnInit } from '@angular/core';
22
import { CommonModule } from '@angular/common';
33
import { VehiclesService } from '../../../services/vehicles.service';
44
import { VehicleBrandsService } from '../../../services/vehicle-brands.service';
5+
import { VehicleDto } from '../../../../src/dto/vehicle';
6+
import { VehicleBrandDto } from '../../../../src/dto/vehicle-brand';
57

68
@Component({
79
selector: 'app-vehicles-list',
@@ -10,20 +12,22 @@ import { VehicleBrandsService } from '../../../services/vehicle-brands.service';
1012
styleUrl: './vehicles-list.component.css',
1113
})
1214
export class VehiclesListComponent implements OnInit {
13-
vehicles: any[] = [];
14-
vehicleBrands: string[] = [];
15+
vehicles: VehicleDto[] = [];
16+
vehicleBrands: VehicleBrandDto[] = [];
1517

1618
constructor(
1719
private readonly vehiclesService: VehiclesService,
1820
private readonly vehiclesBrandService: VehicleBrandsService
1921
) {}
2022

2123
ngOnInit(): void {
22-
this.vehiclesService.getVehicles().subscribe((data) => {
23-
this.vehicles = data;
24-
});
25-
this.vehiclesBrandService.getVehicleBrands().subscribe((data: string[]) => {
26-
this.vehicleBrands = data;
24+
this.vehiclesService.getVehicles().subscribe((response) => {
25+
this.vehicles = response.data;
2726
});
27+
this.vehiclesBrandService
28+
.getVehicleBrands()
29+
.subscribe((response) => {
30+
this.vehicleBrands = response.data;
31+
});
2832
}
2933
}

apps/car-rental/src/dto/page-meta.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type PageMeta = {
2+
readonly page: number;
3+
readonly take: number;
4+
readonly itemCount: number;
5+
readonly pageCount: number;
6+
readonly hasPreviousPage: boolean;
7+
readonly hasNextPage: boolean;
8+
}

apps/car-rental/src/dto/page.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { PageMeta } from "./page-meta";
2+
3+
export type Page<T> = {
4+
data: T[];
5+
meta: PageMeta;
6+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type VehicleBrandDto = {
2+
id: number;
3+
name: string;
4+
createdAt: Date;
5+
updatedAt: Date;
6+
}
7+
8+
export type CreateVehicleBrandDto = Pick<VehicleBrandDto, 'name'>;
9+
export type UpdateVehicleBrandDto = Pick<VehicleBrandDto, 'name'>;

apps/car-rental/src/dto/vehicle.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export type VehicleDto = {
2+
id: number;
3+
brand: string;
4+
registrationNumber: string;
5+
vehicleIdentificationNumber: string;
6+
clientEmail?: string;
7+
clientAddress?: string;
8+
createdAt: Date;
9+
updatedAt: Date;
10+
}
11+
12+
export type CreateVehicleDto = Pick<VehicleDto, 'brand' | 'registrationNumber' | 'vehicleIdentificationNumber'>;
13+
14+
export type UpdateVehicleDto = Partial<Omit<VehicleDto, 'id' | 'createdAt' | 'updatedAt'>>;
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { HttpClient } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3+
import { Observable } from 'rxjs';
4+
import { VehicleBrandDto } from '../dto/vehicle-brand';
5+
import { Page } from '../dto/page';
36

47
@Injectable({
5-
providedIn: 'root'
8+
providedIn: 'root',
69
})
710
export class VehicleBrandsService {
811
private apiUrl = 'http://localhost:3000/api/vehicle-brands';
9-
constructor(private readonly http: HttpClient) { }
12+
constructor(private readonly http: HttpClient) {}
1013

11-
getVehicleBrands(): any {
12-
return this.http.get(this.apiUrl);
14+
getVehicleBrands(): Observable<Page<VehicleBrandDto>> {
15+
return this.http.get<Page<VehicleBrandDto>>(this.apiUrl);
1316
}
1417
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { Injectable } from '@angular/core';
22
import { HttpClient } from '@angular/common/http';
33
import { Observable } from 'rxjs';
4+
import { VehicleDto } from '../dto/vehicle';
5+
import { Page } from '../dto/page';
46

57
@Injectable({
6-
providedIn: 'root'
8+
providedIn: 'root',
79
})
810
export class VehiclesService {
911
private apiUrl = 'http://localhost:3000/api/vehicles';
10-
constructor(private readonly http: HttpClient) { }
12+
constructor(private readonly http: HttpClient) {}
1113

12-
getVehicles(): Observable<any> {
13-
return this.http.get(this.apiUrl);
14+
getVehicles(): Observable<Page<VehicleDto>> {
15+
return this.http.get<Page<VehicleDto>>(this.apiUrl);
1416
}
1517
}

0 commit comments

Comments
 (0)