From a4f430bfc48788c62b5954e807bad99db8b0c1bf Mon Sep 17 00:00:00 2001 From: ki8vi Date: Mon, 19 Aug 2024 03:37:34 +0600 Subject: [PATCH 1/2] feat: add trip-details service --- src/app/api/models/trip-detailed.ts | 29 ++++++++++++++++++ .../trip-detailed.service.spec.ts | 19 ++++++++++++ .../trip-detailed.service.ts | 25 ++++++++++++++++ src/app/app.component.ts | 30 +++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 src/app/api/models/trip-detailed.ts create mode 100644 src/app/api/tripDetailedService/trip-detailed.service.spec.ts create mode 100644 src/app/api/tripDetailedService/trip-detailed.service.ts diff --git a/src/app/api/models/trip-detailed.ts b/src/app/api/models/trip-detailed.ts new file mode 100644 index 0000000..7b56bac --- /dev/null +++ b/src/app/api/models/trip-detailed.ts @@ -0,0 +1,29 @@ +interface Price { + [key: string]: number; +} + +interface Segment { + time: string[]; + price: Price; + occupiedSeats: number[]; +} + +export interface RideInfo { + rideId: number; + path: number[]; + carriages: string[]; + schedule: { + segments: Segment[]; + }; +} + +export interface OrderRequest { + rideId: number; + seat: number; + stationStart: number; + stationEnd: number; +} + +export interface OrderId { + id: string; +} diff --git a/src/app/api/tripDetailedService/trip-detailed.service.spec.ts b/src/app/api/tripDetailedService/trip-detailed.service.spec.ts new file mode 100644 index 0000000..42cb338 --- /dev/null +++ b/src/app/api/tripDetailedService/trip-detailed.service.spec.ts @@ -0,0 +1,19 @@ +import { provideHttpClient } from '@angular/common/http'; +import { TestBed } from '@angular/core/testing'; + +import { TripDetailedService } from './trip-detailed.service'; + +describe('TripDetailedService', () => { + let service: TripDetailedService; + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [provideHttpClient()], + }); + service = TestBed.inject(TripDetailedService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/api/tripDetailedService/trip-detailed.service.ts b/src/app/api/tripDetailedService/trip-detailed.service.ts new file mode 100644 index 0000000..93b950d --- /dev/null +++ b/src/app/api/tripDetailedService/trip-detailed.service.ts @@ -0,0 +1,25 @@ +import { HttpClient } from '@angular/common/http'; +import { inject, Injectable } from '@angular/core'; + +import { Observable } from 'rxjs'; + +import { OrderId, OrderRequest, RideInfo } from '../models/trip-detailed'; + +@Injectable({ + providedIn: 'root', +}) +export class TripDetailedService { + private httpClient = inject(HttpClient); + + public getRideInfo(rideId: number): Observable { + return this.httpClient.get(`search/${rideId}`); + } + + public makeRideOrder(order: OrderRequest): Observable { + return this.httpClient.post('order', order); + } + + public cancelRideOrder(orderId: number): Observable { + return this.httpClient.delete(`order/${orderId}`); + } +} diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 5e355e7..caf9eae 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -5,6 +5,8 @@ import { PrimeNGConfig } from 'primeng/api'; import { MessageModule } from 'primeng/message'; import { MessagesModule } from 'primeng/messages'; +import { StationsService } from './api/stationsService/stations.service'; +import { TripDetailedService } from './api/tripDetailedService/trip-detailed.service'; import { HeaderComponent } from './core/components/header/header.component'; import { LocalStorageService } from './core/services/local-storage/local-storage.service'; import { RoutingService } from './core/services/routing/routing.service'; @@ -22,8 +24,36 @@ export class AppComponent implements OnInit { public routingService = inject(RoutingService); + private tripService = inject(TripDetailedService); + private stationsService = inject(StationsService); + public ngOnInit(): void { this.localStorageService.init(); this.primengConfig.ripple = true; + + // this.stationsService.getStations().subscribe({ + // next: (res) => console.log(res), + // error: (err) => console.error(err) + // }) + // const order = { + // rideId: 5, + // seat: 5, + // stationStart: 22, + // stationEnd: 53, + // } + // this.tripService.makeRideOrder(order).subscribe({ + // next: (res) => console.log(res), + // error: (err) => console.error(err) + // }) + // this.tripService.getRideInfo(1).subscribe({ + // next: (res) => console.log(res), + // error: (err) => console.error(err) + // }) + this.tripService.cancelRideOrder(5).subscribe({ + // eslint-disable-next-line no-console + next: (res) => console.log(res), + // eslint-disable-next-line no-console + error: (err) => console.error(err), + }); } } From 77a7a5571aced3bbb08f47628e5b5bb0bbd04325 Mon Sep 17 00:00:00 2001 From: ki8vi Date: Mon, 19 Aug 2024 04:11:37 +0600 Subject: [PATCH 2/2] refactor: replace interfaces --- package.json | 2 +- src/app/api/models/search.ts | 2 +- src/app/api/models/trip-detailed.ts | 10 +--------- src/app/app.component.ts | 30 ----------------------------- 4 files changed, 3 insertions(+), 41 deletions(-) diff --git a/package.json b/package.json index 08db6c2..1d23350 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "@ngrx/router-store": "^18.0.1", "@ngrx/store": "^18.0.2", "@ngrx/store-devtools": "^18.0.1", - "@planess/train-a-backend": "^0.0.4", + "@planess/train-a-backend": "^0.0.5", "@types/geojson": "^7946.0.14", "@types/jest": "^29.5.12", "@types/maplibre-gl": "^1.14.0", diff --git a/src/app/api/models/search.ts b/src/app/api/models/search.ts index a9ddf34..503735e 100644 --- a/src/app/api/models/search.ts +++ b/src/app/api/models/search.ts @@ -2,7 +2,7 @@ interface Price { [key: string]: number; } -interface Segment { +export interface Segment { time: string[]; price: Price; occupiedSeats: number[]; diff --git a/src/app/api/models/trip-detailed.ts b/src/app/api/models/trip-detailed.ts index 7b56bac..6662205 100644 --- a/src/app/api/models/trip-detailed.ts +++ b/src/app/api/models/trip-detailed.ts @@ -1,12 +1,4 @@ -interface Price { - [key: string]: number; -} - -interface Segment { - time: string[]; - price: Price; - occupiedSeats: number[]; -} +import { Segment } from './search'; export interface RideInfo { rideId: number; diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 93fb89c..01d1d52 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -4,8 +4,6 @@ import { RouterOutlet } from '@angular/router'; import { PrimeNGConfig } from 'primeng/api'; import { ToastModule } from 'primeng/toast'; -import { StationsService } from './api/stationsService/stations.service'; -import { TripDetailedService } from './api/tripDetailedService/trip-detailed.service'; import { HeaderComponent } from './core/components/header/header.component'; import { LocalStorageService } from './core/services/local-storage/local-storage.service'; import { RoutingService } from './core/services/routing/routing.service'; @@ -23,36 +21,8 @@ export class AppComponent implements OnInit { public routingService = inject(RoutingService); - private tripService = inject(TripDetailedService); - private stationsService = inject(StationsService); - public ngOnInit(): void { this.localStorageService.init(); this.primengConfig.ripple = true; - - // this.stationsService.getStations().subscribe({ - // next: (res) => console.log(res), - // error: (err) => console.error(err) - // }) - // const order = { - // rideId: 5, - // seat: 5, - // stationStart: 22, - // stationEnd: 53, - // } - // this.tripService.makeRideOrder(order).subscribe({ - // next: (res) => console.log(res), - // error: (err) => console.error(err) - // }) - // this.tripService.getRideInfo(1).subscribe({ - // next: (res) => console.log(res), - // error: (err) => console.error(err) - // }) - this.tripService.cancelRideOrder(5).subscribe({ - // eslint-disable-next-line no-console - next: (res) => console.log(res), - // eslint-disable-next-line no-console - error: (err) => console.error(err), - }); } }