Skip to content

Commit

Permalink
Merge pull request #68 from Kleostro/feat/tu-01-43/implement-search-api
Browse files Browse the repository at this point in the history
feat(tu-01-43): implement search service
  • Loading branch information
ki8vi authored Aug 18, 2024
2 parents 3715bea + e1a8813 commit ae28397
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 9 deletions.
52 changes: 52 additions & 0 deletions src/app/api/models/search.ts
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[];
}
19 changes: 19 additions & 0 deletions src/app/api/searchService/search.service.spec.ts
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();
});
});
27 changes: 27 additions & 0 deletions src/app/api/searchService/search.service.ts
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());
}
}
9 changes: 0 additions & 9 deletions src/app/api/signUpService/sign-up.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
});
*/

0 comments on commit ae28397

Please sign in to comment.