sharee.bike-App/SharedBusinessLogic/Model/Connector/Query/Query.cs

73 lines
3.1 KiB
C#
Raw Permalink Normal View History

2023-01-18 14:22:51 +01:00
using System;
2021-05-13 20:03:07 +02:00
using System.Collections.Generic;
using System.Threading.Tasks;
2022-08-30 15:42:25 +02:00
using Serilog;
2024-04-09 12:53:23 +02:00
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;
2021-05-13 20:03:07 +02:00
2024-04-09 12:53:23 +02:00
namespace ShareeBike.Model.Connector
2021-05-13 20:03:07 +02:00
{
2023-01-18 14:22:51 +01:00
/// <summary> Provides query functionality from cache without login. </summary>
2022-09-06 16:08:19 +02:00
public class Query : Base, IQuery
{
/// <summary> Cached copri server. </summary>
private readonly ICopriServer server;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>Constructs a copri query object.</summary>
2023-01-18 14:22:51 +01:00
/// <param name="copriServer">Server which implements communication.</param>
public Query(ICopriServerBase copriServer) : base(copriServer)
2022-09-06 16:08:19 +02:00
{
2023-01-18 14:22:51 +01:00
server = copriServer as ICopriServer;
2022-09-06 16:08:19 +02:00
if (server == null)
{
2023-05-11 17:39:28 +02:00
throw new ArgumentException($"Copri server is not of expected type. Type detected is {copriServer.GetType()}.");
2022-09-06 16:08:19 +02:00
}
}
2021-05-13 20:03:07 +02:00
2023-04-19 12:14:14 +02:00
/// <summary> Gets all stations including positions.</summary>
2022-09-06 16:08:19 +02:00
public async Task<Result<StationsAndBikesContainer>> GetBikesAndStationsAsync()
{
var stationsAllResponse = await server.GetStationsAsync();
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
return new Result<StationsAndBikesContainer>(
typeof(CopriCallsMonkeyStore),
2023-05-09 08:47:52 +02:00
new StationsAndBikesContainer(
stationsAllResponse.GetStationsAllMutable(),
new BikeCollection() /* There are no bikes occupied because user is not logged in. */),
2022-09-06 16:08:19 +02:00
stationsAllResponse.GetGeneralData());
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Gets bikes occupied. </summary>
/// <returns>Collection of bikes.</returns>
public async Task<Result<BikeCollection>> GetBikesOccupiedAsync()
{
2023-04-19 12:14:14 +02:00
Log.ForContext<Query>().Error("Unexpected call to get be bikes occupied detected. No user is logged in.");
2022-09-06 16:08:19 +02:00
return new Result<BikeCollection>(
typeof(CopriCallsMonkeyStore),
await Task.FromResult(new BikeCollection(new Dictionary<string, BikeInfo>())),
new GeneralData(),
new Exception("Abfrage der reservierten/ gebuchten Räder fehlgeschlagen. Kein Benutzer angemeldet."));
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Gets bikes occupied. </summary>
2023-11-06 12:23:09 +01:00
/// <param name="operatorUri">Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host.</param>
2023-11-21 15:26:57 +01:00
/// <param name="stationId"> Id of station which is used for filtering bikes. Null if no filtering should be applied.</param>
/// <param name="bikeId"> Id of bike which is used for filtering bikes. Null if no filtering should be applied.</param>
2022-09-06 16:08:19 +02:00
/// <returns> Collection of bikes. </returns>
2023-11-21 15:26:57 +01:00
public async Task<Result<BikeCollection>> GetBikesAsync(Uri operatorUri = null, string stationId = null, string bikeId = null)
2022-09-06 16:08:19 +02:00
{
2023-11-21 15:26:57 +01:00
var bikesAvailableResponse = await server.GetBikesAvailableAsync(operatorUri, stationId, bikeId);
2022-09-06 16:08:19 +02:00
return new Result<BikeCollection>(
typeof(CopriCallsMonkeyStore),
2023-11-21 15:26:57 +01:00
bikesAvailableResponse != null
? bikesAvailableResponse.GetBikesAvailable(Bikes.BikeInfoNS.BC.DataSource.Cache)
: await Task.FromResult(new BikeCollection(new Dictionary<string, BikeInfo>())),
bikesAvailableResponse?.GetGeneralData());
2022-09-06 16:08:19 +02:00
}
}
2021-05-13 20:03:07 +02:00
}