Skip to content

Commit 848ddea

Browse files
authored
create order api (#97)
* create order api * fix
1 parent 302d696 commit 848ddea

File tree

48 files changed

+489
-85
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+489
-85
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# This workflow will build a .NET project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net
3+
4+
name: Order Query API CI
5+
6+
on:
7+
workflow_dispatch:
8+
push:
9+
branches: [ "main" ]
10+
paths:
11+
- src/BuildingBlocks/**
12+
- src/Services/Order.Query/**
13+
- .github/workflows/order-query-api-CI.yml
14+
15+
pull_request:
16+
branches: [ "main" ]
17+
paths:
18+
- src/BuildingBlocks/**
19+
- src/Services/Order.Query/**
20+
- .github/workflows/order-query-api-CI.yml
21+
22+
env:
23+
SERVICE: order-query-API
24+
IMAGE: order-query-API
25+
ProjectPath: src/Services/Order.Query/Order.Query.API
26+
ProjectTestPath: src/Services/Order.Query/Order.Query.API.IntegrationTests
27+
28+
jobs:
29+
30+
build:
31+
runs-on: ubuntu-latest
32+
permissions: write-all
33+
steps:
34+
- name: Checkout code
35+
uses: actions/checkout@v2
36+
- uses: ./.github/workflows/composite/build-push
37+
with:
38+
service: ${{ env.SERVICE }}
39+
image_name: ${{ env.IMAGE }}
40+
project_path: ${{ env.ProjectPath }}
41+
test_path: ${{ env.ProjectTestPath }}
42+
registry_username: ${{ vars.REGISTRYUSERNAME }}
43+
dockerhub_username: ${{ secrets.DOCKERHUB_USERNAME }}
44+
dockerhub_token: ${{ secrets.DOCKERHUB_TOKEN }}

src/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<ItemGroup>
66
<PackageVersion Include="AspNetCore.HealthChecks.Redis" Version="8.0.1" />
77
<PackageVersion Include="Cnblogs.IdentityModel.AspNetCore.OAuth2Introspection" Version="7.0.0" />
8+
<PackageVersion Include="FastEndpoints" Version="7.0.1" />
89
<PackageVersion Include="Grpc.Net.ClientFactory" Version="2.57.0" />
910
<PackageVersion Include="Grpc.Net.Client" Version="2.65.0" />
1011
<PackageVersion Include="Grpc.Core" Version="2.46.6" />
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.AspNetCore.Mvc.Testing;
3+
using Microsoft.AspNetCore.TestHost;
4+
using Order.Query.Api;
5+
6+
namespace Order.Query.API.IntegrationTests;
7+
8+
[CollectionDefinition(Name)]
9+
public class TestCollection : ICollectionFixture<ApiFactory>
10+
{
11+
public const string Name = "TestCollection";
12+
}
13+
14+
public class ApiFactory : WebApplicationFactory<Program>
15+
{
16+
private readonly CancellationTokenSource _timeoutCancellationTokenSource = new(TimeSpan.FromSeconds(30));
17+
protected override void ConfigureWebHost(IWebHostBuilder builder)
18+
{
19+
builder.ConfigureTestServices(services =>
20+
{
21+
22+
});
23+
}
24+
25+
internal CancellationToken CancellationToken => _timeoutCancellationTokenSource.Token;
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using AutoFixture;
2+
using Order.Query.API.Features.GetOrderById;
3+
4+
namespace Order.Query.API.IntegrationTests.AutoFixture.Customizations;
5+
6+
public class GetOrderByIdCustomization : ICustomization
7+
{
8+
public void Customize(IFixture fixture)
9+
{
10+
fixture.Customize<Request>(x => x
11+
.With(r => r.OrderId, Ulid.NewUlid().ToString )
12+
.With(r => r.CustomerId, Guid.NewGuid().ToString()));
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using AutoFixture;
2+
using AutoFixture.Xunit2;
3+
using Order.Query.API.IntegrationTests.AutoFixture.Customizations;
4+
5+
namespace Order.Query.API.IntegrationTests.AutoFixture;
6+
7+
public class DomainDataAutoAttribute() : AutoDataAttribute(FixtureFactory.Create)
8+
{
9+
private static class FixtureFactory
10+
{
11+
public static IFixture Create()
12+
{
13+
var fixture = new Fixture();
14+
15+
fixture.Customize(new GetOrderByIdCustomization());
16+
17+
return fixture;
18+
}
19+
}
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Net;
2+
using FastEndpoints;
3+
using Order.Query.API.Features.GetOrderById;
4+
using Order.Query.API.IntegrationTests.AutoFixture;
5+
using Shouldly;
6+
7+
namespace Order.Query.API.IntegrationTests.Features.GetOrderById;
8+
9+
[Collection(TestCollection.Name)]
10+
public class GetOrderByIdTests(ApiFactory apiFactory) : IAsyncLifetime
11+
{
12+
private HttpClient _client = default!;
13+
14+
public async Task InitializeAsync()
15+
{
16+
_client = apiFactory.CreateClient();
17+
await Task.CompletedTask;
18+
}
19+
20+
[Theory]
21+
[DomainDataAuto]
22+
public async Task GetOrderById_WhenNoTokenProvided_ReturnsUnauthorized(Request request)
23+
{
24+
// Act
25+
var response = await _client.GETAsync<Endpoint, Request>(request);
26+
27+
// Assert
28+
response.ShouldNotBeNull();
29+
response.StatusCode.ShouldBe(HttpStatusCode.OK);
30+
}
31+
32+
public async Task DisposeAsync()
33+
{
34+
await Task.CompletedTask;
35+
}
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="coverlet.collector" />
12+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
14+
<PackageReference Include="Ulid" />
15+
<PackageReference Include="xunit" />
16+
<PackageReference Include="xunit.runner.visualstudio" />
17+
<PackageReference Include="Shouldly" />
18+
<PackageReference Include="AutoFixture.Xunit2"/>
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<Using Include="Xunit"/>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\Order.Query.API\Order.Query.API.csproj" />
27+
</ItemGroup>
28+
29+
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace Order.Query.API.Configurations;
4+
5+
public class DatabaseConfigurations
6+
{
7+
[ConfigurationKeyName("ConnectionStrings:Database")]
8+
[Required]
9+
public required string PostgresDb { get; init; }
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
2+
USER $APP_UID
3+
WORKDIR /app
4+
EXPOSE 8080
5+
EXPOSE 8081
6+
7+
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
8+
ARG BUILD_CONFIGURATION=Release
9+
WORKDIR /src
10+
COPY ["Services/Order.Query/Order.Query.API/Order.Query.API.csproj", "Services/Order.Query/Order.Query.API/"]
11+
RUN dotnet restore "Services/Order.Query/Order.Query.API/Order.Query.API.csproj"
12+
COPY . .
13+
WORKDIR "/src/Services/Order.Query/Order.Query.API"
14+
RUN dotnet build "./Order.Query.API.csproj" -c $BUILD_CONFIGURATION -o /app/build
15+
16+
FROM build AS publish
17+
ARG BUILD_CONFIGURATION=Release
18+
RUN dotnet publish "./Order.Query.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
19+
20+
FROM base AS final
21+
WORKDIR /app
22+
COPY --from=publish /app/publish .
23+
ENTRYPOINT ["dotnet", "Order.Query.API.dll"]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using FastEndpoints;
2+
3+
namespace Order.Query.API.Features.GetOrderById;
4+
5+
public class Endpoint : Endpoint<Request, Response>
6+
{
7+
public override void Configure()
8+
{
9+
Get("/customer/{CustomerId}/orders/{OrderId}");
10+
AllowAnonymous();
11+
DontCatchExceptions();
12+
Version(1);
13+
}
14+
15+
public override async Task HandleAsync(Request req, CancellationToken ct)
16+
{
17+
await Send.OkAsync(ct);
18+
return;
19+
}
20+
}

0 commit comments

Comments
 (0)