using System; using System.Collections.Generic; using System.Threading.Tasks; using Serilog; using ShareeBike.Model.Bikes; using ShareeBike.Model.Connector.Updater; using ShareeBike.Model.Services.CopriApi; using ShareeBike.Repository; using ShareeBike.Services.CopriApi; using BikeInfo = ShareeBike.Model.Bikes.BikeInfoNS.BC.BikeInfo; namespace ShareeBike.Model.Connector { /// Provides query functionality from cache without login. public class Query : Base, IQuery { /// Cached copri server. private readonly ICopriServer server; /// Constructs a copri query object. /// Server which implements communication. public Query(ICopriServerBase copriServer) : base(copriServer) { server = copriServer as ICopriServer; if (server == null) { throw new ArgumentException($"Copri server is not of expected type. Type detected is {copriServer.GetType()}."); } } /// Gets all stations including positions. public async Task> GetBikesAndStationsAsync() { var stationsAllResponse = await server.GetStationsAsync(); return new Result( typeof(CopriCallsMonkeyStore), new StationsAndBikesContainer( stationsAllResponse.GetStationsAllMutable(), new BikeCollection() /* There are no bikes occupied because user is not logged in. */), stationsAllResponse.GetGeneralData()); } /// Gets bikes occupied. /// Collection of bikes. public async Task> GetBikesOccupiedAsync() { Log.ForContext().Error("Unexpected call to get be bikes occupied detected. No user is logged in."); return new Result( typeof(CopriCallsMonkeyStore), await Task.FromResult(new BikeCollection(new Dictionary())), new GeneralData(), new Exception("Abfrage der reservierten/ gebuchten Räder fehlgeschlagen. Kein Benutzer angemeldet.")); } /// Gets bikes occupied. /// Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host. /// Id of station which is used for filtering bikes. Null if no filtering should be applied. /// Id of bike which is used for filtering bikes. Null if no filtering should be applied. /// Collection of bikes. public async Task> GetBikesAsync(Uri operatorUri = null, string stationId = null, string bikeId = null) { var bikesAvailableResponse = await server.GetBikesAvailableAsync(operatorUri, stationId, bikeId); return new Result( typeof(CopriCallsMonkeyStore), bikesAvailableResponse != null ? bikesAvailableResponse.GetBikesAvailable(Bikes.BikeInfoNS.BC.DataSource.Cache) : await Task.FromResult(new BikeCollection(new Dictionary())), bikesAvailableResponse?.GetGeneralData()); } } }