Skip to content

Commit ecb9770

Browse files
authored
move customer api to minimal api (#40)
* move customer api to minimal api * update packages
1 parent f36b92b commit ecb9770

File tree

13 files changed

+270
-128
lines changed

13 files changed

+270
-128
lines changed

src/WarehouseEngine.Api/Controllers/CustomerController.cs

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Http.HttpResults;
3+
using WarehouseEngine.Api.Extensions.ErrorTypeExtensions;
4+
using WarehouseEngine.Application.Dtos;
5+
using WarehouseEngine.Application.Interfaces;
6+
7+
namespace WarehouseEngine.Api.Endpoints;
8+
9+
internal class CustomerEndpoints
10+
{
11+
public static void Map(WebApplication app)
12+
{
13+
app.MapGet("/api/v{version:apiVersion}/customer/{id}", [Authorize] async (ILogger<CustomerEndpoints> logger, ICustomerService customerService, Guid id) =>
14+
{
15+
var customer = await customerService.GetByIdAsync(id);
16+
return customer.Match<Results<Ok<CustomerResponseDto>, ProblemHttpResult>>(
17+
customer => TypedResults.Ok(customer),
18+
invalidResult =>
19+
{
20+
logger.LogError("Record not found. {message}", invalidResult.GetMessage());
21+
return TypedResults.Problem(statusCode: 404, detail: invalidResult.GetMessage());
22+
});
23+
})
24+
.Produces<CustomerResponseDto>(200)
25+
.WithName("GetCustomerById")
26+
.WithTags("Customer");
27+
28+
app.MapGet("/api/v{version:apiVersion}/customer/count", [Authorize] async (ICustomerService customerService) =>
29+
{
30+
var count = await customerService.GetCount();
31+
return TypedResults.Ok(count);
32+
})
33+
.Produces<int>(200)
34+
.WithName("GetCustomerCount")
35+
.WithTags("Customer");
36+
37+
app.MapPost("/api/v{version:apiVersion}/customer", [Authorize] async (ILogger < CustomerEndpoints > logger, ICustomerService customerService, PostCustomerDto customerDto, HttpContext httpContext) =>
38+
{
39+
customerDto.DateCreated = DateTime.UtcNow;
40+
customerDto.CreatedBy = httpContext.User.Identity?.Name ?? "Unknown";
41+
42+
var customer = await customerService.AddAsync(customerDto, httpContext.User.Identity?.Name ?? "Unknown");
43+
44+
return customer.Match<Results<Created, ProblemHttpResult>>(
45+
customer => TypedResults.Created(),
46+
entityExists =>
47+
{
48+
logger.LogWarning("Record not found. {message}", entityExists.GetMessage());
49+
50+
return TypedResults.Problem(statusCode: 404, extensions: [new("Record not found", entityExists.GetMessage())]);
51+
},
52+
invalidShippingResult =>
53+
{
54+
logger.LogWarning(invalidShippingResult.ErrorMessage!);
55+
56+
return TypedResults.Problem(invalidShippingResult.ErrorMessage, statusCode: 404);
57+
});
58+
})
59+
.Produces<CustomerResponseDto>(200)
60+
.ProducesProblem(400)
61+
.WithName("CreateCustomer")
62+
.WithTags("Customer");
63+
}
64+
}

src/WarehouseEngine.Api/Extensions/ErrorTypeExtensions/EntityErrorTypeExtensions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33
namespace WarehouseEngine.Api.Extensions.ErrorTypeExtensions;
44
public static class EntityErrorTypeExtensions
55
{
6+
private static readonly string EntityAlreadyExistsMessage = "Entity already exists";
7+
private static readonly string EntityDoesNotExistMessage = "Entity does not exist";
8+
private static readonly string UnknownErrorMessage = "Unknown error";
69
public static string GetMessage(this EntityErrorType errorType)
710
{
811
if (errorType is EntityAlreadyExists)
912
{
10-
return "Entity already exists";
13+
return EntityAlreadyExistsMessage;
1114
}
1215
else if (errorType is EntityDoesNotExist)
1316
{
14-
return "Entity does not exist";
17+
return EntityDoesNotExistMessage;
1518
}
1619
else
1720
{
18-
return "Unknown error";
21+
return UnknownErrorMessage;
1922
}
2023
}
2124
}

src/WarehouseEngine.Api/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.OpenApi;
99
using Scalar.AspNetCore;
1010
using WarehouseEngine.Api.Configuration;
11+
using WarehouseEngine.Api.Endpoints;
1112
using WarehouseEngine.Api.Examples;
1213
using WarehouseEngine.Api.Middleware.Auth;
1314
using WarehouseEngine.Application.Implementations;
@@ -112,7 +113,7 @@ public static async Task Main(string[] args)
112113

113114
// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/openapi/customize-openapi?view=aspnetcore-10.0#use-document-transformers
114115
// Apply it as a requirement for all operations
115-
foreach (var operation in document.Paths.Values.SelectMany(path => path.Operations))
116+
foreach (var operation in document.Paths.Values.SelectMany(path => path.Operations ?? []))
116117
{
117118
if (operation.Value.Tags is not null && operation.Value.Tags.Any(tag => tag.Name == "Authenticate")) { continue; }
118119

@@ -137,6 +138,8 @@ [new OpenApiSecuritySchemeReference("Bearer", document)] = []
137138
return Task.CompletedTask;
138139
});
139140
});
141+
142+
services.AddValidation();
140143
services.AddProblemDetails();
141144

142145
var app = builder.Build();
@@ -168,6 +171,7 @@ [new OpenApiSecuritySchemeReference("Bearer", document)] = []
168171

169172

170173
app.MapControllers();
174+
CustomerEndpoints.Map(app);
171175

172176
#if DEBUG
173177
app.UseCors("localhost");

src/WarehouseEngine.Api/WarehouseEngine.Api.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0" />
11-
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
12-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.0" />
13-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />
14-
<PackageReference Include="Microsoft.OpenApi" Version="2.3.10" />
10+
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.1" />
11+
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.1" />
12+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.1" />
13+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.1" />
14+
<PackageReference Include="Microsoft.OpenApi" Version="2.3.12" />
1515
<PackageReference Include="OneOf" Version="3.0.271" />
16-
<PackageReference Include="Scalar.AspNetCore" Version="2.11.0" />
16+
<PackageReference Include="Scalar.AspNetCore" Version="2.11.10" />
1717
</ItemGroup>
1818

1919
<ItemGroup>

src/WarehouseEngine.Application/Dtos/Customer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ public class PostCustomerDto
4848
/// </summary>
4949
[JsonIgnore]
5050
public Guid? Id { get; set; }
51+
52+
[Required]
5153
public required string Name { get; init; }
5254

5355
public Address? BillingAddress { get; init; }
5456

57+
[Required]
5558
public required Address ShippingAddress { get; init; }
5659

5760
[JsonIgnore]

src/WarehouseEngine.Application/WarehouseEngine.Application.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.0" />
14+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.1" />
1515
<PackageReference Include="Moq" Version="4.20.72" />
1616
<PackageReference Include="OneOf" Version="3.0.271" />
1717
<PackageReference Include="UUIDNext" Version="4.2.2" />

src/WarehouseEngine.Domain/WarehouseEngine.Domain.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
</ItemGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.0">
18+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.1">
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
<PrivateAssets>all</PrivateAssets>
2121
</PackageReference>
22-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.0" />
23-
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.0" />
22+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.1" />
23+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.1" />
2424
</ItemGroup>
2525

2626
</Project>

src/WarehouseEngine.Infrastructure/WarehouseEngine.Infrastructure.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
<ItemGroup>
1010
<PackageReference Include="EfCore.SchemaCompare" Version="10.0.0" />
11-
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.0" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.0">
11+
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.1">
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
<PrivateAssets>all</PrivateAssets>
1515
</PackageReference>
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.0" />
17-
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.0" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.1" />
17+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.1" />
1818
</ItemGroup>
1919

2020
<ItemGroup>

0 commit comments

Comments
 (0)