sharee.bike-App/TestFramework/Repository/CopriCallsHelper.cs
2023-11-21 15:26:57 +01:00

75 lines
2.1 KiB
C#

using TINK.Repository.Response;
using TINK.Repository;
using System.Linq;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
namespace TestFramework.Repository
{
public static class CopriCallsHelper
{
/// <summary>
/// Filters bikes available response by station.
/// </summary>
/// <param name="bikes"></param>
/// <param name="stationId"></param>
/// <returns></returns>
public static BikesAvailableResponse FilterByStation(this BikesAvailableResponse bikes, string stationId)
{
if (string.IsNullOrEmpty(stationId))
{
// If filter is off there is nothing to filter.
return bikes;
}
var bikesText = JsonConvert.SerializeObject(bikes);
var response = CopriCallsStatic.DeserializeResponse<BikesAvailableResponse>($"{{ \"{nameof(VersionindependentResponse.shareejson)}\" : {bikesText} }}");
var bikeIdList = response.bikes.Select(x => x.Value.bike).ToList();
foreach (var bike in bikeIdList)
{
response.bikes.Remove(bike);
};
var allBikes = bikes.bikes.Values.Where(x => x.station == stationId).ToList();
foreach (var bike in allBikes)
{
response.bikes.Add(bike.bike, bike);
}
return response;
}
/// <summary>
/// Filters bikes available response by station.
/// </summary>
/// <param name="bikes"></param>
/// <param name="stationId"></param>
/// <returns></returns>
public static BikesAvailableResponse FilterByBike(this BikesAvailableResponse bikes, string bikeId)
{
if (string.IsNullOrEmpty(bikeId))
{
// If filter is off there is nothing to filter.
return bikes;
}
var bikesText = JsonConvert.SerializeObject(bikes);
var response = CopriCallsStatic.DeserializeResponse<BikesAvailableResponse>($"{{ \"{nameof(VersionindependentResponse.shareejson)}\" : {bikesText} }}");
var bikeIdList = response.bikes.Select(x => x.Value.bike).ToList();
foreach (var bike in bikeIdList)
{
response.bikes.Remove(bike);
};
var allBikes = bikes.bikes.Values.Where(x => Regex.IsMatch(x.bike, bikeId)).ToList();
foreach (var bike in allBikes)
{
response.bikes.Add(bike.bike, bike);
}
return response;
}
}
}