Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ApplicationDbContext context,
                              RoleManager <ApplicationRole> roleManager,
                              UserManager <ApplicationUser> userManager,
                              IUnitOfWork unitOfWork)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // global cors policy
            app.UseCors(x => x
                        .AllowAnyOrigin()
                        .AllowAnyMethod()
                        .AllowAnyHeader());

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            DataBaseSeeder.Seed(context, unitOfWork, userManager, roleManager);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Exemplo n.º 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            // todo: uncomment to use MySQL
            //services.AddDbContextPool<DataBaseContext>(
            //    options => options.UseMySql(Configuration.GetConnectionString("DefaultConnection")
            //));
            services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
            {
                builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            }));
            services.AddDbContextPool <DataBaseContext>(options => options.UseSqlServer(Configuration.GetConnectionString("dev")));

            DataBaseSeeder.SeedMedicine();


            services.AddScoped <IGetMedicinesHandler, GetMedicinesHandler>();
            services.AddScoped <IAddMedicineHandler, AddMedicineHandler>();
            services.AddScoped <IDeleteMedicineHandler, DeleteMedicineHandler>();
            services.AddScoped <IPutMedicineHandler, PutMedicineHandler>();

            services.AddScoped <IGetIntakeMomentHandler, GetIntakeMomentHandler>();
            services.AddScoped <IAddIntakeMomentHandler, AddIntakeMomentHandler>();
            services.AddScoped <IDeleteIntakeMomentHandler, DeleteIntakeMomentHandler>();
            services.AddScoped <IPutIntakeMomentHandler, PutIntakeMomentHandler>();
        }
        // IMyScopedService is injected into Invoke
        public async Task Invoke(HttpContext httpContext, DataBaseSeeder seeder)
        {
            await seeder.SeedData();

            await _next(httpContext);
        }
Exemplo n.º 4
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            #region Primary Keys

            modelBuilder.Entity <Brand>().HasKey(b => b.Id);
            modelBuilder.Entity <Country>().HasKey(c => c.Id);
            modelBuilder.Entity <Company>().HasKey(c => c.Id);
            modelBuilder.Entity <Corporation>().HasKey(c => c.Id);
            modelBuilder.Entity <DoorType>().HasKey(dt => dt.Id);
            modelBuilder.Entity <Driver>().HasKey(d => d.Id);
            modelBuilder.Entity <FuelCard>().HasKey(fc => fc.Id);
            modelBuilder.Entity <Model>().HasKey(m => m.Id);
            modelBuilder.Entity <EngineType>().HasKey(mt => mt.Id);
            modelBuilder.Entity <Record>().HasKey(r => r.Id);
            modelBuilder.Entity <Series>().HasKey(s => s.Id);
            modelBuilder.Entity <Supplier>().HasKey(s => s.Id);
            modelBuilder.Entity <Vehicle>().HasKey(v => v.Id);
            modelBuilder.Entity <ExteriorColor>().HasKey(ec => ec.Id);
            modelBuilder.Entity <InteriorColor>().HasKey(ic => ic.Id);

            #endregion

            #region OneToOne
            //Vehicles - FuelCard
            modelBuilder.Entity <Vehicle>()
            .HasOne(v => v.FuelCard)
            .WithOne(fc => fc.Vehicle)
            .HasForeignKey <FuelCard>(fc => fc.VehicleId)
            .OnDelete(DeleteBehavior.SetNull);

            modelBuilder.Entity <FuelCard>()
            .HasOne(fc => fc.Vehicle)
            .WithOne(v => v.FuelCard)
            .HasForeignKey <Vehicle>(v => v.FuelCardId)
            .OnDelete(DeleteBehavior.SetNull);

            //Driver - FuelCard
            modelBuilder.Entity <FuelCard>()
            .HasOne(fc => fc.Driver)
            .WithOne(d => d.FuelCard)
            .HasForeignKey <Driver>(d => d.FuelCardId)
            .OnDelete(DeleteBehavior.SetNull);

            modelBuilder.Entity <Driver>()
            .HasOne(d => d.FuelCard)
            .WithOne(fc => fc.Driver)
            .HasForeignKey <FuelCard>(fc => fc.DriverId)
            .OnDelete(DeleteBehavior.SetNull);

            //Record - FuelCard
            modelBuilder.Entity <Record>()
            .HasOne(r => r.FuelCard)
            .WithOne(fc => fc.Record)
            .HasForeignKey <FuelCard>(fc => fc.RecordId)
            .OnDelete(DeleteBehavior.SetNull);


            modelBuilder.Entity <FuelCard>()
            .HasOne(fc => fc.Record)
            .WithOne(r => r.FuelCard)
            .HasForeignKey <Record>(r => r.FuelCardId)
            .OnDelete(DeleteBehavior.SetNull);

            #endregion

            #region OneToMany
            //1 brand -> x EngineTypes
            modelBuilder.Entity <Brand>()
            .HasMany(b => b.EngineTypes)
            .WithOne(mt => mt.Brand)
            .HasForeignKey(mt => mt.BrandId);

            //1 brand -> x series
            modelBuilder.Entity <Brand>()
            .HasMany(b => b.Series)
            .WithOne(s => s.Brand)
            .HasForeignKey(s => s.BrandId);

            //1 brand -> x models
            modelBuilder.Entity <Brand>()
            .HasMany(b => b.Models)
            .WithOne(m => m.Brand)
            .HasForeignKey(m => m.BrandId);

            //1 brand -> x vehicles
            modelBuilder.Entity <Brand>()
            .HasMany(b => b.Vehicles)
            .WithOne(v => v.Brand)
            .HasForeignKey(v => v.BrandId);

            //1 model -> x vehicles
            modelBuilder.Entity <Model>()
            .HasMany(m => m.Vehicles)
            .WithOne(v => v.Model)
            .HasForeignKey(v => v.ModelId);

            //1 doortype -> x vehicles
            modelBuilder.Entity <DoorType>()
            .HasMany(d => d.Vehicles)
            .WithOne(v => v.DoorType)
            .HasForeignKey(v => v.DoorTypeId);

            //1 EngineType -> x vehicles
            modelBuilder.Entity <EngineType>()
            .HasMany(m => m.Vehicles)
            .WithOne(v => v.EngineType)
            .HasForeignKey(v => v.EngineTypeId);

            //1 Fueltype -> x vehicles
            modelBuilder.Entity <FuelType>()
            .HasMany(f => f.Vehicles)
            .WithOne(v => v.FuelType)
            .HasForeignKey(v => v.FuelTypeId);

            //1 ExteriorColor - x Vehicles
            modelBuilder.Entity <Vehicle>()
            .HasOne(v => v.ExteriorColor)
            .WithMany(ec => ec.Vehicles)
            .HasForeignKey(v => v.ExteriorColorId);


            //1 InteriorColor - x Vehicles
            modelBuilder.Entity <Vehicle>()
            .HasOne(v => v.InteriorColor)
            .WithMany(ic => ic.Vehicles)
            .HasForeignKey(v => v.InteriorColorId);

            //1 FuelCard - x Refills
            modelBuilder.Entity <Refill>()
            .HasOne(f => f.FuelCard)
            .WithMany(f => f.Refills)
            .HasForeignKey(r => r.FuelCardId);

            //1 City - x Refills
            modelBuilder.Entity <Refill>()
            .HasOne(r => r.City)
            .WithMany(c => c.Refills)
            .HasForeignKey(r => r.CityId);

            //1 Country - x Cities
            modelBuilder.Entity <City>()
            .HasOne(c => c.Country)
            .WithMany(c => c.Cities)
            .HasForeignKey(c => c.CountryId);

            #endregion

            #region config
            modelBuilder.Entity <Person>()
            .HasIndex(p => p.DriversLicenseNumber)
            .IsUnique();
            modelBuilder.Entity <Supplier>()
            .HasIndex(supplier => supplier.Name)
            .IsUnique();

            DataBaseSeeder.SeedData(modelBuilder);

            modelBuilder.Entity <Supplier>()
            .Property(s => s.Types)
            .HasConversion(
                v => string.Join(",", v.Select(a => a.ToString())),
                v => v.Split(',', StringSplitOptions.RemoveEmptyEntries)
                .Select(a => (SupplierType)Enum.Parse(typeof(SupplierType), a)).ToArray()
                );
            #endregion
        }