From e1a881346e432082ba74feafa13ec28e3150835d Mon Sep 17 00:00:00 2001 From: ki8vi Date: Mon, 19 Aug 2024 01:42:10 +0600 Subject: [PATCH] feat: add search service --- src/app/api/models/search.ts | 52 +++++++++++++++++++ .../api/searchService/search.service.spec.ts | 19 +++++++ src/app/api/searchService/search.service.ts | 27 ++++++++++ src/app/api/signUpService/sign-up.service.ts | 9 ---- 4 files changed, 98 insertions(+), 9 deletions(-) create mode 100644 src/app/api/models/search.ts create mode 100644 src/app/api/searchService/search.service.spec.ts create mode 100644 src/app/api/searchService/search.service.ts diff --git a/src/app/api/models/search.ts b/src/app/api/models/search.ts new file mode 100644 index 0000000..a9ddf34 --- /dev/null +++ b/src/app/api/models/search.ts @@ -0,0 +1,52 @@ +interface Price { + [key: string]: number; +} + +interface Segment { + time: string[]; + price: Price; + occupiedSeats: number[]; +} + +interface Schedule { + rideId: number; + segments: Segment[]; + time: string[]; + occupiedSeats: number[]; +} + +interface Route { + id: number; + path: number[]; + carriages: string[]; + schedule: Schedule[]; + price: Price; +} + +export interface SearchParams { + fromLatitude: number; + fromLongitude: number; + toLatitude: number; + toLongitude: number; + time?: number; +} + +export interface SearchResponse { + from: { + stationId: number; + city: string; + geolocation: { + latitude: number; + longitude: number; + }; + }; + to: { + stationId: number; + city: string; + geolocation: { + latitude: number; + longitude: number; + }; + }; + routes: Route[]; +} diff --git a/src/app/api/searchService/search.service.spec.ts b/src/app/api/searchService/search.service.spec.ts new file mode 100644 index 0000000..48d2178 --- /dev/null +++ b/src/app/api/searchService/search.service.spec.ts @@ -0,0 +1,19 @@ +import { provideHttpClient } from '@angular/common/http'; +import { TestBed } from '@angular/core/testing'; + +import { SearchService } from './search.service'; + +describe('SearchService', () => { + let service: SearchService; + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [provideHttpClient()], + }); + service = TestBed.inject(SearchService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/api/searchService/search.service.ts b/src/app/api/searchService/search.service.ts new file mode 100644 index 0000000..1ea237f --- /dev/null +++ b/src/app/api/searchService/search.service.ts @@ -0,0 +1,27 @@ +import { HttpClient, HttpParams } from '@angular/common/http'; +import { inject, Injectable } from '@angular/core'; + +import { Observable } from 'rxjs'; + +import { SearchParams, SearchResponse } from '../models/search'; + +@Injectable({ + providedIn: 'root', +}) +export class SearchService { + private httpClient = inject(HttpClient); + + public search(inputParams: SearchParams): Observable { + const params = this.generateHttpParams(inputParams); + return this.httpClient.get('search', { params }); + } + + private generateHttpParams(params: SearchParams): HttpParams { + return Object.entries(params).reduce((httpParams, [key, value]) => { + if (typeof value === 'number') { + return httpParams.append(key, value.toString()); + } + return httpParams; + }, new HttpParams()); + } +} diff --git a/src/app/api/signUpService/sign-up.service.ts b/src/app/api/signUpService/sign-up.service.ts index 726d27a..593baf2 100644 --- a/src/app/api/signUpService/sign-up.service.ts +++ b/src/app/api/signUpService/sign-up.service.ts @@ -15,12 +15,3 @@ export class SignUpService { return this.httpClient.post('signup', userData); } } - -/* Example of using in component - this.signUpService.signUp({ email: 'test@test.com', password: 'Test-password' }).subscribe({ - next: () => navigateByUrl('/login'), -> successfull registered - error: (err: OverriddenHttpErrorResponse) => { - console.error(err.error.message); -> errors - }, - }); -*/