diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index e1a83d65..38ab278c 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -2,9 +2,9 @@ name: Check Drive Build & Test on: push: - branches: [ "master" ] + branches: [ "master", "integration" ] pull_request: - branches: [ "master" ] + branches: [ "master", "integration" ] jobs: build: diff --git a/CheckDrive.Api/CheckDrive.Api/Helpers/DatabaseSeeder.cs b/CheckDrive.Api/CheckDrive.Api/Helpers/DatabaseSeeder.cs index 788e8267..878b9b0c 100644 --- a/CheckDrive.Api/CheckDrive.Api/Helpers/DatabaseSeeder.cs +++ b/CheckDrive.Api/CheckDrive.Api/Helpers/DatabaseSeeder.cs @@ -1,15 +1,16 @@ -using Bogus; -using CheckDrive.Domain.Entities; +using CheckDrive.Domain.Entities; +using CheckDrive.Domain.Enums; using CheckDrive.Domain.Interfaces; using CheckDrive.TestDataCreator; using CheckDrive.TestDataCreator.Configurations; using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore; +using System.Linq; namespace CheckDrive.Api.Helpers; public static class DatabaseSeeder { - private static Faker _faker = new Faker(); public static void SeedDatabase( ICheckDriveDbContext context, @@ -23,6 +24,32 @@ public static void SeedDatabase( CreateMechanics(context, userManager, options); CreateOperators(context, userManager, options); CreateDispatchers(context, userManager, options); + CreateDefaultOilMarks(context); + CreateCheckPoints(context, options); + CreateDoctorReviews(context, options); + CreateMechanicHandovers(context); + CreateOperatorReviews(context); + CreateMechanicAcceptances(context); + CreateDispatcherReviews(context); + } + + private static void CreateDefaultOilMarks(ICheckDriveDbContext context) + { + if (context.OilMarks.Any()) return; + + var oilMarks = new List + { + new OilMark { Name = "AI-80" }, + new OilMark { Name = "AI-91" }, + new OilMark { Name = "AI-92" }, + new OilMark { Name = "AI-95" }, + new OilMark { Name = "AI-100" }, + new OilMark { Name = "Diesel"}, + new OilMark { Name = "Gas" } + }; + + context.OilMarks.AddRange(oilMarks); + context.SaveChanges(); } private static void CreateCars(ICheckDriveDbContext context, DataSeedOptions options) @@ -251,812 +278,209 @@ private static void CreateDispatchers(ICheckDriveDbContext context, UserManager< context.SaveChanges(); } - //private static void CreateEmployees(ICheckDriveDbContext context, UserManager userManager, DataSeedOptions options) - //{ - // if (context.Users.Any()) - // { - // return; - // } - - // var roles = context.Roles.Where(x => x.Name != "Admin").Select(x => x.Id).ToArray(); - // var uniqueEmployeesByName = new Dictionary(); - - // for (int i = 0; i < options.UsersCount; i++) - // { - // var employee = FakeDataGenerator.GetEmployee().Generate(); - // var account = FakeDataGenerator.GetAccount().Generate(); - // var role = _faker.PickRandom(roles); - - // if (uniqueEmployeesByName.TryAdd(employee.FirstName + employee.LastName, employee)) - // { - // var result = userManager.CreateAsync(account, $"Qwerty-{i}#"); - // var userRole = new IdentityUserRole { RoleId = role, UserId = account.Id }; - - // if (!result.Result.Succeeded) - // { - // continue; - // } - - // context.UserRoles.Add(userRole); - // employee.Account = account; - // context.Employees.Add(employee); - // } - // } - - // context.SaveChanges(); - //} - - //private static void CreateRoles(CheckDriveDbContext context) - //{ - // if (context.Roles.Any()) return; - - // List roles = new() - // { - // new Role() - // { - // Name = "Manager" - // }, - // new Role() - // { - // Name = "Haydovchi" - // }, - // new Role() - // { - // Name = "Shifokor" - // }, - // new Role() - // { - // Name = "Operator" - // }, - // new Role() - // { - // Name = "Dispetcher" - // }, - // new Role() - // { - // Name = "Mexanik" - // } - // }; - - // context.Roles.AddRange(roles); - // context.SaveChanges(); - //} - - //private static void CreateOilMarks(CheckDriveDbContext context) - //{ - // if (context.OilMarks.Any()) return; - - // List oilMarks = new() - // { - // new OilMark() - // { - // OilMark = "A80" - // }, - // new OilMark() - // { - // OilMark = "A91" - // }, - // new OilMark() - // { - // OilMark = "A92" - // }, - // new OilMark() - // { - // OilMark = "A95" - // }, - // new OilMark() - // { - // OilMark = "Diesel" - // }, - // }; - - // context.OilMarks.AddRange(oilMarks); - // context.SaveChanges(); - //} - - //private static void CreateAccounts(CheckDriveDbContext context) - //{ - // if (context.Accounts.Any()) return; - // List accounts = new() - // { - // new Account() - // { - // Bithdate = DateTime.Now, - // FirstName = "Botir", - // LastName = "Qodirov", - // Login = "manager", - // Password = "1234", - // PhoneNumber = "+998946152254", - // Address = "Sergeli", - // Position = "Menedjer", - // Passport = "AA-0000000", - // RoleId = 1, - // }, - // new Account() - // { - // Bithdate = new DateTime(1980, 7, 23), - // FirstName = "Razzakov", - // LastName = "Doniyor", - // Login = "Razzakov", - // Password = "1234", - // PhoneNumber = "+998991204020", - // Address = "Olmazor tumani, Qorasaroy tor ko`chasi, Qumloq 7 a uy", - // Position = "Toshkent shahar hokimi haydovchisi", - // Passport = "AA-9861688", - // RoleId = 2, - // }, - // new Account() - // { - // Bithdate = new DateTime(1990, 5, 15), - // FirstName = "Fayzullayev", - // LastName = "Dilmurod", - // Login = "Fayzullayev", - // Password = "1234", - // PhoneNumber = "+998909903939", - // Address = "Shayhantoxur tumani, Tarona 1-tor ko`chasi, 9 uy", - // Position = "Toshkent shahar hokimi haydovchisi", - // Passport = "AA-9460873", - // RoleId = 2, - // }, - // new Account() - // { - // Bithdate = new DateTime(1967, 12, 12), - // FirstName = "Xashimov", - // LastName = "Abdukarim", - // Login = "Xashimov", - // Password = "1234", - // PhoneNumber = "+998997966474", - // Address = "Sergeli tumani, Quruvchi ko'chasi,13-a uy, 13-xonadon", - // Position = "Toshkent shahar hokimining moliya-iqtisodiyot va kambag'allikni qisqartirish masalalari bo'yicha hirinchi o'rinbosari-shahar iqtisodiy taraqqiyotva kambag'allikni qisqartirish bosh boshqarmasi boshlig'ining haydovchisi", - // Passport = "AA-0645617", - // RoleId = 2, - // }, - // new Account() - // { - // Bithdate = new DateTime(1986, 7, 5), - // FirstName = "Kamilov", - // LastName = "Davron", - // Login = "Kamilov", - // Password = "1234", - // PhoneNumber = "+998998118180", - // Address = "Shayxontoxur tumani, Kolxoznaya ko'chasi, 85a-uy", - // Position = "Toshkent shahar hokimining qurilish masalalari bo'yicha maslahatchisi haydovchisi", - // Passport = "AA-4246023", - // RoleId = 2, - // }, - // new Account() - // { - // Bithdate = new DateTime(1971, 4, 8), - // FirstName = "Abduraxmanov", - // LastName = "Abdusalom", - // Login = "Abduraxmanov", - // Password = "1234", - // PhoneNumber = "+998935901754", - // Address = "Qibray tumani, O'nqo'rg'on QFY, A.Latifo'jayev 38 uy", - // Position = "Toshkent shahar hokimining qurilish, kommunikatsiyalarni rivojlantirish, ekologiya va ko'kalamzorlashtirish masalalari bo'yicha birinchi o'rinbosari haydovchisi", - // Passport = "AV-4067546", - // RoleId = 2, - // }, - // new Account() - // { - // Bithdate = new DateTime(1978, 12, 17), - // FirstName = "Magrubdjanov", - // LastName = "Abdusamat", - // Login = "Magrubdjanov", - // Password = "1234", - // PhoneNumber = "+998974442214", - // Address = "Yunusobod tumani, Xotira 3-tor ko'chasi, 4-uy", - // Position = "Toshkent shahar hokimining o'rinbosari, Investitsiyalar va tashqi savdo boshqarmasi haydovchisi", - // Passport = "AA-8593883", - // RoleId = 2, - // }, - // new Account() - // { - // Bithdate = new DateTime(1970, 1, 1), - // FirstName = "Xoshimov", - // LastName = "Shuxrat", - // Login = "Xoshimov", - // Password = "1234", - // PhoneNumber = "+998977857757", - // Address = "Toshkent shaxar, Olmazor tumani, Тошмухамеда 1-тор кўчаси 4-уй", - // Position = "Toshkent shahar hokimining Jamoat va diniy tashkilotlar bilan aloqalar bo'yicha o'rinbosari haydovchisi", - // Passport = "AB-2174559", - // RoleId = 2, - // }, - // new Account() - // { - // Bithdate = new DateTime(1986, 6, 20), - // FirstName = "Begmatov", - // LastName = "Sulton", - // Login = "Begmatov", - // Password = "1234", - // PhoneNumber = "+998998180949", - // Address = "Mirzo Ulug'bek tumani, Feruza mavesi, 63 uy, 42 xonadon", - // Position = "Toshkent shahar hokimining o'rinbosari oila va xotin-qizlar boshqarmasi boshlig'ining xaydovchisi", - // Passport = "AV-6449253", - // RoleId = 2, - // }, - // new Account() - // { - // Bithdate = new DateTime(1974, 2, 16), - // FirstName = "Davrukov", - // LastName = "Faxriddin", - // Login = "Davrukov", - // Password = "1234", - // PhoneNumber = "+998900000000", - // Address = "Mirzo Ulug'bek tumani, Muxitdinova ko'chasi, 3b-uy 23-xonadon", - // Position = "Toshkent shahar hokimining xokimining turizm va sport bo'yicha o'rinbosari", - // Passport = "AV-7430746", - // RoleId = 2, - // }, - // new Account() - // { - // Bithdate = new DateTime(1958, 7, 20), - // FirstName = "Pirmetov", - // LastName = "Shuxrat", - // Login = "Pirmetov", - // Password = "1234", - // PhoneNumber = "+998909506381", - // Address = "Mirzo Ulug'bek tumani, TTZ-2 mavzesi 64-uy, 22-xonadon", - // Position = "Toshkent shahar hokimligi haydovchisi", - // Passport = "AA-4590867", - // RoleId = 2, - // }, - // new Account() - // { - // Bithdate = new DateTime(1970, 5, 10), - // FirstName = "Aripov", - // LastName = "Maxmudjon", - // Login = "Aripov", - // Password = "1234", - // PhoneNumber = "+998935810606", - // Address = "Yakkasaroy tumani, 5- tor A.Qaxxor ko'chasi 1-uy", - // Position = "Toshkent shahar hokimligi haydovchisi", - // Passport = "AA-4380462", - // RoleId = 2, - // }, - // new Account() - // { - // Bithdate = new DateTime(1971, 9, 21), - // FirstName = "Tairov", - // LastName = "Sulton", - // Login = "Tairov", - // Password = "1234", - // PhoneNumber = "+998900000000", - // Address = "Toshkent shahar, Yunusobod tumani, 9-mavze, 10-uy. 60-xonadon", - // Position = "Toshkent shahar hokimligi ishlar boshqarmasi boshlig'i", - // Passport = "AA-4732149", - // RoleId = 2, - // }, - // }; - - // context.Accounts.AddRange(accounts); - // context.SaveChanges(); - - - //} - - //private static void CreateCars(CheckDriveDbContext context) - //{ - // if (context.Cars.Any()) return; - - // List cars = new() - // { - // new Car() - // { - // Model = "Mercedes-Benz S450", - // Color = "Qora", - // Number = "PAA 240", - // Mileage = 0, - // MeduimFuelConsumption = 18, - // FuelTankCapacity = 76, - // ManufacturedYear = 2019, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 43200 - // }, - // new Car() - // { - // Model = "Mercedes-Benz S500", - // Color = "Qora", - // Number = "01/010 DAV", - // Mileage = 0, - // MeduimFuelConsumption = 18, - // FuelTankCapacity = 76, - // ManufacturedYear = 2021, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 43200 - // }, - // new Car() - // { - // Model = "Tayota Prado", - // Color = "Qora", - // Number = "01/005 DAV", - // Mileage = 0, - // MeduimFuelConsumption = 15.1, - // FuelTankCapacity = 93, - // ManufacturedYear = 2017, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 36036 - // }, - // new Car() - // { - // Model = "Chevrolet Captiva", - // Color = "Qora", - // Number = "01/012 DAV", - // Mileage = 0, - // MeduimFuelConsumption = 12, - // FuelTankCapacity = 65, - // ManufacturedYear = 2018, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 36000 - // }, - // new Car() - // { - // Model = "Chevrolet Malibu-2", - // Color = "Qora", - // Number = "01/003 DAV", - // Mileage = 0, - // MeduimFuelConsumption = 11.5, - // FuelTankCapacity = 60, - // ManufacturedYear = 2021, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 36000 - // }, - // new Car() - // { - // Model = "Chevrolet Malibu-2", - // Color = "Qora", - // Number = "01/004 DAV", - // Mileage = 0, - // MeduimFuelConsumption = 11.5, - // FuelTankCapacity = 60, - // ManufacturedYear = 2020, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 36000 - // }, - // new Car() - // { - // Model = "Chevrolet Malibu-2", - // Color = "Qora", - // Number = "01/009 DAV", - // Mileage = 0, - // MeduimFuelConsumption = 11.5, - // FuelTankCapacity = 60, - // ManufacturedYear = 2020, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 36000 - // }, - // new Car() - // { - // Model = "Chevrolet Malibu-2", - // Color = "Qora", - // Number = "01/011 DAV", - // Mileage = 0, - // MeduimFuelConsumption = 11.5, - // FuelTankCapacity = 60, - // ManufacturedYear = 2018, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 36000 - // }, - // new Car() - // { - // Model = "Chevrolet Malibu-2", - // Color = "Qora", - // Number = "01/013 DAV", - // Mileage = 0, - // MeduimFuelConsumption = 11.5, - // FuelTankCapacity = 60, - // ManufacturedYear = 2019, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 36000 - // }, - // new Car() - // { - // Model = "Chevrolet Malibu-2", - // Color = "Qora", - // Number = "01/014 DAV", - // Mileage = 0, - // MeduimFuelConsumption = 11.5, - // FuelTankCapacity = 60, - // ManufacturedYear = 2020, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 36000 - // }, - // new Car() - // { - // Model = "Chevrolet Lacetti", - // Color = "Qora", - // Number = "01/234 THA", - // Mileage = 0, - // MeduimFuelConsumption = 10, - // FuelTankCapacity = 60, - // ManufacturedYear = 2020, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 27720 - // }, - // new Car() - // { - // Model = "Chevrolet Lacetti", - // Color = "Qora", - // Number = "01/213 THA", - // Mileage = 0, - // MeduimFuelConsumption = 10, - // FuelTankCapacity = 60, - // ManufacturedYear = 2020, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 27720 - // }, - // new Car() - // { - // Model = "Chevrolet Lacetti", - // Color = "Qora", - // Number = "01/219 THA", - // Mileage = 0, - // MeduimFuelConsumption = 10, - // FuelTankCapacity = 60, - // ManufacturedYear = 2017, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 27720 - // }, - // new Car() - // { - // Model = "Mercedes-Benz Sprinter", - // Color = "Qora", - // Number = "01/200 THA", - // Mileage = 28, - // MeduimFuelConsumption = 18.6, - // FuelTankCapacity = 75, - // ManufacturedYear = 2020, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 16128 - // }, - // new Car() - // { - // Model = "Mercedes-Benz Sprinter", - // Color = "Qora", - // Number = "01/300 THA", - // Mileage = 28, - // MeduimFuelConsumption = 18.6, - // FuelTankCapacity = 75, - // ManufacturedYear = 1996, - // RemainingFuel = 0, - // CarStatus = CarStatus.Free, - // OneYearMediumDistance = 16128 - // } - // }; - - // context.Cars.AddRange(cars); - // context.SaveChanges(); - //} - - //private static void CreateDrivers(CheckDriveDbContext context) - //{ - // if (context.Drivers.Any()) return; - - // var accounts = context.Accounts.ToList(); - // var roles = context.Roles.ToList(); - // List drivers = new(); - - // foreach (var account in accounts) - // { - // var driverRole = roles.FirstOrDefault(r => r.Name == "Haydovchi"); - // if (driverRole != null && account.RoleId == driverRole.Id) - // { - // drivers.Add(new Driver() - // { - // AccountId = account.Id, - // }); - // } - // } - - // context.Drivers.AddRange(drivers); - // context.SaveChanges(); - //} - - //private static void CreateDoctors(CheckDriveDbContext context) - //{ - // if (context.Doctors.Any()) return; - - // var accounts = context.Accounts.ToList(); - // var roles = context.Roles.ToList(); - // List doctors = new(); - - // foreach (var account in accounts) - // { - // var doctorRole = roles.FirstOrDefault(r => r.Name == "Shifokor"); - // if (doctorRole != null && account.RoleId == doctorRole.Id) - // { - // doctors.Add(new Doctor() - // { - // AccountId = account.Id, - // }); - // } - // } - - // context.Doctors.AddRange(doctors); - // context.SaveChanges(); - //} - - //private static void CreateOperators(CheckDriveDbContext context) - //{ - // if (context.Operators.Any()) return; - - // var accounts = context.Accounts.ToList(); - // var roles = context.Roles.ToList(); - // List operators = new(); - - // foreach (var account in accounts) - // { - // var operatorRole = roles.FirstOrDefault(r => r.Name == "Operator"); - // if (operatorRole != null && account.RoleId == operatorRole.Id) - // { - // operators.Add(new Operator() - // { - // AccountId = account.Id, - // }); - // } - // } - - // context.Operators.AddRange(operators); - // context.SaveChanges(); - //} - - //private static void CreateDispatchers(CheckDriveDbContext context) - //{ - // if (context.Dispatchers.Any()) return; - - // var accounts = context.Accounts.ToList(); - // var roles = context.Roles.ToList(); - // List dispatchers = new(); - - // foreach (var account in accounts) - // { - // var dispatcherRole = roles.FirstOrDefault(r => r.Name == "Dispetcher"); - // if (dispatcherRole != null && account.RoleId == dispatcherRole.Id) - // { - // dispatchers.Add(new Dispatcher() - // { - // AccountId = account.Id, - // }); - // } - // } - - // context.Dispatchers.AddRange(dispatchers); - // context.SaveChanges(); - //} - - //private static void CreateMechanics(CheckDriveDbContext context) - //{ - // if (context.Mechanics.Any()) return; - - // var accounts = context.Accounts.ToList(); - // var roles = context.Roles.ToList(); - // List mechanics = new(); - - // foreach (var account in accounts) - // { - // var mechanicRole = roles.FirstOrDefault(r => r.Name == "Mexanik"); - // if (mechanicRole != null && account.RoleId == mechanicRole.Id) - // { - // mechanics.Add(new Mechanic() - // { - // AccountId = account.Id, - // }); - // } - // } - - // context.Mechanics.AddRange(mechanics); - // context.SaveChanges(); - //} - - //private static void CreateDoctorReviews(CheckDriveDbContext context) - //{ - // if (context.DoctorReviews.Any()) return; - - // var drivers = context.Drivers.ToList(); - // var doctors = context.Doctors.ToList(); - // List doctorReviews = new(); - - // foreach (var doctor in doctors) - // { - // int doctorReviewsCount = new Random().Next(5, 10); - - // for (int i = 0; i < doctorReviewsCount; i++) - // { - // var randomDriver = _faker.PickRandom(drivers); - // var isHealthy = _faker.Random.Bool(); - // var comments = isHealthy ? "" : _faker.Lorem.Sentence(); - - // doctorReviews.Add(new DoctorReview() - // { - // IsHealthy = isHealthy, - // Date = _faker.Date.Between(DateTime.Now.AddYears(-1), DateTime.Now), - // Comments = comments, - // DoctorId = doctor.Id, - // DriverId = randomDriver.Id, - // }); - // } - // } - - // context.DoctorReviews.AddRange(doctorReviews); - // context.SaveChanges(); - //} - - //private static void CreateMechanicHandovers(CheckDriveDbContext context) - //{ - // if (context.MechanicsHandovers.Any()) return; - - // var cars = context.Cars.ToList(); - // var drivers = context.Drivers.ToList(); - // var mechanics = context.Mechanics.ToList(); - // List mechanicHandovers = new(); - - // foreach (var mechanic in mechanics) - // { - // var mechanicHandoversCount = new Random().Next(5, 10); - // for (int i = 0; i < mechanicHandoversCount; i++) - // { - // var randomDriver = _faker.PickRandom(drivers); - // var randomCar = _faker.PickRandom(cars); - // var isHanded = _faker.Random.Bool(); - // var comments = isHanded ? "" : _faker.Lorem.Sentence(); - // var status = _faker.Random.Enum(); - - // mechanicHandovers.Add(new MechanicHandover() - // { - // IsHanded = isHanded, - // Comments = comments, - // Date = _faker.Date.Between(DateTime.Now.AddYears(-1), DateTime.Now), - // Status = status, - // Distance = _faker.Random.Int(50, 100), - // MechanicId = mechanic.Id, - // DriverId = randomDriver.Id, - // CarId = randomCar.Id, - // }); - // } - // } - - // context.MechanicsHandovers.AddRange(mechanicHandovers); - // context.SaveChanges(); - //} - - //private static void CreateOperatorReviews(CheckDriveDbContext context) - //{ - // if (context.OperatorReviews.Any()) return; - - // var operators = context.Operators.ToList(); - // var drivers = context.Drivers.ToList(); - // var cars = context.Cars.ToList(); - // var oilMark = context.OilMarks.ToList(); - // List operatorReviews = new(); - - // foreach (var operatorr in operators) - // { - // var operatorReviewsCount = new Random().Next(5, 10); - // for (int i = 0; i < operatorReviewsCount; i++) - // { - // var randomDriver = _faker.PickRandom(drivers); - // var randomCar = _faker.PickRandom(cars); - // var randomOilMark = _faker.PickRandom(oilMark); - // var status = _faker.Random.Enum(); - // var isGiven = _faker.Random.Bool(); - // var comments = isGiven ? "" : _faker.Lorem.Sentence(); - - // operatorReviews.Add(new OperatorReview() - // { - // OilAmount = _faker.Random.Double(10, 20), - // Date = _faker.Date.Between(DateTime.Now.AddYears(-1), DateTime.Now), - // Status = status, - // IsGiven = isGiven, - // Comments = comments, - // OperatorId = operatorr.Id, - // DriverId = randomDriver.Id, - // CarId = randomCar.Id, - // OilMarkId = randomOilMark.Id, - // }); - // } - // } - - // context.OperatorReviews.AddRange(operatorReviews); - // context.SaveChanges(); - //} - //private static void CreateMechanicAcceptance(CheckDriveDbContext context) - //{ - // if (context.MechanicsAcceptances.Any()) return; - - // var cars = context.Cars.ToList(); - // var drivers = context.Drivers.ToList(); - // var mechanics = context.Mechanics.ToList(); - // List mechanicAcceptances = new(); - - // foreach (var mechanic in mechanics) - // { - // var mechanicAcceptancesCount = new Random().Next(5, 10); - // for (int i = 0; i < mechanicAcceptancesCount; i++) - // { - // var randomDriver = _faker.PickRandom(drivers); - // var randomCar = _faker.PickRandom(cars); - // var isAccepted = _faker.Random.Bool(); - // var comments = isAccepted ? "" : _faker.Lorem.Sentence(); - // var status = _faker.Random.Enum(); - - // mechanicAcceptances.Add(new MechanicAcceptance() - // { - // IsAccepted = isAccepted, - // Comments = comments, - // Date = _faker.Date.Between(DateTime.Now.AddYears(-1), DateTime.Now), - // Status = status, - // Distance = _faker.Random.Int(50, 100), - // MechanicId = mechanic.Id, - // DriverId = randomDriver.Id, - // CarId = randomCar.Id, - // }); - // } - // } - - // context.MechanicsAcceptances.AddRange(mechanicAcceptances); - // context.SaveChanges(); - //} - - //private static void CreateDispatcherReviews(CheckDriveDbContext context) - //{ - // if (context.DispatchersReviews.Any()) return; - - // var dispatchers = context.Dispatchers.ToList(); - // var mechanics = context.Mechanics.ToList(); - // var drivers = context.Drivers.ToList(); - // var operators = context.Operators.ToList(); - // var cars = context.Cars.ToList(); - // var mechanicHandovers = context.MechanicsHandovers.ToList(); - // var mechanicAcceptances = context.MechanicsAcceptances.ToList(); - // var operatorReviews = context.OperatorReviews.ToList(); - // List dispatcherReviews = new(); - - // foreach (var dispatcher in dispatchers) - // { - // var dispatcherReviewsCount = new Random().Next(5, 10); - // for (int i = 0; i < dispatcherReviewsCount; i++) - // { - // var randomDriver = _faker.PickRandom(drivers); - // var randomOperator = _faker.PickRandom(operators); - // var randomMechanics = _faker.PickRandom(mechanics); - // var randomCar = _faker.PickRandom(cars); - // var randomMechanicHandover = _faker.PickRandom(mechanicHandovers); - // var randomMechanicAcceptance = _faker.PickRandom(mechanicAcceptances); - // var randomOperatorReview = _faker.PickRandom(operatorReviews); - - // dispatcherReviews.Add(new DispatcherReview() - // { - // Date = _faker.Date.Between(DateTime.Now.AddYears(-1), DateTime.Now), - // FuelSpended = _faker.Random.Double(10, 20), - // DistanceCovered = _faker.Random.Int(50, 100), - // DispatcherId = dispatcher.Id, - // DriverId = randomDriver.Id, - // OperatorId = randomOperator.Id, - // MechanicId = randomMechanics.Id, - // CarId = randomCar.Id, - // MechanicAcceptanceId = randomMechanicAcceptance.Id, - // MechanicHandoverId = randomMechanicHandover.Id, - // OperatorReviewId = randomOperatorReview.Id, - // }); - // } - // } - - // context.DispatchersReviews.AddRange(dispatcherReviews); - // context.SaveChanges(); - //} + private static void CreateCheckPoints(ICheckDriveDbContext context, DataSeedOptions options) + { + if (context.CheckPoints.Any()) return; + + var checkPoints = FakeDataGenerator.GetCheckPoints() + .Generate(options.CheckPointsCount); + + foreach (var checkPoint in checkPoints) + { + checkPoint.DoctorReview = null!; + context.CheckPoints.Add(checkPoint); + } + + context.SaveChanges(); + } + + private static void CreateDoctorReviews(ICheckDriveDbContext context, DataSeedOptions options) + { + if (context.DoctorReviews.Any()) return; + + var checkPointId = context.CheckPoints.Select(x => x.Id).ToList(); + var driverIds = context.Drivers.Select(x => x.Id).ToList(); + var doctorIds = context.Doctors.Select(x => x.Id).ToList(); + var uniqueDoctorReviews = new Dictionary(); + + for (int i = 0; i < options.CheckPointsCount; i++) + { + var doctorReview = FakeDataGenerator.GetDoctorReview(doctorIds, driverIds).Generate(); + + doctorReview.CheckPointId = checkPointId[i]; + + if (uniqueDoctorReviews.TryAdd(doctorReview.CheckPointId, doctorReview)) + { + context.DoctorReviews.Add(doctorReview); + } + } + + context.SaveChanges(); + } + + private static void CreateMechanicHandovers(ICheckDriveDbContext context) + { + if (context.MechanicHandovers.Any()) return; + + var checkPoint = context.CheckPoints + .Include(x => x.DoctorReview) + .Where(x => x.Stage == CheckPointStage.DoctorReview) + .Where(x => x.DoctorReview.Status == ReviewStatus.Approved) + .ToList(); + + var mechanicIds = context.Mechanics.Select(x => x.Id).ToList(); + + var cars = context.Cars + .ToList(); + + var uniqueMechanicHandovers= new Dictionary(); + + for (int i = 0; i < checkPoint.Count; i++) + { + var mechanicHandover = FakeDataGenerator.GetMechanichandover(mechanicIds, cars).Generate(); + + mechanicHandover.CheckPointId = checkPoint[i].Id; + mechanicHandover.Date = checkPoint[i].StartDate; + + checkPoint[i].Stage = CheckPointStage.MechanicHandover; + + if(mechanicHandover.Status == ReviewStatus.RejectedByReviewer) + { + checkPoint[i].Status = CheckPointStatus.InterruptedByReviewerRejection; + } + + if (uniqueMechanicHandovers.TryAdd(mechanicHandover.CheckPointId, mechanicHandover)) + { + context.MechanicHandovers.Add(mechanicHandover); + } + } + + context.SaveChanges(); + } + + private static void CreateOperatorReviews(ICheckDriveDbContext context) + { + if (context.OperatorReviews.Any()) return; + + var checkPoint = context.CheckPoints + .Include(x => x.MechanicHandover) + .Where(x => x.Stage == CheckPointStage.MechanicHandover) + .Where(x => x.Status == CheckPointStatus.InProgress) + .ToList(); + + var operatorIds = context.Operators.Select(x => x.Id).ToList(); + + var oilMarkIds = context.OilMarks + .Select(x => x.Id) + .ToList(); + + var uniqueMechanicHandovers = new Dictionary(); + + for (int i = 0; i < checkPoint.Count; i++) + { + var operatorReview = FakeDataGenerator.GetOperatorReviews(operatorIds, oilMarkIds).Generate(); + + operatorReview.CheckPointId = checkPoint[i].Id; + operatorReview.Date = checkPoint[i].StartDate; + + checkPoint[i].Stage = CheckPointStage.OperatorReview; + + if (operatorReview.Status == ReviewStatus.RejectedByReviewer) + { + checkPoint[i].Status = CheckPointStatus.InterruptedByReviewerRejection; + } + + if (uniqueMechanicHandovers.TryAdd(operatorReview.CheckPointId, operatorReview)) + { + context.OperatorReviews.Add(operatorReview); + } + } + + context.SaveChanges(); + } + + private static void CreateMechanicAcceptances(ICheckDriveDbContext context) + { + if (context.MechanicAcceptances.Any()) return; + + var checkPoint = context.CheckPoints + .Where(x => x.Stage == CheckPointStage.OperatorReview) + .Where(x => x.Status == CheckPointStatus.InProgress) + .ToList(); + + var mechanicIds = context.Mechanics.Select(x => x.Id).ToList(); + + var uniqueMechanicAcceptance = new Dictionary(); + + for (int i = 0; i < checkPoint.Count; i++) + { + var mechanicAcceptance = FakeDataGenerator.GetMechanicAcceptance(mechanicIds).Generate(); + + mechanicAcceptance.CheckPointId = checkPoint[i].Id; + + var randomDate = new Random().Next(1, 20); + mechanicAcceptance.Date = checkPoint[i].StartDate.AddDays(randomDate); + + checkPoint[i].Stage = CheckPointStage.MechanicAcceptance; + + if (mechanicAcceptance.Status == ReviewStatus.RejectedByReviewer) + { + checkPoint[i].Status = CheckPointStatus.InterruptedByReviewerRejection; + } + + if (uniqueMechanicAcceptance.TryAdd(mechanicAcceptance.CheckPointId, mechanicAcceptance)) + { + context.MechanicAcceptances.Add(mechanicAcceptance); + } + } + + context.SaveChanges(); + } + + private static void CreateDispatcherReviews(ICheckDriveDbContext context) + { + if (context.DispatcherReviews.Any()) return; + + var checkPoints = context.CheckPoints + .Include(x => x.MechanicAcceptance) + .Where(x => x.Stage == CheckPointStage.MechanicAcceptance) + .Where(x => x.Status == CheckPointStatus.InProgress) + .ToList(); + + var dispatcherIds = context.Dispatchers.Select(x => x.Id).ToList(); + + var uniqueDispatcherReviews = new Dictionary(); + + for (int i = 0; i < checkPoints.Count; i++) + { + var dispatcherReview = FakeDataGenerator.GetDispatcherReview(dispatcherIds).Generate(); + + dispatcherReview.CheckPointId = checkPoints[i].Id; + dispatcherReview.Date = checkPoints[i].MechanicAcceptance!.Date; + + checkPoints[i].Stage = CheckPointStage.DispatcherReview; + + if (dispatcherReview.FuelConsumptionAdjustment.HasValue || dispatcherReview.DistanceTravelledAdjustment.HasValue) + { + checkPoints[i].Stage = CheckPointStage.ManagerReview; + checkPoints[i].Status = CheckPointStatus.PendingManagerReview; + } + else if (dispatcherReview.Status != ReviewStatus.Approved) + { + checkPoints[i].Status = CheckPointStatus.InterruptedByReviewerRejection; + } + else + { + checkPoints[i].Status = CheckPointStatus.Completed; + } + + + if (uniqueDispatcherReviews.TryAdd(dispatcherReview.CheckPointId, dispatcherReview)) + { + context.DispatcherReviews.Add(dispatcherReview); + } + } + + context.SaveChanges(); + } } diff --git a/CheckDrive.Api/CheckDrive.Api/appsettings.Testing.json b/CheckDrive.Api/CheckDrive.Api/appsettings.Testing.json index adc64dd0..498c4ea5 100644 --- a/CheckDrive.Api/CheckDrive.Api/appsettings.Testing.json +++ b/CheckDrive.Api/CheckDrive.Api/appsettings.Testing.json @@ -26,6 +26,7 @@ "DoctorsCount": 5, "MechanicsCount": 10, "OperatorsCount": 10, - "DispatchersCount": 10 + "DispatchersCount": 10, + "CheckPointsCount": 100 } } \ No newline at end of file diff --git a/CheckDrive.Api/CheckDrive.TestDataCreator/Configurations/DataSeedOptions.cs b/CheckDrive.Api/CheckDrive.TestDataCreator/Configurations/DataSeedOptions.cs index 19c2dc80..677f4640 100644 --- a/CheckDrive.Api/CheckDrive.TestDataCreator/Configurations/DataSeedOptions.cs +++ b/CheckDrive.Api/CheckDrive.TestDataCreator/Configurations/DataSeedOptions.cs @@ -12,4 +12,5 @@ public class DataSeedOptions public int MechanicsCount { get; set; } public int OperatorsCount { get; set; } public int DispatchersCount { get; set; } + public int CheckPointsCount { get; set; } } diff --git a/CheckDrive.Api/CheckDrive.TestDataCreator/FakeDataGenerator.cs b/CheckDrive.Api/CheckDrive.TestDataCreator/FakeDataGenerator.cs index 41d3fd87..232fb4e3 100644 --- a/CheckDrive.Api/CheckDrive.TestDataCreator/FakeDataGenerator.cs +++ b/CheckDrive.Api/CheckDrive.TestDataCreator/FakeDataGenerator.cs @@ -39,4 +39,52 @@ public static class FakeDataGenerator public static Faker GetOilMark() => new Faker() .RuleFor(x => x.Name, f => f.PickRandom(_oilMarks)); + + public static Faker GetCheckPoints() => new Faker() + .RuleFor(x => x.StartDate, f => f.Date.Past(5)) + .RuleFor(x => x.Stage, CheckPointStage.DoctorReview) + .RuleFor(x => x.Status, f => f.Random.Bool(0.98f) ? CheckPointStatus.InProgress : CheckPointStatus.InterruptedByReviewerRejection); + + public static Faker GetDoctorReview( + List doctorIds, + List driverIds) => new Faker() + .RuleFor(x => x.DoctorId, f => f.PickRandom(doctorIds)) + .RuleFor(x => x.DriverId, f => f.PickRandom(driverIds)) + .RuleFor(x => x.Date, f => f.Date.Past(3)) + .RuleFor(x => x.Notes, f => f.Lorem.Sentence(2, 4)) + .RuleFor(x => x.Status, f => f.Random.Bool(0.98f) ? ReviewStatus.Approved : ReviewStatus.RejectedByReviewer); + + public static Faker GetMechanichandover( + List mechanicIds, + List cars) => new Faker() + .RuleFor(x => x.MechanicId, f => f.PickRandom(mechanicIds)) + .RuleFor(x => x.CarId, f => f.PickRandom(cars).Id) + .RuleFor(x => x.InitialMileage, f => f.PickRandom(cars).Mileage + f.Random.Int(1,10)) + .RuleFor(x => x.Notes, f => f.Lorem.Sentence(2, 4)) + .RuleFor(x => x.Status, f => f.Random.Bool(0.98f) ? ReviewStatus.PendingDriverApproval : ReviewStatus.RejectedByReviewer); + + public static Faker GetOperatorReviews( + List operatorIds, + List oilMarkIds) => new Faker() + .RuleFor(x => x.OperatorId, f => f.PickRandom(operatorIds)) + .RuleFor(x => x.OilMarkId, f => f.PickRandom(oilMarkIds)) + .RuleFor(x => x.InitialOilAmount, f => f.Random.Int(10,60)) + .RuleFor(x => x.OilRefillAmount, f => f.Random.Int(0,60)) + .RuleFor(x => x.Notes, f => f.Lorem.Sentence(2, 4)) + .RuleFor(x => x.Status, f => f.Random.Bool(0.98f) ? ReviewStatus.PendingDriverApproval : ReviewStatus.RejectedByReviewer); + + public static Faker GetMechanicAcceptance(List mechanicIds) => new Faker() + .RuleFor(x => x.MechanicId, f => f.PickRandom(mechanicIds)) + .RuleFor(x => x.FinalMileage, f => f.Random.Int(10_000,100_000)) + .RuleFor(x => x.RemainingFuelAmount, f => f.Random.Int(10, 60)) + .RuleFor(x => x.Notes, f => f.Lorem.Sentence(2, 4)) + .RuleFor(x => x.Status, f => f.Random.Bool(0.98f) ? ReviewStatus.PendingDriverApproval : ReviewStatus.RejectedByReviewer); + + public static Faker GetDispatcherReview(List dispatcherIds) => new Faker() + .RuleFor(x => x.DispatcherId, f => f.PickRandom(dispatcherIds)) + .RuleFor(x => x.DistanceTravelledAdjustment, f => f.Random.Bool(0.98f) ? null : f.Random.Int(10_000, 100_000)) + .RuleFor(x => x.FuelConsumptionAdjustment, f => f.Random.Bool(0.98f) ? null : f.Random.Int(10, 60)) + .RuleFor(x => x.Notes, f => f.Lorem.Sentence(2, 4)) + .RuleFor(x => x.Status, f => f.Random.Bool(0.98f) ? ReviewStatus.Approved : ReviewStatus.RejectedByReviewer); } +