House Keeping

This commit is contained in:
Charles Showalter 2022-05-11 13:18:34 -07:00
parent 0058dedd25
commit bafe874f96
9 changed files with 81 additions and 21 deletions

View File

@ -7,6 +7,8 @@
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc;
namespace API.Controllers
{
[Route("errors/{code}")]
[ApiExplorerSettings(IgnoreApi = true)]
public class ErrorController : BaseApiController
{
public IActionResult Error(int code)

View File

@ -4,6 +4,7 @@ using Core.Interfaces;
using Core.Specifications;
using API.Dtos;
using AutoMapper;
using API.Errors;
namespace API.Controllers
{
@ -31,10 +32,13 @@ namespace API.Controllers
}
[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
public async Task<ActionResult<ProductToReturnDto>> GetProduct(int id)
{
var spec = new ProductsWithTypesAndBrandsSpecification(id);
var product = await _productsRepo.GetEntityWithSpec(spec);
if (product == null) return NotFound(new ApiResponse(404));
return _mapper.Map<Product, ProductToReturnDto>(product);
}

View File

@ -4,6 +4,7 @@ namespace API.Controllers;
[ApiController]
[Route("[controller]")]
[ApiExplorerSettings(IgnoreApi = true)]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]

View File

@ -1,7 +0,0 @@
namespace API.Errors
{
public class ApiValidationErrorReponse
{
}
}

View File

@ -0,0 +1,11 @@
namespace API.Errors
{
public class ApiValidationErrorResponse : ApiResponse
{
public ApiValidationErrorResponse() : base(400)
{
}
public IEnumerable<string> Errors { get; set; }
}
}

View File

@ -0,0 +1,32 @@
using API.Errors;
using Core.Interfaces;
using Infrastructure.Data;
using Microsoft.AspNetCore.Mvc;
namespace API.Extensions
{
public static class ApplicationServicesExtensions
{
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
{
services.AddScoped<iProductRepository, ProductRepository>();
services.AddScoped(typeof(IGenericRepository<>), (typeof(GenericRepository<>)));
services.Configure<ApiBehaviorOptions>(options =>
options.InvalidModelStateResponseFactory = actionContext =>
{
var errors = actionContext.ModelState
.Where(e => e.Value.Errors.Count > 0)
.SelectMany(x => x.Value.Errors)
.Select(x => x.ErrorMessage).ToArray();
var errorRepsponse = new ApiValidationErrorResponse
{
Errors = errors
};
return new BadRequestObjectResult(errorRepsponse);
});
return services;
}
}
}

View File

@ -0,0 +1,25 @@
using Microsoft.OpenApi.Models;
namespace API.Extensions
{
public static class SwaggerServiceExtensions
{
public static IServiceCollection AddSwaggerDocumentation(this IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebAPIv5", Version = "v1" });
});
return services;
}
public static IApplicationBuilder UseSwaggerDocumentation(this IApplicationBuilder app)
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPIv5 v1"));
return app;
}
}
}

View File

@ -1,9 +1,8 @@
using API.Extensions;
using API.Helpers;
using API.Middleware;
using Core.Interfaces;
using Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
namespace API
{
@ -21,14 +20,11 @@ namespace API
{
services.AddControllers();
services.AddApplicationServices();
services.AddSwaggerDocumentation();
services.AddDbContext<StoreContext>(x => x.UseSqlite(_config.GetConnectionString("DefaultConnection")));
services.AddScoped<iProductRepository, ProductRepository>();
services.AddScoped(typeof(IGenericRepository<>), (typeof(GenericRepository<>)));
services.AddAutoMapper(typeof(MappingProfiles));
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebAPIv5", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@ -36,12 +32,6 @@ namespace API
{
app.UseMiddleware<ExceptionMiddleware>();
if (env.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPIv5 v1"));
}
app.UseStatusCodePagesWithReExecute("/errors/{0}");
app.UseHttpsRedirection();
@ -50,6 +40,7 @@ namespace API
app.UseStaticFiles();
app.UseAuthorization();
app.UseSwaggerDocumentation();
app.UseEndpoints(endpoints =>
{