sharee.bike-App/SharedBusinessLogic/Repository/CopriCallsHelper.cs
2024-04-09 12:53:23 +02:00

67 lines
1.9 KiB
C#

using ShareeBike.Repository.Response;
using ShareeBike.Repository;
using System.Linq;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
namespace SharedBusinessLogic.Tests.Framework.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 response = CopriCallsStatic.DeserializeResponse<BikesAvailableResponse>(
$"{{ \"{nameof(VersionindependentResponse.shareejson)}\" : {JsonConvert.SerializeObject(bikes)} }}");
response.bikes.Clear();
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 response = CopriCallsStatic.DeserializeResponse<BikesAvailableResponse>(
$"{{ \"{nameof(VersionindependentResponse.shareejson)}\" : {JsonConvert.SerializeObject(bikes)} }}");
response.bikes.Clear();
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;
}
}
}