Move the pic controller to a minimal API
This commit is contained in:
parent
8debda5c67
commit
d5533c0ff9
@ -32,8 +32,6 @@ app.UseCors("CorsPolicy");
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapGet("/", () => Results.Redirect("/swagger"));
|
||||
|
||||
app.MapControllers();
|
||||
app.MapReverseProxy();
|
||||
|
||||
|
@ -49,6 +49,7 @@
|
||||
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="7.0.3" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.3" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.3" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="7.0.5" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="7.0.3" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="7.0.3" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="7.0.3" />
|
||||
|
@ -19,8 +19,6 @@ var app = builder.Build();
|
||||
|
||||
app.UseServiceDefaults();
|
||||
|
||||
app.MapGet("/", () => Results.Redirect("/swagger"));
|
||||
|
||||
app.MapGrpcService<BasketService>();
|
||||
app.MapControllers();
|
||||
|
||||
|
42
src/Services/Catalog/Catalog.API/Apis/PicApi.cs
Normal file
42
src/Services/Catalog/Catalog.API/Apis/PicApi.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
|
||||
namespace Catalog.API.Apis;
|
||||
|
||||
public static class PicApi
|
||||
{
|
||||
public static IEndpointConventionBuilder MapPicApi(this IEndpointRouteBuilder routes)
|
||||
{
|
||||
return routes.MapGet("api/v1/catalog/items/{catalogItemId:int}/pic",
|
||||
async (int catalogItemId, CatalogContext db, IWebHostEnvironment environment) =>
|
||||
{
|
||||
var item = await db.CatalogItems.FindAsync(catalogItemId);
|
||||
|
||||
if (item is null)
|
||||
{
|
||||
return Results.NotFound();
|
||||
}
|
||||
|
||||
var path = Path.Combine(environment.ContentRootPath, "Pics", item.PictureFileName);
|
||||
|
||||
string imageFileExtension = Path.GetExtension(item.PictureFileName);
|
||||
string mimetype = GetImageMimeTypeFromImageFileExtension(imageFileExtension);
|
||||
|
||||
return Results.File(path, mimetype);
|
||||
})
|
||||
.WithTags("Pic")
|
||||
.Produces(404);
|
||||
|
||||
static string GetImageMimeTypeFromImageFileExtension(string extension) => extension switch
|
||||
{
|
||||
".png" => "image/png",
|
||||
".gif" => "image/gif",
|
||||
".jpg" or ".jpeg" => "image/jpeg",
|
||||
".bmp" => "image/bmp",
|
||||
".tiff" => "image/tiff",
|
||||
".wmf" => "image/wmf",
|
||||
".jp2" => "image/jp2",
|
||||
".svg" => "image/svg+xml",
|
||||
_ => "application/octet-stream",
|
||||
};
|
||||
}
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
|
||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
public class PicController : ControllerBase
|
||||
{
|
||||
private readonly IWebHostEnvironment _env;
|
||||
private readonly CatalogContext _catalogContext;
|
||||
|
||||
public PicController(IWebHostEnvironment env,
|
||||
CatalogContext catalogContext)
|
||||
{
|
||||
_env = env;
|
||||
_catalogContext = catalogContext;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("api/v1/catalog/items/{catalogItemId:int}/pic")]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
||||
// GET: /<controller>/
|
||||
public async Task<ActionResult> GetImageAsync(int catalogItemId)
|
||||
{
|
||||
if (catalogItemId <= 0)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var item = await _catalogContext.CatalogItems
|
||||
.SingleOrDefaultAsync(ci => ci.Id == catalogItemId);
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
var path = Path.Combine(_env.ContentRootPath, "Pics", item.PictureFileName);
|
||||
|
||||
string imageFileExtension = Path.GetExtension(item.PictureFileName);
|
||||
string mimetype = GetImageMimeTypeFromImageFileExtension(imageFileExtension);
|
||||
|
||||
return PhysicalFile(path, mimetype);
|
||||
}
|
||||
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
private string GetImageMimeTypeFromImageFileExtension(string extension)
|
||||
{
|
||||
string mimetype = extension switch
|
||||
{
|
||||
".png" => "image/png",
|
||||
".gif" => "image/gif",
|
||||
".jpg" or ".jpeg" => "image/jpeg",
|
||||
".bmp" => "image/bmp",
|
||||
".tiff" => "image/tiff",
|
||||
".wmf" => "image/wmf",
|
||||
".jp2" => "image/jp2",
|
||||
".svg" => "image/svg+xml",
|
||||
_ => "application/octet-stream",
|
||||
};
|
||||
return mimetype;
|
||||
}
|
||||
}
|
@ -35,9 +35,9 @@ global using Microsoft.eShopOnContainers.Services.Catalog.API.Model;
|
||||
global using Microsoft.eShopOnContainers.Services.Catalog.API.ViewModel;
|
||||
global using Microsoft.Extensions.Configuration;
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
global using Microsoft.Extensions.FileProviders;
|
||||
global using Microsoft.Extensions.Logging;
|
||||
global using Microsoft.Extensions.Options;
|
||||
global using Polly;
|
||||
global using Polly.Retry;
|
||||
global using Services.Common;
|
||||
global using Catalog.API.Apis;
|
||||
|
@ -18,8 +18,7 @@ var app = builder.Build();
|
||||
|
||||
app.UseServiceDefaults();
|
||||
|
||||
app.MapGet("/", () => Results.Redirect("/swagger"));
|
||||
|
||||
app.MapPicApi();
|
||||
app.MapControllers();
|
||||
app.MapGrpcService<CatalogService>();
|
||||
|
||||
|
@ -44,8 +44,6 @@ var app = builder.Build();
|
||||
|
||||
app.UseServiceDefaults();
|
||||
|
||||
app.MapGet("/", () => Results.Redirect("/swagger"));
|
||||
|
||||
app.MapGrpcService<OrderingService>();
|
||||
app.MapControllers();
|
||||
|
||||
|
@ -4,6 +4,7 @@ using HealthChecks.UI.Client;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||
@ -100,7 +101,7 @@ public static class CommonExtensions
|
||||
return true;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseDefaultOpenApi(this IApplicationBuilder app, IConfiguration configuration)
|
||||
public static IApplicationBuilder UseDefaultOpenApi(this WebApplication app, IConfiguration configuration)
|
||||
{
|
||||
var openApiSection = configuration.GetSection("OpenApi");
|
||||
|
||||
@ -139,6 +140,9 @@ public static class CommonExtensions
|
||||
}
|
||||
});
|
||||
|
||||
// Add a redirect from the root of the app to the swagger endpoint
|
||||
app.MapGet("/", () => Results.Redirect("/swagger")).ExcludeFromDescription();
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
@ -151,6 +155,8 @@ public static class CommonExtensions
|
||||
return services;
|
||||
}
|
||||
|
||||
services.AddEndpointsApiExplorer();
|
||||
|
||||
return services.AddSwaggerGen(options =>
|
||||
{
|
||||
/// {
|
||||
|
@ -29,6 +29,7 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.HealthChecks" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
@ -21,7 +21,6 @@ var app = builder.Build();
|
||||
|
||||
app.UseServiceDefaults();
|
||||
|
||||
app.MapGet("/", () => Results.Redirect("/swagger"));
|
||||
app.MapControllers();
|
||||
|
||||
var eventBus = app.Services.GetRequiredService<IEventBus>();
|
||||
|
Loading…
x
Reference in New Issue
Block a user