Sky.Net/API/Extensions/ApplicationServicesExtensions.cs
Charles Showalter 344ecb8762 Added Order API
2022-05-24 15:35:03 -07:00

37 lines
1.4 KiB
C#

using API.Errors;
using Core.Interfaces;
using Infrastructure.Data;
using Infrastructure.Services;
using Microsoft.AspNetCore.Mvc;
namespace API.Extensions
{
public static class ApplicationServicesExtensions
{
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
{
services.AddScoped<ITokenService, TokenService>();
services.AddScoped<iProductRepository, ProductRepository>();
services.AddScoped<IBasketRepository, BasketRepository>();
services.AddScoped<IOrderService, OrderService>();
services.AddScoped<IUnitOfWork, UnitOfWork>();
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;
}
}
}