autobus-api_old/AutobusApi.Infrastructure/DbInitializer.cs

516 lines
16 KiB
C#

using AutobusApi.Domain.Entities;
using AutobusApi.Domain.Enums;
using AutobusApi.Infrastructure.Data.Entities;
using AutobusApi.Infrastructure.Identity;
using AutoubsApi.Infrastructure.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Npgsql;
namespace AutobusApi.Infrastructure;
public static class DbInitializer
{
public static void Initialize(ApplicationDbContext dbContext, ApplicationIdentityDbContext identityDbContext)
{
if (dbContext.Database.IsRelational())
{
var domainAppliedMigrations = dbContext.Database.GetAppliedMigrations();
var identityAppliedMigrations = identityDbContext.Database.GetAppliedMigrations();
if (domainAppliedMigrations.Count() == 0)
{
dbContext.Database.Migrate();
// https://github.com/npgsql/efcore.pg/issues/292
dbContext.Database.OpenConnection();
((NpgsqlConnection)dbContext.Database.GetDbConnection()).ReloadTypes();
InitializeDomain(dbContext);
}
if (identityAppliedMigrations.Count() == 0)
{
identityDbContext.Database.Migrate();
InitializeIdentity(identityDbContext);
}
return;
}
dbContext.Database.EnsureCreated();
InitializeDomain(dbContext);
identityDbContext.Database.EnsureCreated();
InitializeIdentity(identityDbContext);
}
private static void InitializeDomain(ApplicationDbContext dbContext)
{
var country1 = new Country()
{
Name = "Country A"
};
dbContext.Countries.AddRange(new[] { country1 });
dbContext.SaveChanges();
var region1 = new Region()
{
Name = "Region A",
Country = country1
};
dbContext.Regions.AddRange(new[] { region1 });
dbContext.SaveChanges();
var city1 = new City()
{
Name = "City A",
Region = region1
};
var city2 = new City()
{
Name = "City B",
Region = region1
};
var city3 = new City()
{
Name = "City C",
Region = region1
};
var city4 = new City()
{
Name = "City D",
Region = region1
};
var city5 = new City()
{
Name = "City E",
Region = region1
};
var city6 = new City()
{
Name = "City F",
Region = region1
};
var city7 = new City()
{
Name = "City G",
Region = region1
};
var city8 = new City()
{
Name = "City H",
Region = region1
};
var city9 = new City()
{
Name = "City I",
Region = region1
};
var city10 = new City()
{
Name = "City J",
Region = region1
};
dbContext.Cities.AddRange(new[] { city1, city2, city3, city4, city5, city6, city7, city8, city9, city10 });
dbContext.SaveChanges();
var address1 = new Address()
{
Name = "address A",
Location = new Coordinates(11, 11),
VehicleType = VehicleType.Bus,
City = city1
};
var address2 = new Address()
{
Name = "address B",
Location = new Coordinates(22, 22),
VehicleType = VehicleType.Bus,
City = city2
};
var address3 = new Address()
{
Name = "address C",
Location = new Coordinates(33, 33),
VehicleType = VehicleType.Bus,
City = city3
};
var address4 = new Address()
{
Name = "address E",
Location = new Coordinates(44, 44),
VehicleType = VehicleType.Bus,
City = city4
};
var address5 = new Address()
{
Name = "address F",
Location = new Coordinates(55, 55),
VehicleType = VehicleType.Bus,
City = city5
};
var address6 = new Address()
{
Name = "address G",
Location = new Coordinates(66, 66),
VehicleType = VehicleType.Bus,
City = city6
};
var address7 = new Address()
{
Name = "address H",
Location = new Coordinates(77, 77),
VehicleType = VehicleType.Bus,
City = city7
};
var address8 = new Address()
{
Name = "address I",
Location = new Coordinates(88, 88),
VehicleType = VehicleType.Bus,
City = city8
};
var address9 = new Address()
{
Name = "address J",
Location = new Coordinates(99, 99),
VehicleType = VehicleType.Bus,
City = city9
};
var address10 = new Address()
{
Name = "address K",
Location = new Coordinates(110, 110),
VehicleType = VehicleType.Bus,
City = city10
};
dbContext.Addresses.AddRange(new[] { address1, address2, address3, address4, address5, address6, address7, address8, address9, address10 });
dbContext.SaveChanges();
var route1 = new Route()
{
RouteAddresses = new RouteAddress[]
{
new RouteAddress
{
Order = 1,
Address = address1
},
new RouteAddress
{
Order = 2,
Address = address2
},
new RouteAddress
{
Order = 3,
Address = address3
},
new RouteAddress
{
Order = 4,
Address = address4
},
new RouteAddress
{
Order = 5,
Address = address5
},
new RouteAddress
{
Order = 6,
Address = address8
}
}
};
var route2 = new Route()
{
RouteAddresses = new RouteAddress[]
{
new RouteAddress
{
Order = 1,
Address = address2
},
new RouteAddress
{
Order = 2,
Address = address3
},
new RouteAddress
{
Order = 3,
Address = address6
},
new RouteAddress
{
Order = 4,
Address = address7
}
}
};
var route3 = new Route()
{
RouteAddresses = new RouteAddress[]
{
new RouteAddress
{
Order = 1,
Address = address6
},
new RouteAddress
{
Order = 2,
Address = address8
},
new RouteAddress
{
Order = 3,
Address = address9
},
new RouteAddress
{
Order = 4,
Address = address10
}
}
};
dbContext.Routes.AddRange(new[] { route1, route2, route3 });
dbContext.SaveChanges();
var company1 = new Company
{
Name = "Smartlines",
LegalAddress = "1234 Spectrum Avenue, Metroville, XYZ 98765, United States",
ContactEmail = "support@smartlines.com",
ContactPhoneNumber = "+00000000000"
};
var company2 = new Company
{
Name = "ecobus",
LegalAddress = "5678 Galaxy Street, Cosmic City, ABC 54321, United States",
ContactEmail = "support@ecobus.com",
ContactPhoneNumber = "+00000000000"
};
dbContext.Companies.AddRange(new[] { company1, company2 });
dbContext.SaveChanges();
var vehicle1 = new Bus()
{
Number = "AC8236HK",
Model = "Mercedes-Benz Citaro",
Capacity = 20,
HasWC = true,
HasWiFi = false,
HasOutlets = true,
HasMultimedia = false,
HasClimateControl = true,
Company = company1
};
var vehicle2 = new Bus()
{
Number = "EP2566MK",
Model = "Van Hool TDX25",
Capacity = 25,
HasWC = true,
HasWiFi = false,
HasOutlets = true,
HasMultimedia = false,
HasClimateControl = true,
Company = company2
};
var vehicle3 = new Bus()
{
Number = "OK8634MC",
Model = "New Flyer Xcelsior",
Capacity = 30,
HasWC = true,
HasWiFi = false,
HasOutlets = true,
HasMultimedia = false,
HasClimateControl = true,
Company = company1
};
dbContext.Vehicles.AddRange(new[] { vehicle1, vehicle2, vehicle3 });
dbContext.SaveChanges();
var vehicleEnrollment1 = new VehicleEnrollment()
{
DepartureDateTimeUtc = DateTime.UtcNow,
Vehicle = vehicle1,
Route = route1,
RouteAddressDetails = new RouteAddressDetails[]
{
new RouteAddressDetails()
{
TimeToNextAddress = TimeSpan.FromMinutes(50),
CostToNextAddress = 25,
CurrentAddressStopTime = TimeSpan.FromMinutes(5),
RouteAddress = route1.RouteAddresses.ToArray()[0],
},
new RouteAddressDetails()
{
TimeToNextAddress = TimeSpan.FromMinutes(40),
CostToNextAddress = 20,
CurrentAddressStopTime = TimeSpan.FromMinutes(5),
RouteAddress = route1.RouteAddresses.ToArray()[1],
},
new RouteAddressDetails()
{
TimeToNextAddress = TimeSpan.FromMinutes(45),
CostToNextAddress = 23.33,
CurrentAddressStopTime = TimeSpan.FromMinutes(5),
RouteAddress = route1.RouteAddresses.ToArray()[2],
},
new RouteAddressDetails()
{
TimeToNextAddress = TimeSpan.FromMinutes(25),
CostToNextAddress = 15,
CurrentAddressStopTime = TimeSpan.FromMinutes(5),
RouteAddress = route1.RouteAddresses.ToArray()[3],
},
new RouteAddressDetails()
{
TimeToNextAddress = TimeSpan.FromMinutes(30),
CostToNextAddress = 17.5,
CurrentAddressStopTime = TimeSpan.FromMinutes(5),
RouteAddress = route1.RouteAddresses.ToArray()[4],
},
new RouteAddressDetails()
{
TimeToNextAddress = TimeSpan.FromMinutes(35),
CostToNextAddress = 19,
CurrentAddressStopTime = TimeSpan.FromMinutes(5),
RouteAddress = route1.RouteAddresses.ToArray()[5],
}
}
};
var vehicleEnrollment2 = new VehicleEnrollment()
{
DepartureDateTimeUtc = DateTime.UtcNow.AddMinutes(30),
Vehicle = vehicle2,
Route = route2,
RouteAddressDetails = new RouteAddressDetails[]
{
new RouteAddressDetails()
{
TimeToNextAddress = TimeSpan.FromMinutes(40),
CostToNextAddress = 25,
CurrentAddressStopTime = TimeSpan.FromMinutes(5),
RouteAddress = route2.RouteAddresses.ToArray()[0],
},
new RouteAddressDetails()
{
TimeToNextAddress = TimeSpan.FromMinutes(50),
CostToNextAddress = 20,
CurrentAddressStopTime = TimeSpan.FromMinutes(5),
RouteAddress = route2.RouteAddresses.ToArray()[1],
},
new RouteAddressDetails()
{
TimeToNextAddress = TimeSpan.FromMinutes(45),
CostToNextAddress = 23.33,
CurrentAddressStopTime = TimeSpan.FromMinutes(5),
RouteAddress = route2.RouteAddresses.ToArray()[2],
},
new RouteAddressDetails()
{
TimeToNextAddress = TimeSpan.FromMinutes(25),
CostToNextAddress = 15,
CurrentAddressStopTime = TimeSpan.FromMinutes(5),
RouteAddress = route2.RouteAddresses.ToArray()[3],
}
}
};
var vehicleEnrollment3 = new VehicleEnrollment()
{
DepartureDateTimeUtc = DateTime.UtcNow.AddMinutes(175),
Vehicle = vehicle3,
Route = route3,
RouteAddressDetails = new RouteAddressDetails[]
{
new RouteAddressDetails()
{
TimeToNextAddress = TimeSpan.FromMinutes(50),
CostToNextAddress = 25,
CurrentAddressStopTime = TimeSpan.FromMinutes(5),
RouteAddress = route2.RouteAddresses.ToArray()[0],
},
new RouteAddressDetails()
{
TimeToNextAddress = TimeSpan.FromMinutes(40),
CostToNextAddress = 20,
CurrentAddressStopTime = TimeSpan.FromMinutes(5),
RouteAddress = route2.RouteAddresses.ToArray()[1],
},
new RouteAddressDetails()
{
TimeToNextAddress = TimeSpan.FromMinutes(45),
CostToNextAddress = 23.33,
CurrentAddressStopTime = TimeSpan.FromMinutes(5),
RouteAddress = route2.RouteAddresses.ToArray()[2],
},
new RouteAddressDetails()
{
TimeToNextAddress = TimeSpan.FromMinutes(25),
CostToNextAddress = 15,
CurrentAddressStopTime = TimeSpan.FromMinutes(5),
RouteAddress = route2.RouteAddresses.ToArray()[3],
}
}
};
dbContext.VehicleEnrollments.AddRange(new[] { vehicleEnrollment1, vehicleEnrollment2/* , vehicleEnrollment3 */ });
dbContext.SaveChanges();
}
private static void InitializeIdentity(ApplicationIdentityDbContext identityDbContext)
{
foreach (var role in Enum.GetValues(typeof(IdentityRoles)).Cast<IdentityRoles>())
{
identityDbContext.Roles.Add(new IdentityRole<int>
{
Name = role.ToString(),
NormalizedName = role.ToString().ToUpper(),
ConcurrencyStamp = Guid.NewGuid().ToString()
});
}
identityDbContext.SaveChanges();
}
}