Ejemplo n.º 1
0
        public static IServiceCollection AddApplicationServices(this IServiceCollection services)
        {
            services.AddScoped <ITokenService, TokenService>();
            services.AddScoped <IOrderService, OrderService>();
            services.AddScoped <IUnitOfWork, UnitOfWork>();

            services.AddScoped <IProductRepository, ProductRepository>();
            services.AddScoped <IBasketRepository, BasketRepository>();
            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 errorResponse = new APIValidationErrorResponse {
                        Errors = errors
                    };

                    return(new BadRequestObjectResult(errorResponse));
                };
            });

            return(services);
        }
Ejemplo n.º 2
0
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            services.AddControllers();
            services.AddDbContext <DatabaseContext>(ob => ob.UseNpgsql(configuration.GetConnectionString("DatabaseConnection")));
            services.AddDbContext <IdentityContext>(ob => ob.UseNpgsql(configuration.GetConnectionString("IdentityConnection")));
            services.AddSingleton <IConnectionMultiplexer>(serviceProvider =>
            {
                var configurationOptions = ConfigurationOptions.Parse(configuration.GetConnectionString("RedisConnection"));
                return(ConnectionMultiplexer.Connect(configurationOptions));
            });

            var builder = services.AddIdentityCore <AppUser>();

            builder = new IdentityBuilder(builder.UserType, builder.Services);
            builder.AddEntityFrameworkStores <IdentityContext>();
            builder.AddSignInManager <SignInManager <AppUser> >();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Token:Key"])),
                    ValidIssuer      = configuration["Token:Issuer"],
                    ValidateIssuer   = true,
                    ValidateAudience = false
                };
            });

            services.AddScoped <IProductService, ProductService>();
            services.AddScoped <ICartService, CartService>();
            services.AddScoped(typeof(IGenericService <>), typeof(GenericService <>));
            services.AddScoped <IJWTService, JWTService>();
            services.AddScoped <IOrderService, OrderService>();
            services.AddScoped <IUnitOfWork, UnitOfWork>();

            services.AddAutoMapper(typeof(MappingProfiles));

            services.Configure <ApiBehaviorOptions>(options =>
            {
                options.InvalidModelStateResponseFactory = actionContext =>
                {
                    var errors = actionContext.ModelState
                                 .Where(error => error.Value.Errors.Count > 0)
                                 .SelectMany(kvp => kvp.Value.Errors)
                                 .Select(modelError => modelError.ErrorMessage).ToArray();

                    var errorResponse = new APIValidationErrorResponse {
                        Errors = errors
                    };

                    return(new BadRequestObjectResult(errorResponse));
                };
            });

            services.AddCors(options => options.AddPolicy("CorsPolicy", policy =>
                                                          policy.AllowAnyHeader().AllowAnyMethod().WithOrigins("https://localhost:4200")));

            services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo {
                Title = "Comme API", Version = "v1"
            }));
        }