32 lines
1.1 KiB
C#
32 lines
1.1 KiB
C#
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|