mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-16 15:16:34 +01:00
159 lines
7.8 KiB
C#
159 lines
7.8 KiB
C#
using MonkeyCache.FileStore;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using TINK.Model.Bike;
|
|
using TINK.Model.Services.CopriApi;
|
|
using TINK.Model.Repository;
|
|
|
|
namespace TINK.Model.Connector
|
|
{
|
|
/// <summary> Provides query functionality for a logged in user. </summary>
|
|
public class CachedQueryLoggedIn : BaseLoggedIn, IQuery
|
|
{
|
|
/// <summary> Cached copri server. </summary>
|
|
private readonly ICachedCopriServer server;
|
|
|
|
/// <summary>Constructs a copri query object.</summary>
|
|
/// <param name="p_oCopriServer">Server which implements communication.</param>
|
|
public CachedQueryLoggedIn(ICopriServerBase p_oCopriServer,
|
|
string p_strSessionCookie,
|
|
string p_strMail,
|
|
Func<DateTime> p_oDateTimeProvider) : base(p_oCopriServer, p_strSessionCookie, p_strMail, p_oDateTimeProvider)
|
|
{
|
|
server = p_oCopriServer as ICachedCopriServer;
|
|
if (server == null)
|
|
{
|
|
throw new ArgumentException($"Copri server is not of expected typ. Type detected is {p_oCopriServer.GetType()}.");
|
|
}
|
|
}
|
|
|
|
/// <summary> Gets all stations including postions.</summary>
|
|
public async Task<Result<StationsAndBikesContainer>> GetBikesAndStationsAsync()
|
|
{
|
|
var resultStations = await server.GetStations();
|
|
|
|
if (resultStations.Source == typeof(CopriCallsMonkeyStore)
|
|
|| resultStations.Exception != null)
|
|
{
|
|
// Stations were read from cache ==> get bikes availbalbe and occupied from cache as well to avoid inconsistencies
|
|
return new Result<StationsAndBikesContainer>(
|
|
resultStations.Source,
|
|
new StationsAndBikesContainer(
|
|
resultStations.Response.GetStationsAllMutable(),
|
|
UpdaterJSON.GetBikesAll(
|
|
(await server.GetBikesAvailable(true)).Response,
|
|
(await server.GetBikesOccupied(true)).Response,
|
|
Mail,
|
|
DateTimeProvider)),
|
|
resultStations.Exception);
|
|
}
|
|
|
|
var l_oBikesAvailableResponse = await server.GetBikesAvailable();
|
|
if (l_oBikesAvailableResponse.Source == typeof(CopriCallsMonkeyStore)
|
|
|| l_oBikesAvailableResponse.Exception != null)
|
|
{
|
|
// Bikes avilable were read from cache ==> get bikes occupied from cache as well to avoid inconsistencies
|
|
return new Result<StationsAndBikesContainer>(
|
|
l_oBikesAvailableResponse.Source,
|
|
new StationsAndBikesContainer(
|
|
(await server.GetStations(true)).Response.GetStationsAllMutable(),
|
|
UpdaterJSON.GetBikesAll(l_oBikesAvailableResponse.Response,
|
|
(await server.GetBikesOccupied(true)).Response,
|
|
Mail,
|
|
DateTimeProvider)),
|
|
l_oBikesAvailableResponse.Exception);
|
|
}
|
|
|
|
var l_oBikesOccupiedResponse = await server.GetBikesOccupied();
|
|
if (l_oBikesOccupiedResponse.Source == typeof(CopriCallsMonkeyStore)
|
|
|| l_oBikesOccupiedResponse.Exception != null)
|
|
{
|
|
// Bikes occupied were read from cache ==> get bikes available from cache as well to avoid inconsistencies
|
|
return new Result<StationsAndBikesContainer>(
|
|
l_oBikesOccupiedResponse.Source,
|
|
new StationsAndBikesContainer(
|
|
(await server.GetStations(true)).Response.GetStationsAllMutable(),
|
|
UpdaterJSON.GetBikesAll(
|
|
(await server.GetBikesAvailable(true)).Response,
|
|
l_oBikesOccupiedResponse.Response,
|
|
Mail,
|
|
DateTimeProvider)),
|
|
l_oBikesOccupiedResponse.Exception);
|
|
}
|
|
|
|
// Both types bikes could read from copri => update cache
|
|
server.AddToCache(resultStations);
|
|
server.AddToCache(l_oBikesAvailableResponse);
|
|
server.AddToCache(l_oBikesOccupiedResponse);
|
|
|
|
var exceptions = new[] { resultStations?.Exception, l_oBikesAvailableResponse?.Exception, l_oBikesOccupiedResponse?.Exception }.Where(x => x != null).ToArray();
|
|
|
|
return new Result<StationsAndBikesContainer>(
|
|
resultStations.Source,
|
|
new StationsAndBikesContainer(
|
|
resultStations.Response.GetStationsAllMutable(),
|
|
UpdaterJSON.GetBikesAll(
|
|
l_oBikesAvailableResponse.Response,
|
|
l_oBikesOccupiedResponse.Response,
|
|
Mail,
|
|
DateTimeProvider)),
|
|
exceptions.Length > 0 ? new AggregateException(exceptions) : null);
|
|
}
|
|
|
|
/// <summary> Gets bikes occupied. </summary>
|
|
/// <returns>Collection of bikes.</returns>
|
|
public async Task<Result<BikeCollection>> GetBikesOccupiedAsync()
|
|
{
|
|
var result = await server.GetBikesOccupied();
|
|
server.AddToCache(result);
|
|
return new Result<BikeCollection>(result.Source, result.Response.GetBikesOccupied(Mail, DateTimeProvider), result.Exception);
|
|
}
|
|
|
|
/// <summary> Gets bikes available and bikes occupied. </summary>
|
|
/// <returns>Collection of bikes.</returns>
|
|
public async Task<Result<BikeCollection>> GetBikesAsync()
|
|
{
|
|
var l_oBikesAvailableResponse = await server.GetBikesAvailable();
|
|
|
|
if (l_oBikesAvailableResponse.Source == typeof(CopriCallsMonkeyStore)
|
|
|| l_oBikesAvailableResponse.Exception != null)
|
|
{
|
|
// Bikes avilable were read from cache ==> get bikes occupied from cache as well to avoid inconsistencies
|
|
return new Result<BikeCollection>(
|
|
l_oBikesAvailableResponse.Source,
|
|
UpdaterJSON.GetBikesAll(
|
|
l_oBikesAvailableResponse.Response,
|
|
(await server.GetBikesOccupied(true)).Response,
|
|
Mail,
|
|
DateTimeProvider),
|
|
l_oBikesAvailableResponse.Exception);
|
|
}
|
|
|
|
var l_oBikesOccupiedResponse = await server.GetBikesOccupied();
|
|
if (l_oBikesOccupiedResponse.Source == typeof(CopriCallsMonkeyStore)
|
|
|| l_oBikesOccupiedResponse.Exception != null)
|
|
{
|
|
// Bikes occupied were read from cache ==> get bikes available from cache as well to avoid inconsistencies
|
|
return new Result<BikeCollection>(
|
|
l_oBikesOccupiedResponse.Source,
|
|
UpdaterJSON.GetBikesAll(
|
|
(await server.GetBikesAvailable(true)).Response,
|
|
l_oBikesOccupiedResponse.Response,
|
|
Mail,
|
|
DateTimeProvider),
|
|
l_oBikesOccupiedResponse.Exception);
|
|
|
|
}
|
|
|
|
// Both types bikes could read from copri => update cache
|
|
server.AddToCache(l_oBikesAvailableResponse);
|
|
server.AddToCache(l_oBikesOccupiedResponse);
|
|
|
|
return new Result<BikeCollection>(
|
|
l_oBikesAvailableResponse.Source,
|
|
UpdaterJSON.GetBikesAll(l_oBikesAvailableResponse.Response, l_oBikesOccupiedResponse.Response, Mail, DateTimeProvider),
|
|
l_oBikesAvailableResponse.Exception != null || l_oBikesOccupiedResponse.Exception != null ? new AggregateException(new[] { l_oBikesAvailableResponse.Exception, l_oBikesOccupiedResponse.Exception }) : null);
|
|
}
|
|
}
|
|
}
|