mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-05 10:36:30 +01:00
67 lines
1.9 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|