Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            ApplicationConfig config = new ApplicationConfig(Configuration);

            services.AddScoped(provider => config.DbContextConfiguration);

            services.AddDbContext <PizzaShopDbContext>((provider, builder) => { builder.UseSqlServer(provider.GetService <DbContextConfiguration>().ConnectionString); });
            services.AddEntityFrameworkSqlServer();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata      = false;
                options.SaveToken                 = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer      = Configuration["Jwt:Issuer"],
                    ValidAudience    = Configuration["Jwt:Audience"],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:SecretKey"])),
                    ClockSkew        = TimeSpan.Zero
                };
                services.AddCors();
            });

            services.AddAuthorization(config =>
            {
                config.AddPolicy(Policies.Admin, Policies.AdminPolicy());
                config.AddPolicy(Policies.Client, Policies.UserPolicy());
            });

            services.AddScoped <IAccountRepository, AccountRepository>();
            services.AddScoped <IAccountService, AccountService>();

            services.AddScoped <IPizzaService, PizzaService>();
            services.AddScoped <IPizzaRepository, PizzaRepository>();

            services.AddScoped <IRepository <Pizza>, GenericRepository <Pizza> >();
            services.AddScoped <IPizzaIngredientRepository, PizzaIngredientRepository>();
            services.AddScoped <IRepository <Price>, GenericRepository <Price> >();

            services.AddScoped <IRepository <Ingredient>, GenericRepository <Ingredient> >();
            services.AddScoped <IIngredientService, IngredientSerivce>();
            services.AddScoped <IRepository <Order>, GenericRepository <Order> >();
            services.AddScoped <IOrderService, OrderService>();
            services.AddScoped <IRepository <OrderPrice>, GenericRepository <OrderPrice> >();


            services.AddControllersWithViews();
            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });
        }