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

87 lines
3 KiB
C#
Raw Normal View History

2021-05-13 20:03:07 +02:00
using System;
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;
2021-05-13 20:03:07 +02:00
namespace TINK.Model.Connector
{
2022-09-06 16:08:19 +02:00
/// <summary> Provides query functionality for a logged in user. </summary>
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)
{
throw new ArgumentException($"Copri server is not of expected typ. Type detected is {copriServer.GetType()}.");
}
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
2022-09-06 16:08:19 +02:00
/// <summary> Gets all stations including postions.</summary>
public async Task<Result<StationsAndBikesContainer>> GetBikesAndStationsAsync()
{
var stationResponse = await server.GetStationsAsync();
var bikesAvailableResponse = await server.GetBikesAvailableAsync();
var bikesOccupiedResponse = await server.GetBikesOccupiedAsync();
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(),
UpdaterJSON.GetBikesAll(
bikesAvailableResponse?.bikes?.Values,
bikesOccupiedResponse?.bikes_occupied?.Values,
Mail,
DateTimeProvider)),
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),
UpdaterJSON.GetBikesAll(
bikesFeedbackRequired.bikes?.Values?.Select(bike => bike)?.Where(bike => bike.GetState() == State.InUseStateEnum.FeedbackPending),
bikesOccupiedResponse?.bikes_occupied?.Values,
Mail,
DateTimeProvider),
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),
UpdaterJSON.GetBikesAll(
bikesAvailableResponse?.bikes?.Values,
bikesOccupiedResponse?.bikes_occupied?.Values,
Mail,
DateTimeProvider),
bikesAvailableResponse.GetGeneralData());
}
}
2021-05-13 20:03:07 +02:00
}