mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-12-25 08:36:27 +01:00
89 lines
3.2 KiB
C#
89 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using TINK.Model.Bikes;
|
|
using TINK.Model.Connector.Updater;
|
|
using TINK.Model.Services.CopriApi;
|
|
using TINK.Repository;
|
|
using TINK.Repository.Response;
|
|
|
|
namespace TINK.Model.Connector
|
|
{
|
|
/// <summary> Provides query functionality from cache for a logged in user. </summary>
|
|
public class QueryLoggedIn : BaseLoggedIn, IQuery
|
|
{
|
|
/// <summary> Copri server. </summary>
|
|
private readonly ICopriServer server;
|
|
|
|
/// <summary>Constructs a copri query object.</summary>
|
|
/// <param name="copriServer">Server which implements communication.</param>
|
|
public QueryLoggedIn(ICopriServerBase copriServer,
|
|
string sessionCookie,
|
|
string mail,
|
|
Func<DateTime> dateTimeProvider) : base(copriServer, sessionCookie, mail, dateTimeProvider)
|
|
{
|
|
server = copriServer as ICopriServer;
|
|
if (server == null)
|
|
{
|
|
throw new ArgumentException($"Copri server is not of expected type. Type detected is {copriServer.GetType()}.");
|
|
}
|
|
|
|
server = copriServer as ICopriServer;
|
|
}
|
|
|
|
/// <summary> Gets all stations including positions.</summary>
|
|
public async Task<Result<StationsAndBikesContainer>> GetBikesAndStationsAsync()
|
|
{
|
|
var stationResponse = await server.GetStationsAsync();
|
|
|
|
return new Result<StationsAndBikesContainer>(
|
|
typeof(CopriCallsMonkeyStore),
|
|
new StationsAndBikesContainer(
|
|
stationResponse.GetStationsAllMutable(),
|
|
BikeCollectionFactory.GetBikesAll(
|
|
null, // Bikes available are no more of interest because count of available bikes at each given station is was added to station object.
|
|
stationResponse.bikes_occupied?.Values ?? new Dictionary<string, BikeInfoReservedOrBooked>().Values,
|
|
Mail,
|
|
DateTimeProvider,
|
|
Bikes.BikeInfoNS.BC.DataSource.Cache)),
|
|
stationResponse.GetGeneralData());
|
|
}
|
|
|
|
/// <summary> Gets bikes occupied and bikes for which feedback is required. </summary>
|
|
/// <returns>Collection of bikes.</returns>
|
|
public async Task<Result<BikeCollection>> GetBikesOccupiedAsync()
|
|
{
|
|
var bikesFeedbackRequired = await server.GetBikesAvailableAsync();
|
|
var bikesOccupiedResponse = await server.GetBikesOccupiedAsync();
|
|
|
|
return new Result<BikeCollection>(
|
|
typeof(CopriCallsMonkeyStore),
|
|
BikeCollectionFactory.GetBikesAll(
|
|
bikesFeedbackRequired.bikes?.Values?.Select(bike => bike)?.Where(bike => bike.GetState() == State.InUseStateEnum.FeedbackPending),
|
|
bikesOccupiedResponse?.bikes_occupied?.Values,
|
|
Mail,
|
|
DateTimeProvider,
|
|
Bikes.BikeInfoNS.BC.DataSource.Cache),
|
|
bikesOccupiedResponse.GetGeneralData());
|
|
}
|
|
|
|
/// <summary> Gets bikes available and bikes occupied. </summary>
|
|
/// <returns>Collection of bikes.</returns>
|
|
public async Task<Result<BikeCollection>> GetBikesAsync()
|
|
{
|
|
var bikesAvailableResponse = await server.GetBikesAvailableAsync();
|
|
var bikesOccupiedResponse = await server.GetBikesOccupiedAsync();
|
|
|
|
return new Result<BikeCollection>(
|
|
typeof(CopriCallsMonkeyStore),
|
|
BikeCollectionFactory.GetBikesAll(
|
|
bikesAvailableResponse?.bikes?.Values,
|
|
bikesOccupiedResponse?.bikes_occupied?.Values,
|
|
Mail,
|
|
DateTimeProvider,
|
|
Bikes.BikeInfoNS.BC.DataSource.Cache),
|
|
bikesAvailableResponse.GetGeneralData());
|
|
}
|
|
}
|
|
}
|