sharee.bike-App/TINKLib/Model/Connector/Query/CachedQueryLoggedIn.cs

160 lines
7.7 KiB
C#
Raw Permalink Normal View History

2021-06-26 20:57:55 +02:00
using System;
2021-05-13 20:03:07 +02:00
using System.Linq;
using System.Threading.Tasks;
using TINK.Model.Bike;
using TINK.Model.Services.CopriApi;
2021-06-26 20:57:55 +02:00
using TINK.Repository;
2021-05-13 20:03:07 +02:00
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()
{
2021-06-26 20:57:55 +02:00
var stationsResponse = await server.GetStations();
2021-05-13 20:03:07 +02:00
2021-06-26 20:57:55 +02:00
if (stationsResponse.Source == typeof(CopriCallsMonkeyStore)
|| stationsResponse.Exception != null)
2021-05-13 20:03:07 +02:00
{
// Stations were read from cache ==> get bikes availbalbe and occupied from cache as well to avoid inconsistencies
return new Result<StationsAndBikesContainer>(
2021-06-26 20:57:55 +02:00
stationsResponse.Source,
2021-05-13 20:03:07 +02:00
new StationsAndBikesContainer(
2021-06-26 20:57:55 +02:00
stationsResponse.Response.GetStationsAllMutable(),
2021-05-13 20:03:07 +02:00
UpdaterJSON.GetBikesAll(
(await server.GetBikesAvailable(true)).Response,
(await server.GetBikesOccupied(true)).Response,
Mail,
DateTimeProvider)),
2021-06-26 20:57:55 +02:00
stationsResponse.Exception);
2021-05-13 20:03:07 +02:00
}
2021-06-26 20:57:55 +02:00
var bikesAvailableResponse = await server.GetBikesAvailable();
if (bikesAvailableResponse.Source == typeof(CopriCallsMonkeyStore)
|| bikesAvailableResponse.Exception != null)
2021-05-13 20:03:07 +02:00
{
// Bikes avilable were read from cache ==> get bikes occupied from cache as well to avoid inconsistencies
return new Result<StationsAndBikesContainer>(
2021-06-26 20:57:55 +02:00
bikesAvailableResponse.Source,
2021-05-13 20:03:07 +02:00
new StationsAndBikesContainer(
(await server.GetStations(true)).Response.GetStationsAllMutable(),
2021-06-26 20:57:55 +02:00
UpdaterJSON.GetBikesAll(bikesAvailableResponse.Response,
2021-05-13 20:03:07 +02:00
(await server.GetBikesOccupied(true)).Response,
Mail,
DateTimeProvider)),
2021-06-26 20:57:55 +02:00
bikesAvailableResponse.Exception);
2021-05-13 20:03:07 +02:00
}
2021-06-26 20:57:55 +02:00
var bikesOccupiedResponse = await server.GetBikesOccupied();
if (bikesOccupiedResponse.Source == typeof(CopriCallsMonkeyStore)
|| bikesOccupiedResponse.Exception != null)
2021-05-13 20:03:07 +02:00
{
// Bikes occupied were read from cache ==> get bikes available from cache as well to avoid inconsistencies
return new Result<StationsAndBikesContainer>(
2021-06-26 20:57:55 +02:00
bikesOccupiedResponse.Source,
2021-05-13 20:03:07 +02:00
new StationsAndBikesContainer(
(await server.GetStations(true)).Response.GetStationsAllMutable(),
UpdaterJSON.GetBikesAll(
(await server.GetBikesAvailable(true)).Response,
2021-06-26 20:57:55 +02:00
bikesOccupiedResponse.Response,
2021-05-13 20:03:07 +02:00
Mail,
DateTimeProvider)),
2021-06-26 20:57:55 +02:00
bikesOccupiedResponse.Exception);
2021-05-13 20:03:07 +02:00
}
// Both types bikes could read from copri => update cache
2021-06-26 20:57:55 +02:00
server.AddToCache(stationsResponse);
server.AddToCache(bikesAvailableResponse);
server.AddToCache(bikesOccupiedResponse);
2021-05-13 20:03:07 +02:00
2021-06-26 20:57:55 +02:00
var exceptions = new[] { stationsResponse?.Exception, bikesAvailableResponse?.Exception, bikesOccupiedResponse?.Exception }.Where(x => x != null).ToArray();
2021-05-13 20:03:07 +02:00
2021-06-26 20:57:55 +02:00
var stationsMutable = stationsResponse.Response.GetStationsAllMutable();
var bikesMutable = UpdaterJSON.GetBikesAll(
bikesAvailableResponse.Response,
bikesOccupiedResponse.Response,
2021-05-13 20:03:07 +02:00
Mail,
2021-06-26 20:57:55 +02:00
DateTimeProvider);
return new Result<StationsAndBikesContainer>(
stationsResponse.Source,
new StationsAndBikesContainer(stationsMutable, bikesMutable),
2021-05-13 20:03:07 +02:00
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);
}
}
}