Skip to content

Commit

Permalink
Merge pull request #123 from cnblogs/fix-nullable-result-from-query
Browse files Browse the repository at this point in the history
fix: map null query result to NotFoundResult
  • Loading branch information
ikesnowy authored Jul 8, 2023
2 parents 3b7d19a + 98aae1e commit 1e0eb6f
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Cnblogs.CodeQuality" Version="1.6.8">
<PackageReference Include="Cnblogs.CodeQuality" Version="1.7.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public QueryEndpointHandler(IMediator mediator)
}

var response = await _mediator.Send(query);
return response;
return response == null ? Results.NotFound() : Results.Ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MediatR" Version="12.0.1"/>
<PackageReference Include="MediatR" Version="12.1.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
</Description>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MediatR" Version="12.0.1"/>
<PackageReference Include="MediatR" Version="12.1.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

namespace Cnblogs.Architecture.IntegrationTestProject.Application.Queries;

public record GetStringQuery(string? AppId = null, int? StringId = null) : IQuery<string>;
public record GetStringQuery(string? AppId = null, int? StringId = null, bool Found = true) : IQuery<string>;
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public class GetStringQueryHandler : IQueryHandler<GetStringQuery, string>
/// <inheritdoc />
public Task<string?> Handle(GetStringQuery request, CancellationToken cancellationToken)
{
return Task.FromResult((string?)"Hello");
return request.Found ? Task.FromResult((string?)"Hello") : Task.FromResult((string?)null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PackageReference Include="Cnblogs.Serilog.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http.Json;
using System.Net;
using System.Net.Http.Json;
using Cnblogs.Architecture.Ddd.Infrastructure.Abstractions;
using Cnblogs.Architecture.IntegrationTestProject;
using FluentAssertions;
Expand All @@ -23,6 +24,19 @@ public async Task GetItem_SuccessAsync()
content.Should().NotBeNullOrEmpty();
}

[Fact]
public async Task GetItem_NotFondAsync()
{
// Arrange
var builder = new WebApplicationFactory<Program>();

// Act
var response = await builder.CreateClient().GetAsync("/api/v1/strings/1?found=false");

// Assert
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}

[Fact]
public async Task ListItems_SuccessAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down

0 comments on commit 1e0eb6f

Please sign in to comment.