diff --git a/TicketOffice/Models/SeedData.cs b/TicketOffice/Models/SeedData.cs new file mode 100644 index 0000000..0d77aa0 --- /dev/null +++ b/TicketOffice/Models/SeedData.cs @@ -0,0 +1,96 @@ +using Microsoft.EntityFrameworkCore; +using TicketOffice.Data; + +namespace TicketOffice.Models; + +public class SeedData +{ + public static void Initialize(IServiceProvider serviceProvider) + { + using var context = + new TicketOfficeContext(serviceProvider.GetRequiredService>()); + + if (context == null) + { + throw new ArgumentNullException("Null TicketOfficeContext"); + } + + if (context.User.Any() | context.Route.Any() | context.City.Any() | context.Ticket.Any()) + { + return; // Data has been seeded + } + + context.Database.EnsureCreated(); + + context.User.AddRange(new User[] + { + new User + { + Email = "danylo.nazarko@nure.ua", + Password = "*Hashed Password*", + IsManager = false, + }, + new User + { + Email = "ruslan.shanin@nure.ua", + Password = "*Hashed Password*", + IsManager = false + } + }); + + context.Route.AddRange(new Route[] + { + new Route { + Number = "0001", + Capacity = 30, + Cities = new City[] + { + new City + { + Name = "Кремінна", + ArrivalTime = new DateTime(2022, 03, 28, 8, 15, 0), + DepartureTime = new DateTime(2022, 03, 28, 8, 35, 0), + }, + new City + { + Name = "Рубіжне", + ArrivalTime = new DateTime(2022, 03, 28, 9, 5, 0), + DepartureTime = new DateTime(2022, 03, 28, 9, 25, 0), + }, + new City + { + Name = "Сєвєродонецьк", + ArrivalTime = new DateTime(2022, 03, 28, 9, 55, 0) + } + }, + }, + new Route + { + Number = "0002", + Capacity = 25, + Cities = new City[] + { + new City + { + Name = "Сєвєродонецьк", + ArrivalTime = new DateTime(2022, 03, 28, 15, 55, 0), + DepartureTime = new DateTime(2022, 03, 28, 16, 15, 0), + }, + new City + { + Name = "Рубіжне", + ArrivalTime = new DateTime(2022, 03, 28, 16, 45, 0), + DepartureTime = new DateTime(2022, 03, 28, 17, 5, 0), + }, + new City + { + Name = "Кремінна", + ArrivalTime = new DateTime(2022, 03, 28, 17, 40, 0) + } + } + } + }); + + context.SaveChanges(); + } +} \ No newline at end of file diff --git a/TicketOffice/Program.cs b/TicketOffice/Program.cs index 5dee008..0bc60f0 100644 --- a/TicketOffice/Program.cs +++ b/TicketOffice/Program.cs @@ -1,6 +1,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using TicketOffice.Data; +using TicketOffice.Models; var builder = WebApplication.CreateBuilder(args); @@ -12,6 +13,10 @@ builder.Services.AddDbContext(options => var app = builder.Build(); +using var scope = app.Services.CreateScope(); +var services = scope.ServiceProvider; +SeedData.Initialize(services); + // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) {