Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit d5533c0

Browse files
davidfowlReubenBond
authored andcommitted
Move the pic controller to a minimal API
1 parent 8debda5 commit d5533c0

File tree

11 files changed

+53
-72
lines changed

11 files changed

+53
-72
lines changed

src/ApiGateways/Web.Bff.Shopping/aggregator/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
app.UseAuthentication();
3333
app.UseAuthorization();
3434

35-
app.MapGet("/", () => Results.Redirect("/swagger"));
36-
3735
app.MapControllers();
3836
app.MapReverseProxy();
3937

src/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="7.0.3" />
5050
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.3" />
5151
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.3" />
52+
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="7.0.5" />
5253
<PackageVersion Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="7.0.3" />
5354
<PackageVersion Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="7.0.3" />
5455
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="7.0.3" />

src/Services/Basket/Basket.API/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
app.UseServiceDefaults();
2121

22-
app.MapGet("/", () => Results.Redirect("/swagger"));
23-
2422
app.MapGrpcService<BasketService>();
2523
app.MapControllers();
2624

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.AspNetCore.Routing;
2+
3+
namespace Catalog.API.Apis;
4+
5+
public static class PicApi
6+
{
7+
public static IEndpointConventionBuilder MapPicApi(this IEndpointRouteBuilder routes)
8+
{
9+
return routes.MapGet("api/v1/catalog/items/{catalogItemId:int}/pic",
10+
async (int catalogItemId, CatalogContext db, IWebHostEnvironment environment) =>
11+
{
12+
var item = await db.CatalogItems.FindAsync(catalogItemId);
13+
14+
if (item is null)
15+
{
16+
return Results.NotFound();
17+
}
18+
19+
var path = Path.Combine(environment.ContentRootPath, "Pics", item.PictureFileName);
20+
21+
string imageFileExtension = Path.GetExtension(item.PictureFileName);
22+
string mimetype = GetImageMimeTypeFromImageFileExtension(imageFileExtension);
23+
24+
return Results.File(path, mimetype);
25+
})
26+
.WithTags("Pic")
27+
.Produces(404);
28+
29+
static string GetImageMimeTypeFromImageFileExtension(string extension) => extension switch
30+
{
31+
".png" => "image/png",
32+
".gif" => "image/gif",
33+
".jpg" or ".jpeg" => "image/jpeg",
34+
".bmp" => "image/bmp",
35+
".tiff" => "image/tiff",
36+
".wmf" => "image/wmf",
37+
".jp2" => "image/jp2",
38+
".svg" => "image/svg+xml",
39+
_ => "application/octet-stream",
40+
};
41+
}
42+
}

src/Services/Catalog/Catalog.API/Controllers/PicController.cs

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/Services/Catalog/Catalog.API/GlobalUsings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
global using Microsoft.eShopOnContainers.Services.Catalog.API.ViewModel;
3636
global using Microsoft.Extensions.Configuration;
3737
global using Microsoft.Extensions.DependencyInjection;
38-
global using Microsoft.Extensions.FileProviders;
3938
global using Microsoft.Extensions.Logging;
4039
global using Microsoft.Extensions.Options;
4140
global using Polly;
4241
global using Polly.Retry;
4342
global using Services.Common;
43+
global using Catalog.API.Apis;

src/Services/Catalog/Catalog.API/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818

1919
app.UseServiceDefaults();
2020

21-
app.MapGet("/", () => Results.Redirect("/swagger"));
22-
21+
app.MapPicApi();
2322
app.MapControllers();
2423
app.MapGrpcService<CatalogService>();
2524

src/Services/Ordering/Ordering.API/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444

4545
app.UseServiceDefaults();
4646

47-
app.MapGet("/", () => Results.Redirect("/swagger"));
48-
4947
app.MapGrpcService<OrderingService>();
5048
app.MapControllers();
5149

src/Services/Services.Common/CommonExtensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.AspNetCore.Builder;
55
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
66
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.Http;
78
using Microsoft.AspNetCore.Routing;
89
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
910
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
@@ -100,7 +101,7 @@ public static async Task<bool> CheckHealthAsync(this WebApplication app)
100101
return true;
101102
}
102103

103-
public static IApplicationBuilder UseDefaultOpenApi(this IApplicationBuilder app, IConfiguration configuration)
104+
public static IApplicationBuilder UseDefaultOpenApi(this WebApplication app, IConfiguration configuration)
104105
{
105106
var openApiSection = configuration.GetSection("OpenApi");
106107

@@ -139,6 +140,9 @@ public static IApplicationBuilder UseDefaultOpenApi(this IApplicationBuilder app
139140
}
140141
});
141142

143+
// Add a redirect from the root of the app to the swagger endpoint
144+
app.MapGet("/", () => Results.Redirect("/swagger")).ExcludeFromDescription();
145+
142146
return app;
143147
}
144148

@@ -151,6 +155,8 @@ public static IServiceCollection AddDefaultOpenApi(this IServiceCollection servi
151155
return services;
152156
}
153157

158+
services.AddEndpointsApiExplorer();
159+
154160
return services.AddSwaggerGen(options =>
155161
{
156162
/// {

src/Services/Services.Common/Services.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<PackageReference Include="Microsoft.AspNetCore.HealthChecks" />
3030
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" />
3131
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" />
32+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
3233
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
3334
<PrivateAssets>all</PrivateAssets>
3435
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

0 commit comments

Comments
 (0)