Kenneth Bruen
2 years ago
7 changed files with 118 additions and 1 deletions
@ -0,0 +1,40 @@ |
|||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using Microsoft.AspNetCore.Http; |
||||||
|
using Microsoft.AspNetCore.Mvc; |
||||||
|
using scraper.Models.Itinerary; |
||||||
|
using Server.Services.Interfaces; |
||||||
|
|
||||||
|
namespace Server.Controllers.V3; |
||||||
|
|
||||||
|
[ApiController] |
||||||
|
[ApiExplorerSettings(GroupName = "v3")] |
||||||
|
[Route("/v3/[controller]")]
|
||||||
|
public class ItinerariesController : Controller { |
||||||
|
private IDataManager DataManager { get; } |
||||||
|
private IDatabase Database { get; } |
||||||
|
|
||||||
|
public ItinerariesController(IDataManager dataManager, IDatabase database) { |
||||||
|
this.DataManager = dataManager; |
||||||
|
this.Database = database; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
[HttpGet("")] |
||||||
|
[ProducesResponseType(typeof(IEnumerable<IItinerary>), StatusCodes.Status200OK)] |
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)] |
||||||
|
public async Task<ActionResult<IEnumerable<IItinerary>>> FindItineraries( |
||||||
|
[FromQuery] string from, |
||||||
|
[FromQuery] string to, |
||||||
|
[FromQuery] DateTimeOffset? date |
||||||
|
) { |
||||||
|
var itineraries = await DataManager.FetchItineraries(from, to, date); |
||||||
|
|
||||||
|
if (itineraries == null) { |
||||||
|
return NotFound(); |
||||||
|
} |
||||||
|
|
||||||
|
return Ok(itineraries); |
||||||
|
} |
||||||
|
} |
@ -1,11 +1,14 @@ |
|||||||
using System; |
using System; |
||||||
|
using System.Collections.Generic; |
||||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||||
using InfoferScraper.Models.Train; |
using InfoferScraper.Models.Train; |
||||||
using InfoferScraper.Models.Station; |
using InfoferScraper.Models.Station; |
||||||
|
using scraper.Models.Itinerary; |
||||||
|
|
||||||
namespace Server.Services.Interfaces; |
namespace Server.Services.Interfaces; |
||||||
|
|
||||||
public interface IDataManager { |
public interface IDataManager { |
||||||
public Task<IStationScrapeResult?> FetchStation(string stationName, DateTimeOffset date); |
public Task<IStationScrapeResult?> FetchStation(string stationName, DateTimeOffset date); |
||||||
public Task<ITrainScrapeResult?> FetchTrain(string trainNumber, DateTimeOffset date); |
public Task<ITrainScrapeResult?> FetchTrain(string trainNumber, DateTimeOffset date); |
||||||
|
public Task<IReadOnlyList<IItinerary>?> FetchItineraries(string from, string to, DateTimeOffset? date = null); |
||||||
} |
} |
||||||
|
@ -0,0 +1,33 @@ |
|||||||
|
using System.Collections.Generic; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using MongoDB.Driver; |
||||||
|
|
||||||
|
namespace Server.Utils; |
||||||
|
|
||||||
|
public record IAsyncCusorAsyncEnumerator<T>(IAsyncCursor<T> Cursor) { |
||||||
|
private IEnumerator<T>? enumerator = null; |
||||||
|
|
||||||
|
public T Current => enumerator!.Current; |
||||||
|
|
||||||
|
public async Task<bool> MoveNextAsync() { |
||||||
|
bool result; |
||||||
|
if (enumerator != null) { |
||||||
|
result = enumerator.MoveNext(); |
||||||
|
if (result) return true; |
||||||
|
} |
||||||
|
|
||||||
|
result = await Cursor.MoveNextAsync(); |
||||||
|
if (result) { |
||||||
|
enumerator = Cursor.Current.GetEnumerator(); |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static class IAsyncCursorExtensions { |
||||||
|
public static IAsyncCusorAsyncEnumerator<T> GetAsyncEnumerator<T>(this IAsyncCursor<T> cursor) { |
||||||
|
return new(cursor); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue