-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
217 additions
and
43 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
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
29 changes: 24 additions & 5 deletions
29
.../src/app/Pages/london-tube/london-tube-singleboard/london-tube-singleboard.component.html
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
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
16 changes: 16 additions & 0 deletions
16
DepartureBoardWeb/ClientApp/src/app/Services/london-tube.service.spec.ts
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,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { LondonTubeService } from './london-tube.service'; | ||
|
||
describe('LondonTubeService', () => { | ||
let service: LondonTubeService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(LondonTubeService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
DepartureBoardWeb/ClientApp/src/app/Services/london-tube.service.ts
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,23 @@ | ||
import { Injectable } from '@angular/core'; | ||
import {HttpClient, HttpParams} from "@angular/common/http"; | ||
import {Observable} from "rxjs"; | ||
import {environment} from "../../environments/environment"; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class LondonTubeService { | ||
|
||
constructor(private http: HttpClient) {} | ||
|
||
getDepartures(stopCode: string, count: number = 4): Observable<object[]> { | ||
const url = "/api/TubeDepartures"; | ||
|
||
let params = new HttpParams().append("code", stopCode); | ||
params = params.append("count", count.toString()); | ||
|
||
return this.http.get<object[]>(environment.apiBaseUrl + url, { | ||
params: params, | ||
}); | ||
} | ||
} |
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,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using BusDataAPI; | ||
using BusDataAPI.DataSource; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace DepartureBoardWeb.Controllers; | ||
|
||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class TubeDeparturesController : Controller | ||
{ | ||
private readonly IMemoryCache _cache; | ||
|
||
public TubeDeparturesController(IMemoryCache cache) | ||
{ | ||
_cache = cache; | ||
} | ||
|
||
[HttpGet] | ||
public List<BusDeparture> GetTubeLiveDepartures(string code, int? count) | ||
{ | ||
var cacheEntry = _cache.GetOrCreate($"{code}_{count}", entry => | ||
{ | ||
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(30); | ||
return GetTubeDepartures(code, count); | ||
}); | ||
return cacheEntry; | ||
} | ||
|
||
private List<BusDeparture> GetTubeDepartures(string code, int? count) | ||
{ | ||
TflApi api = new TflApi(); | ||
var departures = api.GetLiveDepartures(code); | ||
if (count.HasValue) | ||
departures = departures.Take(count.Value).ToList(); | ||
|
||
foreach (BusDeparture busDeparture in departures) | ||
{ | ||
if (busDeparture.Destination.Contains(" Underground Station")) | ||
busDeparture.Destination = busDeparture.Destination.Replace(" Underground Station", ""); | ||
} | ||
|
||
return departures; | ||
} | ||
|
||
[HttpGet("search")] | ||
public List<TflApi.StopPoint> Search(string query) | ||
{ | ||
var stations = _cache.GetOrCreate("tube_stations", entry => | ||
{ | ||
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(3); | ||
TflApi api = new TflApi(); | ||
return api.GetAllStations(); | ||
}); | ||
|
||
return stations; | ||
} | ||
} |