SE-23-Lisov-N.A-SoDM/RentalBusiness/Program.cs

51 lines
1.6 KiB
C#

using Microsoft.EntityFrameworkCore;
using Npgsql;
using RentalBusiness.AuxilaryElements.BusinessLogic;
using RentalBusiness.Enums;
using RentalBusiness.Models;
var builder = WebApplication.CreateBuilder(args);
var dataSourceBuilder = new NpgsqlDataSourceBuilder("Host=localhost;Database=local;Username=postgres;Password=postgres");
dataSourceBuilder.MapEnum<CarStatus>();
dataSourceBuilder.MapEnum<ContractStatus>();
await using var dataSource = dataSourceBuilder.Build();
await using var connection = await dataSource.OpenConnectionAsync();
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<RentalbusinessContext>(options => options.UseNpgsql(builder.Configuration.GetConnectionString("ASPDB")));
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
using (var context = new RentalbusinessContext(services.GetRequiredService<DbContextOptions<RentalbusinessContext>>()))
{
if(!context.Models.Any() && !context.Brands.Any() && !context.Customers.Any() && !context.Storages.Any())
{
await SeedData.InitializeBrands(connection);
await SeedData.InitializeModels(connection);
await SeedData.InitializeCustomers(connection);
await SeedData.InitializeCars(connection);
}
}
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();