House Keeping
This commit is contained in:
parent
0058dedd25
commit
bafe874f96
@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
|
<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" Version="6.0.4" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.4">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.4">
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
namespace API.Controllers
|
namespace API.Controllers
|
||||||
{
|
{
|
||||||
[Route("errors/{code}")]
|
[Route("errors/{code}")]
|
||||||
|
[ApiExplorerSettings(IgnoreApi = true)]
|
||||||
public class ErrorController : BaseApiController
|
public class ErrorController : BaseApiController
|
||||||
{
|
{
|
||||||
public IActionResult Error(int code)
|
public IActionResult Error(int code)
|
||||||
|
@ -4,6 +4,7 @@ using Core.Interfaces;
|
|||||||
using Core.Specifications;
|
using Core.Specifications;
|
||||||
using API.Dtos;
|
using API.Dtos;
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
|
using API.Errors;
|
||||||
|
|
||||||
namespace API.Controllers
|
namespace API.Controllers
|
||||||
{
|
{
|
||||||
@ -31,10 +32,13 @@ namespace API.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
|
||||||
public async Task<ActionResult<ProductToReturnDto>> GetProduct(int id)
|
public async Task<ActionResult<ProductToReturnDto>> GetProduct(int id)
|
||||||
{
|
{
|
||||||
var spec = new ProductsWithTypesAndBrandsSpecification(id);
|
var spec = new ProductsWithTypesAndBrandsSpecification(id);
|
||||||
var product = await _productsRepo.GetEntityWithSpec(spec);
|
var product = await _productsRepo.GetEntityWithSpec(spec);
|
||||||
|
if (product == null) return NotFound(new ApiResponse(404));
|
||||||
return _mapper.Map<Product, ProductToReturnDto>(product);
|
return _mapper.Map<Product, ProductToReturnDto>(product);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ namespace API.Controllers;
|
|||||||
|
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
|
[ApiExplorerSettings(IgnoreApi = true)]
|
||||||
public class WeatherForecastController : ControllerBase
|
public class WeatherForecastController : ControllerBase
|
||||||
{
|
{
|
||||||
private static readonly string[] Summaries = new[]
|
private static readonly string[] Summaries = new[]
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
namespace API.Errors
|
|
||||||
{
|
|
||||||
public class ApiValidationErrorReponse
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
11
API/Errors/ApiValidationErrorResponse.cs
Normal file
11
API/Errors/ApiValidationErrorResponse.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
namespace API.Errors
|
||||||
|
{
|
||||||
|
public class ApiValidationErrorResponse : ApiResponse
|
||||||
|
{
|
||||||
|
public ApiValidationErrorResponse() : base(400)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<string> Errors { get; set; }
|
||||||
|
}
|
||||||
|
}
|
32
API/Extensions/ApplicationServicesExtensions.cs
Normal file
32
API/Extensions/ApplicationServicesExtensions.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
25
API/Extensions/SwaggerServiceExtensions.cs
Normal file
25
API/Extensions/SwaggerServiceExtensions.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,8 @@
|
|||||||
|
using API.Extensions;
|
||||||
using API.Helpers;
|
using API.Helpers;
|
||||||
using API.Middleware;
|
using API.Middleware;
|
||||||
using Core.Interfaces;
|
|
||||||
using Infrastructure.Data;
|
using Infrastructure.Data;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.OpenApi.Models;
|
|
||||||
|
|
||||||
namespace API
|
namespace API
|
||||||
{
|
{
|
||||||
@ -21,14 +20,11 @@ namespace API
|
|||||||
{
|
{
|
||||||
|
|
||||||
services.AddControllers();
|
services.AddControllers();
|
||||||
|
services.AddApplicationServices();
|
||||||
|
services.AddSwaggerDocumentation();
|
||||||
services.AddDbContext<StoreContext>(x => x.UseSqlite(_config.GetConnectionString("DefaultConnection")));
|
services.AddDbContext<StoreContext>(x => x.UseSqlite(_config.GetConnectionString("DefaultConnection")));
|
||||||
services.AddScoped<iProductRepository, ProductRepository>();
|
|
||||||
services.AddScoped(typeof(IGenericRepository<>), (typeof(GenericRepository<>)));
|
|
||||||
services.AddAutoMapper(typeof(MappingProfiles));
|
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.
|
// 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>();
|
app.UseMiddleware<ExceptionMiddleware>();
|
||||||
|
|
||||||
if (env.IsDevelopment())
|
|
||||||
{
|
|
||||||
app.UseSwagger();
|
|
||||||
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPIv5 v1"));
|
|
||||||
}
|
|
||||||
|
|
||||||
app.UseStatusCodePagesWithReExecute("/errors/{0}");
|
app.UseStatusCodePagesWithReExecute("/errors/{0}");
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
@ -50,6 +40,7 @@ namespace API
|
|||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
|
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
app.UseSwaggerDocumentation();
|
||||||
|
|
||||||
app.UseEndpoints(endpoints =>
|
app.UseEndpoints(endpoints =>
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user