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

90 lines
3.2 KiB
C#
Raw Normal View History

2023-01-18 14:22:51 +01:00
using System;
2023-05-09 08:47:52 +02:00
using System.Collections.Generic;
2022-08-30 15:42:25 +02:00
using System.Linq;
2021-05-13 20:03:07 +02:00
using System.Threading.Tasks;
2022-08-30 15:42:25 +02:00
using TINK.Model.Bikes;
using TINK.Model.Connector.Updater;
2021-05-13 20:03:07 +02:00
using TINK.Model.Services.CopriApi;
2021-06-26 20:57:55 +02:00
using TINK.Repository;
2023-05-09 08:47:52 +02:00
using TINK.Repository.Response;
2021-05-13 20:03:07 +02:00
namespace TINK.Model.Connector
{
2023-01-18 14:22:51 +01:00
/// <summary> Provides query functionality from cache for a logged in user. </summary>
2022-09-06 16:08:19 +02:00
public class QueryLoggedIn : BaseLoggedIn, IQuery
{
/// <summary> 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>
/// <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)
{
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
2022-09-06 16:08:19 +02:00
server = copriServer as ICopriServer;
}
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 stationResponse = 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),
new StationsAndBikesContainer(
stationResponse.GetStationsAllMutable(),
2023-01-18 14:22:51 +01:00
BikeCollectionFactory.GetBikesAll(
2023-05-09 08:47:52 +02:00
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,
2022-09-06 16:08:19 +02:00
Mail,
2023-01-18 14:22:51 +01:00
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache)),
2022-09-06 16:08:19 +02:00
stationResponse.GetGeneralData());
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <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();
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
return new Result<BikeCollection>(
typeof(CopriCallsMonkeyStore),
2023-01-18 14:22:51 +01:00
BikeCollectionFactory.GetBikesAll(
2022-09-06 16:08:19 +02:00
bikesFeedbackRequired.bikes?.Values?.Select(bike => bike)?.Where(bike => bike.GetState() == State.InUseStateEnum.FeedbackPending),
bikesOccupiedResponse?.bikes_occupied?.Values,
Mail,
2023-01-18 14:22:51 +01:00
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache),
2022-09-06 16:08:19 +02:00
bikesOccupiedResponse.GetGeneralData());
}
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
/// <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();
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
return new Result<BikeCollection>(
typeof(CopriCallsMonkeyStore),
2023-01-18 14:22:51 +01:00
BikeCollectionFactory.GetBikesAll(
2022-09-06 16:08:19 +02:00
bikesAvailableResponse?.bikes?.Values,
bikesOccupiedResponse?.bikes_occupied?.Values,
Mail,
2023-01-18 14:22:51 +01:00
DateTimeProvider,
Bikes.BikeInfoNS.BC.DataSource.Cache),
2022-09-06 16:08:19 +02:00
bikesAvailableResponse.GetGeneralData());
}
}
2021-05-13 20:03:07 +02:00
}