-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from Kleostro/feat/tu-01-43/implement-search-api
feat(tu-01-43): implement search service
- Loading branch information
Showing
4 changed files
with
98 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SearchResponse> { | ||
const params = this.generateHttpParams(inputParams); | ||
return this.httpClient.get<SearchResponse>('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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,3 @@ export class SignUpService { | |
return this.httpClient.post('signup', userData); | ||
} | ||
} | ||
|
||
/* Example of using in component | ||
this.signUpService.signUp({ email: '[email protected]', password: 'Test-password' }).subscribe({ | ||
next: () => navigateByUrl('/login'), -> successfull registered | ||
error: (err: OverriddenHttpErrorResponse) => { | ||
console.error(err.error.message); -> errors | ||
}, | ||
}); | ||
*/ |