sharee.bike-App/SharedBusinessLogic/Repository/CopriCallsHelper.cs

68 lines
1.9 KiB
C#
Raw Permalink Normal View History

2024-04-09 12:53:23 +02:00
using ShareeBike.Repository.Response;
using ShareeBike.Repository;
2023-11-21 15:26:57 +01:00
using System.Linq;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
2024-04-09 12:53:23 +02:00
namespace SharedBusinessLogic.Tests.Framework.Repository
2023-11-21 15:26:57 +01:00
{
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;
}
2024-04-09 12:53:23 +02:00
var response = CopriCallsStatic.DeserializeResponse<BikesAvailableResponse>(
$"{{ \"{nameof(VersionindependentResponse.shareejson)}\" : {JsonConvert.SerializeObject(bikes)} }}");
2023-11-21 15:26:57 +01:00
2024-04-09 12:53:23 +02:00
response.bikes.Clear();
2023-11-21 15:26:57 +01:00
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;
}
2024-04-09 12:53:23 +02:00
var response = CopriCallsStatic.DeserializeResponse<BikesAvailableResponse>(
$"{{ \"{nameof(VersionindependentResponse.shareejson)}\" : {JsonConvert.SerializeObject(bikes)} }}");
2023-11-21 15:26:57 +01:00
2024-04-09 12:53:23 +02:00
response.bikes.Clear();
2023-11-21 15:26:57 +01:00
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;
}
}
}