mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-06 02:56:32 +01:00
134 lines
3.6 KiB
C#
134 lines
3.6 KiB
C#
|
using System;
|
||
|
using System.Threading.Tasks;
|
||
|
using Serilog;
|
||
|
using ShareeBike.Repository.Exception;
|
||
|
using ShareeBike.Model.Connector;
|
||
|
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||
|
using ShareeBike.Services.CopriApi.Exception;
|
||
|
using ShareeBike.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler;
|
||
|
|
||
|
namespace ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command
|
||
|
{
|
||
|
public static class CancelReservationCommand
|
||
|
{
|
||
|
|
||
|
/// <summary>
|
||
|
/// Possible steps of returning a bike.
|
||
|
/// </summary>
|
||
|
public enum Step
|
||
|
{
|
||
|
CancelReservation,
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Possible steps of returning a bike.
|
||
|
/// </summary>
|
||
|
public enum State
|
||
|
{
|
||
|
WebConnectFailed,
|
||
|
InvalidResponse,
|
||
|
GeneralCancelReservationError,
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Interface to notify view model about steps/ state changes of returning bike process.
|
||
|
/// </summary>
|
||
|
public interface ICancelReservationCommandListener
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Reports current step.
|
||
|
/// </summary>
|
||
|
/// <param name="currentStep">Current step to report.</param>
|
||
|
void ReportStep(Step currentStep);
|
||
|
|
||
|
/// <summary>
|
||
|
/// Reports current state.
|
||
|
/// </summary>
|
||
|
/// <param name="currentState">Current state to report.</param>
|
||
|
/// <param name="message">Message describing the current state.</param>
|
||
|
/// <returns></returns>
|
||
|
Task ReportStateAsync(State currentState, string message);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Get current location.
|
||
|
/// </summary>
|
||
|
/// <param name="listener"></param>
|
||
|
/// <returns></returns>
|
||
|
/// <exception cref="Exception"></exception>
|
||
|
public static async Task InvokeAsync<T>(
|
||
|
IBikeInfoMutable bike,
|
||
|
Func<bool> isConnectedDelegate,
|
||
|
Func<bool, IConnector> connectorFactory,
|
||
|
ICancelReservationCommandListener listener)
|
||
|
{
|
||
|
// Invokes member to notify about step being started.
|
||
|
void InvokeCurrentStep(Step step)
|
||
|
{
|
||
|
if (listener == null)
|
||
|
return;
|
||
|
|
||
|
try
|
||
|
{
|
||
|
listener.ReportStep(step);
|
||
|
}
|
||
|
catch (Exception exception)
|
||
|
{
|
||
|
Log.ForContext<T>().Error("An exception {@exception} was thrown invoking step-action for step {step} ", exception, step);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Invokes member to notify about state change.
|
||
|
async Task InvokeCurrentStateAsync(State state, string message)
|
||
|
{
|
||
|
if (listener == null)
|
||
|
return;
|
||
|
|
||
|
try
|
||
|
{
|
||
|
await listener.ReportStateAsync(state, message);
|
||
|
}
|
||
|
catch (Exception exception)
|
||
|
{
|
||
|
Log.ForContext<T>().Error("An exception {@exception} was thrown invoking state-action for state {state} ", exception, state);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//// Start Action
|
||
|
// Cancel Reservation
|
||
|
InvokeCurrentStep(Step.CancelReservation);
|
||
|
|
||
|
try
|
||
|
{
|
||
|
await connectorFactory(true).Command.DoCancelReservation(bike);
|
||
|
Log.ForContext<T>().Information("User canceled reservation of bike {bikeId} successfully.", bike.Id);
|
||
|
}
|
||
|
catch (Exception exception)
|
||
|
{
|
||
|
Log.ForContext<ReservedDisconnected>().Information("User selected reserved bike {bikeId} but cancel reservation failed.", bike.Id);
|
||
|
if (exception is InvalidAuthorizationResponseException)
|
||
|
{
|
||
|
// Copri response is invalid.
|
||
|
Log.ForContext<T>().Error("Invalid auth. response.");
|
||
|
await InvokeCurrentStateAsync(State.InvalidResponse, exception.Message);
|
||
|
}
|
||
|
else if (exception is WebConnectFailureException
|
||
|
|| exception is RequestNotCachableException)
|
||
|
{
|
||
|
// Copri server is not reachable.
|
||
|
Log.ForContext<T>().Error("Copri server not reachable.");
|
||
|
await InvokeCurrentStateAsync(State.WebConnectFailed, exception.Message);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Log.ForContext<T>().Error("{@exception}", exception);
|
||
|
await InvokeCurrentStateAsync(State.GeneralCancelReservationError, exception.Message);
|
||
|
}
|
||
|
|
||
|
throw;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|