2021-05-13 20:03:07 +02:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
2021-06-26 20:57:55 +02:00
|
|
|
|
namespace TINK.Repository.Exception
|
2021-05-13 20:03:07 +02:00
|
|
|
|
{
|
|
|
|
|
/// <summary> Handles booking request which fail due to too many bikes requested/ booked.</summary>
|
|
|
|
|
public class BookingDeclinedException : InvalidResponseException
|
|
|
|
|
{
|
|
|
|
|
/// <summary> Holds error description if user/ password combination is not valid. </summary>
|
|
|
|
|
public const string BOOKING_FAILURE_STATUS_MESSAGE_UPPERCASE = "(OK: BOOKING_REQUEST DECLINED\\. MAX COUNT OF )([0-9]+)( OCCUPIED BIKES HAS BEEN REACHED)";
|
|
|
|
|
|
|
|
|
|
/// <summary> Prevents invalid use of exception. </summary>
|
|
|
|
|
private BookingDeclinedException() : base(typeof(BookingDeclinedException).Name)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary> Prevents invalid use of exception. </summary>
|
|
|
|
|
public BookingDeclinedException(int maxBikesCount) : base(typeof(BookingDeclinedException).Name)
|
|
|
|
|
{
|
|
|
|
|
MaxBikesCount = maxBikesCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsBookingDeclined(string responseState, out BookingDeclinedException exception)
|
|
|
|
|
{
|
|
|
|
|
// Check if there are too many bikes requested/ booked.
|
|
|
|
|
var match = Regex.Match(
|
2022-08-30 15:42:25 +02:00
|
|
|
|
responseState.ToUpper(),
|
2021-05-13 20:03:07 +02:00
|
|
|
|
BOOKING_FAILURE_STATUS_MESSAGE_UPPERCASE);
|
2022-08-30 15:42:25 +02:00
|
|
|
|
if (match.Groups.Count != 4
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|| !int.TryParse(match.Groups[2].ToString(), out int maxBikesCount))
|
|
|
|
|
{
|
|
|
|
|
exception = null;
|
2022-08-30 15:42:25 +02:00
|
|
|
|
return false;
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exception = new BookingDeclinedException(maxBikesCount);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary> Holds the maximum count of bikes allowed to reserve/ book.</summary>
|
|
|
|
|
public int MaxBikesCount { get; private set; }
|
|
|
|
|
}
|
|
|
|
|
}
|