3 Commits

7 changed files with 107 additions and 0 deletions

1
.gitignore vendored
View File

@@ -401,3 +401,4 @@ FodyWeavers.xsd
/DigitalData.Core.Tests/obj/DigitalData.Core.Tests.csproj.nuget.dgspec.json
/DigitalData.Core.Tests/obj/DigitalData.Core.Tests.csproj.nuget.g.targets
/DigitalData.Core.Tests/obj/project.assets.json
/.vs/DigitalData.Core/v17/TestStore/0/testlog.manifest

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,59 @@
using Newtonsoft.Json;
using System.Net.Http.Json;
using static System.Net.WebRequestMethods;
namespace DigitalData.Core.Client
{
public static class HttpExtensions
{
public static async Task<string> Text(this HttpResponseMessage response) => await response.Content.ReadAsStringAsync();
public static async Task<T?> Json<T>(this HttpResponseMessage response) => await response.Content.ReadFromJsonAsync<T>();
public static async Task<dynamic> Json(this HttpResponseMessage response)
{
string json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<dynamic>(json) ?? string.Empty;
}
public static async Task<IEnumerable<dynamic>> JsonList(this HttpResponseMessage response)
{
string json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<dynamic>(json) ?? string.Empty;
}
public static async Task<HttpResponseMessage> Fetch(this string url, string method = Http.Get)
{
using HttpClient client = new();
return method switch
{
Http.Get => await client.GetAsync(url),
_ => throw new NotImplementedException(nameof(method)),
};
}
public static async void ThenAsync<T>(this Task<T> task, Action<T> toDo)
{
var then = await task;
toDo(then);
}
public static async Task<I> ThenAsync<T, I>(this Task<T> task, Func<T, I> toDo)
{
var then = await task;
return toDo(then);
}
public static async Task<I> ThenAsync<T, I>(this Task<T> task, Func<T, Task<I>> toDoAsync)
{
var then = await task;
return await toDoAsync(then);
}
public static void ForEach<T>(this IEnumerable<T> values, Action<T> action)
{
foreach (var value in values)
action(value);
}
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DigitalData.Core.Tests.Client
{
internal class HttpExtensionsTest
{
}
}

View File

@@ -75,5 +75,21 @@ namespace DigitalData.Core.Tests.DTO
Assert.IsFalse(dto1 != dto2);
}
[Test]
public void EntityWithSameIdShouldBeContained()
{
IEnumerable<SampleDto> dtos = new List<SampleDto>() { new(1), new(2) };
var dto = new SampleDto(1);
Assert.That(dtos, Does.Contain(dto));
}
[Test]
public void EntityWithSameIdShouldBeNotContained()
{
IEnumerable<SampleDto> dtos = new List<SampleDto>() { new(1), new(2) };
var dto = new SampleDto(3);
Assert.That(dtos, !Does.Contain(dto));
}
}
}

View File

@@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.Tests", "D
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.DTO", "DigitalData.Core.DTO\DigitalData.Core.DTO.csproj", "{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Client", "DigitalData.Core.Client\DigitalData.Core.Client.csproj", "{6A80FFEC-9B83-40A7-8C78-124440B48B33}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}.Release|Any CPU.Build.0 = Release|Any CPU
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE