|
| 1 | +using Bogus; |
| 2 | +using Inflow.Domain.Entities; |
| 3 | +using Inflow.Infrastructure; |
| 4 | +using Microsoft.EntityFrameworkCore; |
| 5 | + |
| 6 | +namespace Inflow.Api.Extensions |
| 7 | +{ |
| 8 | + public static class DatabaseSeeder |
| 9 | + { |
| 10 | + private static Faker _faker = new Faker(); |
| 11 | + |
| 12 | + public static void SeedDatabase(this IServiceCollection _, IServiceProvider serviceProvider) |
| 13 | + { |
| 14 | + var options = serviceProvider.GetRequiredService<DbContextOptions<InflowDbContext>>(); |
| 15 | + using var context = new InflowDbContext(options); |
| 16 | + |
| 17 | + CreateCategories(context); |
| 18 | + CreateProducts(context); |
| 19 | + CreateCustomers(context); |
| 20 | + CreateSales(context); |
| 21 | + CreateSaleItems(context); |
| 22 | + CreateSuppliers(context); |
| 23 | + CreateSupplies(context); |
| 24 | + CreateSupplyItems(context); |
| 25 | + EditPriceForAllEntities(context); |
| 26 | + } |
| 27 | + |
| 28 | + private static void CreateCategories(InflowDbContext context) |
| 29 | + { |
| 30 | + if (context.Categories.Any()) return; |
| 31 | + |
| 32 | + List<string> categoryNames = new(); |
| 33 | + List<Category> categories = new(); |
| 34 | + |
| 35 | + for (int i = 0; i < 25; i++) |
| 36 | + { |
| 37 | + var categoryName = _faker.Commerce |
| 38 | + .Categories(1) |
| 39 | + .First() |
| 40 | + .FirstLetterToUpper(); |
| 41 | + int attempts = 0; |
| 42 | + |
| 43 | + while (categoryNames.Contains(categoryName) && attempts < 100) |
| 44 | + { |
| 45 | + categoryName = _faker.Commerce |
| 46 | + .Categories(1) |
| 47 | + .First() |
| 48 | + .FirstLetterToUpper(); |
| 49 | + attempts++; |
| 50 | + } |
| 51 | + |
| 52 | + categoryNames.Add(categoryName); |
| 53 | + categories.Add(new Category |
| 54 | + { |
| 55 | + Name = categoryName, |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + context.Categories.AddRange(categories); |
| 60 | + context.SaveChanges(); |
| 61 | + } |
| 62 | + public static void CreateProducts(InflowDbContext context) |
| 63 | + { |
| 64 | + if (context.Products.Any()) return; |
| 65 | + |
| 66 | + var categories = context.Categories.ToList(); |
| 67 | + var productNames = new List<string>(); |
| 68 | + var products = new List<Product>(); |
| 69 | + |
| 70 | + foreach (var category in categories) |
| 71 | + { |
| 72 | + var productsCount = new Random().Next(5, 10); |
| 73 | + |
| 74 | + for (int i = 0; i < productsCount; i++) |
| 75 | + { |
| 76 | + var quantityInStock = new Random().Next(5, 10); |
| 77 | + var productName = _faker.Commerce.ProductName().FirstLetterToUpper(); |
| 78 | + |
| 79 | + int attempts = 0; |
| 80 | + |
| 81 | + while (productNames.Contains(productName) && attempts < 100) |
| 82 | + { |
| 83 | + productName = _faker.Commerce |
| 84 | + .ProductName() |
| 85 | + .FirstLetterToUpper(); |
| 86 | + |
| 87 | + attempts++; |
| 88 | + } |
| 89 | + |
| 90 | + productNames.Add(productName); |
| 91 | + |
| 92 | + products.Add(new Product |
| 93 | + { |
| 94 | + Name = productName, |
| 95 | + Description = _faker.Commerce.ProductDescription(), |
| 96 | + Price = _faker.Random.Decimal(10_000, 200_000), |
| 97 | + ExpireDate = _faker.Date.Between(DateTime.Now.AddYears(-3), DateTime.Now), |
| 98 | + QuantityInStock = quantityInStock, |
| 99 | + CategoryId = category.Id, |
| 100 | + }); |
| 101 | + } |
| 102 | + } |
| 103 | + context.Products.AddRange(products); |
| 104 | + context.SaveChanges(); |
| 105 | + } |
| 106 | + private static void CreateCustomers(InflowDbContext context) |
| 107 | + { |
| 108 | + if (context.Customers.Any()) return; |
| 109 | + List<Customer> customers = new List<Customer>(); |
| 110 | + |
| 111 | + for (int i = 0; i < 100; i++) |
| 112 | + { |
| 113 | + customers.Add(new Customer() |
| 114 | + { |
| 115 | + FirstName = _faker.Name.FirstName(), |
| 116 | + LastName = _faker.Name.LastName(), |
| 117 | + PhoneNumber = _faker.Phone.PhoneNumber("+998-(##) ###-##-##") |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + context.Customers.AddRange(customers); |
| 122 | + context.SaveChanges(); |
| 123 | + } |
| 124 | + private static void CreateSales(InflowDbContext context) |
| 125 | + { |
| 126 | + if (context.Sales.Any()) return; |
| 127 | + |
| 128 | + var customers = context.Customers.ToList(); |
| 129 | + List<Sale> sales = new List<Sale>(); |
| 130 | + |
| 131 | + foreach (var customer in customers) |
| 132 | + { |
| 133 | + int salesCount = new Random().Next(3, 6); |
| 134 | + for (int i = 0; i < salesCount; i++) |
| 135 | + { |
| 136 | + sales.Add(new Sale() |
| 137 | + { |
| 138 | + CustomerId = customer.Id, |
| 139 | + SaleDate = _faker.Date.Between(DateTime.Now.AddYears(-2), DateTime.Now), |
| 140 | + }); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + context.Sales.AddRange(sales); |
| 145 | + context.SaveChanges(); |
| 146 | + } |
| 147 | + private static void CreateSaleItems(InflowDbContext context) |
| 148 | + { |
| 149 | + if (context.SaleItems.Any()) return; |
| 150 | + |
| 151 | + var sales = context.Sales.ToList(); |
| 152 | + var products = context.Products.ToList(); |
| 153 | + List<SaleItem> saleItems = new List<SaleItem>(); |
| 154 | + |
| 155 | + foreach (var sale in sales) |
| 156 | + { |
| 157 | + int saleItemsCount = new Random().Next(5, 10); |
| 158 | + |
| 159 | + for (int i = 0; i < saleItemsCount; i++) |
| 160 | + { |
| 161 | + var randomProduct = _faker.PickRandom(products); |
| 162 | + |
| 163 | + var quantity = new Random().Next(10, 20); |
| 164 | + |
| 165 | + saleItems.Add(new SaleItem() |
| 166 | + { |
| 167 | + ProductId = randomProduct.Id, |
| 168 | + SaleId = sale.Id, |
| 169 | + Quantity = quantity, |
| 170 | + UnitPrice = randomProduct.Price * (decimal)1.25, |
| 171 | + }); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + context.SaleItems.AddRange(saleItems); |
| 176 | + context.SaveChanges(); |
| 177 | + } |
| 178 | + private static void CreateSuppliers(InflowDbContext context) |
| 179 | + { |
| 180 | + if (context.Suppliers.Any()) return; |
| 181 | + List<Supplier> suppliers = new List<Supplier>(); |
| 182 | + |
| 183 | + for (int i = 0; i < 50; i++) |
| 184 | + { |
| 185 | + suppliers.Add(new Supplier() |
| 186 | + { |
| 187 | + FirstName = _faker.Name.FirstName(), |
| 188 | + LastName = _faker.Name.LastName(), |
| 189 | + PhoneNumber = _faker.Phone.PhoneNumber("+998-(##) ###-##-##"), |
| 190 | + Company = _faker.Company.CompanyName(), |
| 191 | + }); |
| 192 | + } |
| 193 | + |
| 194 | + context.Suppliers.AddRange(suppliers); |
| 195 | + context.SaveChanges(); |
| 196 | + } |
| 197 | + private static void CreateSupplies(InflowDbContext context) |
| 198 | + { |
| 199 | + if (context.Supplies.Any()) return; |
| 200 | + |
| 201 | + var suppliers = context.Suppliers.ToList(); |
| 202 | + List<Supply> supplies = new List<Supply>(); |
| 203 | + |
| 204 | + foreach (var supplier in suppliers) |
| 205 | + { |
| 206 | + int suppliesCount = new Random().Next(10, 12); |
| 207 | + for (int i = 0; i < suppliesCount; i++) |
| 208 | + { |
| 209 | + supplies.Add(new Supply() |
| 210 | + { |
| 211 | + SupplierId = supplier.Id, |
| 212 | + SupplyDate = _faker.Date.Between(DateTime.Now.AddYears(-2), DateTime.Now), |
| 213 | + }); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + context.Supplies.AddRange(supplies); |
| 218 | + context.SaveChanges(); |
| 219 | + } |
| 220 | + private static void CreateSupplyItems(InflowDbContext context) |
| 221 | + { |
| 222 | + if (context.SupplyItems.Any()) return; |
| 223 | + |
| 224 | + var supplies = context.Supplies.ToList(); |
| 225 | + var products = context.Products.ToList(); |
| 226 | + List<SupplyItem> supplyItems = new List<SupplyItem>(); |
| 227 | + |
| 228 | + foreach (var supply in supplies) |
| 229 | + { |
| 230 | + int supplyItemsCount = new Random().Next(20, 30); |
| 231 | + |
| 232 | + for (int i = 0; i < supplyItemsCount; i++) |
| 233 | + { |
| 234 | + var randomProduct = _faker.PickRandom(products); |
| 235 | + |
| 236 | + var quantity = new Random().Next(5, 10); |
| 237 | + |
| 238 | + supplyItems.Add(new SupplyItem() |
| 239 | + { |
| 240 | + ProductId = randomProduct.Id, |
| 241 | + SupplyId = supply.Id, |
| 242 | + Quantity = quantity, |
| 243 | + UnitPrice = randomProduct.Price * (decimal)0.8 |
| 244 | + }); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + context.SupplyItems.AddRange(supplyItems); |
| 249 | + context.SaveChanges(); |
| 250 | + } |
| 251 | + private static void EditPriceForAllEntities(InflowDbContext context) |
| 252 | + { |
| 253 | + var products = context.Products.ToList(); |
| 254 | + if (products[1].Price >= 20_000) |
| 255 | + { |
| 256 | + foreach (var product in products) |
| 257 | + { |
| 258 | + var price = product.Price / 12450; |
| 259 | + product.Price = price; |
| 260 | + context.Products.Update(product); |
| 261 | + } |
| 262 | + var saleitems = context.SaleItems.ToList(); |
| 263 | + foreach (var sale in saleitems) |
| 264 | + { |
| 265 | + var price = sale.UnitPrice / 12450; |
| 266 | + sale.UnitPrice = price; |
| 267 | + context.SaleItems.Update(sale); |
| 268 | + } |
| 269 | + var supplyItem = context.SupplyItems.ToList(); |
| 270 | + foreach (var supply in supplyItem) |
| 271 | + { |
| 272 | + var price = supply.UnitPrice / 12450; |
| 273 | + supply.UnitPrice = price; |
| 274 | + context.SupplyItems.Update(supply); |
| 275 | + } |
| 276 | + context.SaveChanges(); |
| 277 | + } |
| 278 | + } |
| 279 | + } |
| 280 | +} |
0 commit comments