Skip to content

Commit

Permalink
feat: add schedule service
Browse files Browse the repository at this point in the history
  • Loading branch information
ki8vi committed Aug 19, 2024
1 parent 5ab51c6 commit 1bcf66d
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/app/api/constants/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const ROUTE_ENDPOINT = 'route';
export const RIDE_ENDPOINT = 'ride';
export const SEARCH_ENDPOINT = 'search';
15 changes: 15 additions & 0 deletions src/app/api/models/schedule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Route, Schedule, Segment } from './search';

type PickedRoute = Pick<Route, 'carriages' | 'id' | 'path'>;

type OmitedSegment = Omit<Segment, 'occupiedSeats'>;

type CustomSchedule = Pick<Schedule, 'rideId'> & { segments: OmitedSegment[] };

export interface RouteInfo extends PickedRoute {
schedule: CustomSchedule[];
}

export interface RideBody {
segments: OmitedSegment[];
}
2 changes: 1 addition & 1 deletion src/app/api/models/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface Segment {
occupiedSeats: number[];
}

interface Schedule {
export interface Schedule {
rideId: number;
segments: Segment[];
time: string[];
Expand Down
10 changes: 5 additions & 5 deletions src/app/api/routeService/route.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ import { inject, Injectable } from '@angular/core';

import { Observable } from 'rxjs';

import { ROUTE_ENDPOINT } from '../constants/constants';
import { RouteBody, RouteId, RouteResponse } from '../models/route';

@Injectable({
providedIn: 'root',
})
export class RouteService {
private httpClient = inject(HttpClient);
private ROUTE_ENDPOINT = 'route';

public getAllRoutes(): Observable<RouteResponse[]> {
return this.httpClient.get<RouteResponse[]>(this.ROUTE_ENDPOINT);
return this.httpClient.get<RouteResponse[]>(ROUTE_ENDPOINT);
}

public createRoute(newRoute: RouteBody): Observable<RouteId> {
return this.httpClient.post<RouteId>(this.ROUTE_ENDPOINT, newRoute);
return this.httpClient.post<RouteId>(ROUTE_ENDPOINT, newRoute);
}

public updateRoute(id: number, route: RouteBody): Observable<RouteId> {
return this.httpClient.put<RouteId>(`${this.ROUTE_ENDPOINT}/${id}`, route);
return this.httpClient.put<RouteId>(`${ROUTE_ENDPOINT}/${id}`, route);
}

public deleteRoute(id: number): Observable<object> {
return this.httpClient.delete<object>(`${this.ROUTE_ENDPOINT}/${id}`);
return this.httpClient.delete<object>(`${ROUTE_ENDPOINT}/${id}`);
}
}
19 changes: 19 additions & 0 deletions src/app/api/scheduleService/schedule.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 { ScheduleService } from './schedule.service';

describe('ScheduleService', () => {
let service: ScheduleService;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [provideHttpClient()],
});
service = TestBed.inject(ScheduleService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
27 changes: 27 additions & 0 deletions src/app/api/scheduleService/schedule.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { HttpClient } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';

import { Observable } from 'rxjs';

import { RIDE_ENDPOINT, ROUTE_ENDPOINT } from '../constants/constants';
import { RouteId } from '../models/route';
import { RideBody, RouteInfo } from '../models/schedule';

@Injectable({
providedIn: 'root',
})
export class ScheduleService {
private httpClient = inject(HttpClient);

public getRouteById(id: number): Observable<RouteInfo> {
return this.httpClient.get<RouteInfo>(`${ROUTE_ENDPOINT}/${id}`);
}

public createRide(routeId: number, rideData: RideBody): Observable<RouteId> {
return this.httpClient.post<RouteId>(`${ROUTE_ENDPOINT}/${routeId}/${RIDE_ENDPOINT}`, rideData);
}

public updateRide(routeId: number, rideId: number, rideData: RideBody): Observable<object> {
return this.httpClient.put<object>(`${ROUTE_ENDPOINT}/${routeId}/${RIDE_ENDPOINT}/${rideId}`, rideData);
}
}
3 changes: 2 additions & 1 deletion src/app/api/searchService/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { inject, Injectable } from '@angular/core';

import { Observable } from 'rxjs';

import { SEARCH_ENDPOINT } from '../constants/constants';
import { SearchParams, SearchResponse } from '../models/search';

@Injectable({
Expand All @@ -13,7 +14,7 @@ export class SearchService {

public search(inputParams: SearchParams): Observable<SearchResponse> {
const params = this.generateHttpParams(inputParams);
return this.httpClient.get<SearchResponse>('search', { params });
return this.httpClient.get<SearchResponse>(SEARCH_ENDPOINT, { params });
}

private generateHttpParams(params: SearchParams): HttpParams {
Expand Down
3 changes: 2 additions & 1 deletion src/app/api/tripDetailedService/trip-detailed.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { inject, Injectable } from '@angular/core';

import { Observable } from 'rxjs';

import { SEARCH_ENDPOINT } from '../constants/constants';
import { RideInfo } from '../models/trip-detailed';

@Injectable({
Expand All @@ -12,6 +13,6 @@ export class TripDetailedService {
private httpClient = inject(HttpClient);

public getRideInfo(rideId: number): Observable<RideInfo> {
return this.httpClient.get<RideInfo>(`search/${rideId}`);
return this.httpClient.get<RideInfo>(`${SEARCH_ENDPOINT}/${rideId}`);
}
}

0 comments on commit 1bcf66d

Please sign in to comment.