Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions EstateReportingAPI.Client/EstateReportingApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,7 @@ public async Task<List<TodaysSalesValueByHour>> GetTodaysSalesValueByHour(String

public async Task<TodaysSettlement> GetTodaysSettlement(String accessToken, Guid estateId, Guid? merchantId, Guid? operatorId, DateTime comparisonDate, CancellationToken cancellationToken){
TodaysSettlement response = null;



QueryStringBuilder builder = new QueryStringBuilder();
builder.AddParameter("comparisonDate", $"{comparisonDate.Date:yyyy-MM-dd}");
builder.AddParameter("merchantId", merchantId);
Expand Down Expand Up @@ -648,33 +647,62 @@ private String BuildRequestUrl(String route){

public class QueryStringBuilder
{
private Dictionary<string, object> parameters;
private Dictionary<string, object> parameters = new Dictionary<String, Object>();

public QueryStringBuilder()
public QueryStringBuilder AddParameter(string key, Object value)
{
parameters = new Dictionary<string, object>();
this.parameters.Add(key, value);
return this;
}

public QueryStringBuilder AddParameter(string key, object value)
static Dictionary<string, object> FilterDictionary(Dictionary<string, object> inputDictionary)
{
// Check for null values, excluding value types like Guid
if (value != null && !(value is ValueType && Nullable.GetUnderlyingType(value.GetType()) == null))
Dictionary<string, object> result = new Dictionary<string, object>();

foreach (KeyValuePair<String, Object> entry in inputDictionary)
{
parameters[key] = value;
if (entry.Value != null && !IsDefaultValue(entry.Value))
{
result.Add(entry.Key, entry.Value);
}
}
return this;

return result;
}

public string BuildQueryString()
static bool IsDefaultValue<T>(T value)
{
if (parameters.Count == 0)
Object? defaultValue = GetDefault(value.GetType());

if (defaultValue == null && value.GetType() == typeof(String))
{
return string.Empty;
defaultValue = String.Empty;
}
return defaultValue.Equals(value);
}

public static object GetDefault(Type t)
{
Func<object> f = GetDefault<object>;
return f.Method.GetGenericMethodDefinition().MakeGenericMethod(t).Invoke(null, null);
}

var queryString = new StringBuilder();
private static T GetDefault<T>()
{
return default(T);
}

public string BuildQueryString(){
Dictionary<String, Object> filtered = FilterDictionary(this.parameters);

if (filtered.Count == 0)
{
return string.Empty;
}

foreach (var kvp in parameters)
StringBuilder queryString = new StringBuilder();

foreach (KeyValuePair<String, Object> kvp in filtered)
{
if (queryString.Length > 0)
{
Expand Down
3 changes: 2 additions & 1 deletion EstateReportingAPI.Tests/EstateReportingAPI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<DebugType>None</DebugType>
<DebugType>Full</DebugType>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down Expand Up @@ -32,6 +32,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EstateReportingAPI.Client\EstateReportingAPI.Client.csproj" />
<ProjectReference Include="..\EstateReportingAPI\EstateReportingAPI.csproj" />
</ItemGroup>

Expand Down
36 changes: 34 additions & 2 deletions EstateReportingAPI.Tests/General/BootstrapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Threading;
using Client;
using EstateReportingAPI;
using Lamar;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Moq;
using Shouldly;
using Xunit;

[Collection("TestCollection")]
public class BootstrapperTests
{
[Fact]
public void VerifyBootstrapperIsValid()
{
public void VerifyBootstrapperIsValid(){
Mock<IWebHostEnvironment> hostingEnvironment = new Mock<IWebHostEnvironment>();
hostingEnvironment.Setup(he => he.EnvironmentName).Returns("Development");
hostingEnvironment.Setup(he => he.ContentRootPath).Returns("/home");
Expand Down Expand Up @@ -67,4 +71,32 @@ private void AddTestRegistrations(ServiceRegistry services,
services.AddSingleton<IConfiguration>(Startup.Configuration);
}
}

public class QueryStringBuilderTests
{
[Theory]
[InlineData("StringTest", "value",true)]
[InlineData("IntegerTest", 10, true)]
[InlineData("DecimalTest1", 1.00, true)]
[InlineData("DecimalTest2", 0.01, true)]
[InlineData("GuidTest", "69F35754-EB7D-441B-A62F-F50633AFBE91", true)]
[InlineData("NullTest", null, false)]
[InlineData("EmptyStringTest", "", false)]
[InlineData("ZeroIntegerTest", 0, false)]
[InlineData("ZeroDecimalTest1", 0, false)]
[InlineData("ZeroDecimalTest2", 0.00, false)]
[InlineData("EmptyGuidTest", "00000000-0000-0000-0000-000000000000", false)]
public void QueryStringBuilderTests_BuildQueryString_ValuesAddedAsExpected(String keyname, Object value, Boolean isAdded){
if (keyname == "GuidTest" || keyname == "EmptyGuidTest")
value = Guid.Parse(value.ToString());

QueryStringBuilder builder = new QueryStringBuilder();
builder.AddParameter(keyname, value);

String queryString = builder.BuildQueryString();

queryString.Contains(keyname).ShouldBe(isAdded);

}
}
}