Skip to content

Commit

Permalink
re-introduced seeding for Catalog, Inventory is WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
erikshafer committed Jun 24, 2024
1 parent 740e397 commit e1e5eb9
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 87 deletions.
5 changes: 3 additions & 2 deletions src/Legacy/Legacy.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Legacy.Application;
using Legacy.Data;
using Legacy.Data.DbContexts;
using Legacy.Data.Seeds;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
Expand Down Expand Up @@ -66,9 +67,9 @@
var catalogDb = scope.ServiceProvider.GetRequiredService<CatalogDbContext>();
catalogDb.Database.EnsureDeleted();
catalogDb.Database.EnsureCreated();
// catalogDb.Seed();
var inventoryDb = scope.ServiceProvider.GetRequiredService<InventoryDbContext>();
// inventoryDb.Seed();
// inventoryDb.Database.EnsureDeleted();
inventoryDb.Database.EnsureCreated();

await app.RunAsync();

Expand Down
16 changes: 12 additions & 4 deletions src/Legacy/Legacy.Data/Configurations/AddressConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ public void Configure(EntityTypeBuilder<Address> builder)

builder.Property(e => e.AddressLine1)
.IsRequired()
.HasMaxLength(255);
.HasMaxLength(128);

builder.Property(e => e.AddressLine2)
.IsRequired(false)
.HasMaxLength(255);
.HasMaxLength(128);

builder.Property(e => e.City)
.IsRequired()
.HasMaxLength(255);
.HasMaxLength(128);

builder.Property(e => e.StateProvince)
.IsRequired()
.HasMaxLength(128);

builder.Property(e => e.PostalCode)
.IsRequired()
.HasMaxLength(64);

builder.Property(e => e.CountryId);
builder.HasOne<Country>(e => e.Country)
Expand All @@ -29,6 +37,6 @@ public void Configure(EntityTypeBuilder<Address> builder)

builder.Property(e => e.Phone)
.IsRequired(false)
.HasMaxLength(50);
.HasMaxLength(64);
}
}
13 changes: 13 additions & 0 deletions src/Legacy/Legacy.Data/DbContexts/CatalogDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Legacy.Data.Seeds;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
Expand Down Expand Up @@ -43,6 +44,18 @@ public CatalogDbContext()
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(CatalogDbContext).Assembly);

modelBuilder.Entity<Brand>()
.HasData(CatalogSeeder.GenerateBrands());

modelBuilder.Entity<Category>()
.HasData(CatalogSeeder.GenerateCategories());

modelBuilder.Entity<Item>()
.HasData(CatalogSeeder.GenerateItems());

modelBuilder.Entity<Restriction>()
.HasData(CatalogSeeder.GenerateRestrictions());
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/Legacy/Legacy.Data/DbContexts/InventoryDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Legacy.Data.Seeds;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
Expand Down Expand Up @@ -42,6 +43,15 @@ public InventoryDbContext()
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(InventoryDbContext).Assembly);

modelBuilder.Entity<Warehouse>()
.HasData(InventorySeeder.GenerateWarehouses());

modelBuilder.Entity<Inventory>()
.HasData(InventorySeeder.GenerateInventories());

modelBuilder.Entity<InventoryHistory>()
.HasData(InventorySeeder.GenerateInventoryHistories());
}
}

Expand Down
62 changes: 19 additions & 43 deletions src/Legacy/Legacy.Data/Seeds/CatalogSeeder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Bogus;
using Legacy.Data.DbContexts;
using Legacy.Domain.Entities;

namespace Legacy.Data.Seeds;
Expand All @@ -11,64 +10,37 @@ public static class CatalogSeeder
private const int MaxItems = 1_024;
private const int MaxRestrictions = 128;

public static void Seed(this CatalogDbContext dbContext)
{
// brands
if (dbContext.Brands.Any() is false)
{
var brands = GenerateBrands();
dbContext.AddRange(brands);
dbContext.SaveChanges();
}

// categories
if (dbContext.Categories.Any() is false)
{

var categories = GenerateCategories();
dbContext.AddRange(categories);
dbContext.SaveChanges();
}

// items
if (dbContext.Items.Any() is false)
{
var items = GenerateItems();
dbContext.AddRange(items);
dbContext.SaveChanges();
}

// restrictions
if (dbContext.Restrictions.Any() is false)
{
var restrictions = GenerateRestrictions();
dbContext.AddRange(restrictions);
dbContext.SaveChanges();
}
}

private static IEnumerable<Brand> GenerateBrands()
public static IEnumerable<Brand> GenerateBrands()
{
var id = 0;
var brandFaker = new Faker<Brand>()
.UseSeed(100)
.RuleFor(p => p.Id, f => ++id)
.RuleFor(p => p.Name, f => f.Company.CompanyName(0))
.RuleFor(p => p.PrimaryContactName, f => f.Name.FullName());
var brands = brandFaker.Generate(MaxBrands);
return brands;
}

private static IEnumerable<Category> GenerateCategories()
public static IEnumerable<Category> GenerateCategories()
{
var id = 0;
var categoryFaker = new Faker<Category>()
.UseSeed(100)
.RuleFor(p => p.Id, f => ++id)
.RuleFor(p => p.Name, f => f.Commerce.Department(1))
.RuleFor(p => p.Code, f => f.Random.AlphaNumeric(8).ToUpper())
.RuleFor(p => p.Description, f => f.Random.Words(4));
var categories = categoryFaker.Generate(MaxCategories);
return categories;
}

private static IEnumerable<Item> GenerateItems()
public static IEnumerable<Item> GenerateItems()
{
var id = 0;
var itemFaker = new Faker<Item>()
.UseSeed(1_000)
.RuleFor(p => p.Id, f => ++id)
.RuleFor(p => p.Name, f => f.Commerce.ProductName())
.RuleFor(p => p.Description, f => f.Commerce.ProductDescription())
.RuleFor(p => p.Color, f => f.Commerce.Color())
Expand All @@ -85,18 +57,22 @@ private static IEnumerable<Item> GenerateItems()
.RuleFor(p => p.Discontinued, f => f.Random.Bool(0.10f))
.RuleFor(p => p.IsVariant, f => f.Random.Bool(0.10f))
.RuleFor(p => p.ChildCouldChokeWarning, f => f.Random.Bool(0.02f))
// .Ignore(p => p.Brand)
.Ignore(p => p.Brand)
.RuleFor(p => p.BrandId, f => f.Random.Number(1, MaxBrands))
// .Ignore(p => p.Category)
.Ignore(p => p.Category)
.RuleFor(p => p.CategoryId, f => f.Random.Number(1, MaxBrands));
var items = itemFaker.Generate(MaxItems);
return items;
}

private static IEnumerable<Restriction> GenerateRestrictions()
public static IEnumerable<Restriction> GenerateRestrictions()
{
var id = 0;
var restrictionFaker = new Faker<Restriction>()
.UseSeed(1_000)
.RuleFor(p => p.Id, f => ++id)
.RuleFor(p => p.ItemId, f => f.Random.Number(1, MaxItems))
.Ignore(p => p.Item)
.RuleFor(p => p.IsRestricted, f => f.Random.Bool(0.35f))
.RuleFor(p => p.Reason, f => f.Lorem.Sentence(1, 2));
var restrictions = restrictionFaker.Generate(MaxRestrictions);
Expand Down
57 changes: 24 additions & 33 deletions src/Legacy/Legacy.Data/Seeds/InventorySeeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,42 @@ public static class InventorySeeder
{
private const int MaxInventories = 1_024;
private const int MaxInventoryHistories = 1_024;
private const int MaxWarehouses = 13;
private const int MaxQuantityOnHand = 24;
private const float ChanceOfOutOfStock = 0.15f;

public static void Seed(this InventoryDbContext dbContext)
{
// inventories
if (dbContext.Inventories.Any() is false)
{
var inventories = GenerateInventories();
dbContext.AddRange(inventories);
dbContext.SaveChanges();
}

// inventory histories
if (dbContext.InventoryHistories.Any() is false)
{
var inventoryHistories = GenerateInventoryHistories();
dbContext.AddRange(inventoryHistories);
dbContext.SaveChanges();
}

// warehouses
if (dbContext.Warehouses.Any() is false)
{
var warehouses = GenerateWarehouses();
dbContext.AddRange(warehouses);
dbContext.SaveChanges();
}
}
private const int MaxBrands = 64;
private const int MaxCategories = 512;
private const int MaxItems = 1_024;
private const int MaxRestrictions = 128;

private static IEnumerable<Inventory> GenerateInventories()
public static IEnumerable<Inventory> GenerateInventories()
{
var inventoryFaker = new Faker<Inventory>();
// TODO: inventory faker rules
var id = 0;
var inventoryFaker = new Faker<Inventory>()
.UseSeed(1_000)
.RuleFor(p => p.Id, f => ++id)
.RuleFor(p => p.ItemId, f => f.Random.Number(1, MaxItems))
.Ignore(p => p.Item)
.RuleFor(p => p.WarehouseId, f => f.Random.Number(1, MaxWarehouses))
.Ignore(p => p.Warehouse)
.RuleFor(p => p.Quantity, f => f.Random.Number(1, MaxQuantityOnHand).OrDefault(f, ChanceOfOutOfStock));
var inventories = inventoryFaker.Generate(MaxInventories);
return inventories;
}

private static IEnumerable<InventoryHistory> GenerateInventoryHistories()
public static IEnumerable<InventoryHistory> GenerateInventoryHistories()
{
var inventoryHistoriesFaker = new Faker<InventoryHistory>();
// TODO: inventory history faker rules
// TODO: more comprehensive inventory history faker rules
var id = 0;
var inventoryHistoriesFaker = new Faker<InventoryHistory>()
.UseSeed(1_000)
.RuleFor(p => p.Id, f => ++id);
var inventoryHistories = inventoryHistoriesFaker.Generate(MaxInventories);
return inventoryHistories;
}

private static IEnumerable<Warehouse> GenerateWarehouses()
public static IEnumerable<Warehouse> GenerateWarehouses()
{
var date = new DateTime(2019,02,02,00,00,00);

Expand Down
5 changes: 4 additions & 1 deletion src/Legacy/Legacy.Domain/Entities/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ public class Address : AuditableEntity

public string City { get; set; }

public string CountryId { get; set; }
public string StateProvince { get; set; }

public string PostalCode { get; set; }

public string CountryId { get; set; }
public Country Country { get; set; }

public string Phone { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions src/Legacy/Legacy.Domain/Entities/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ public class Inventory : AuditableEntity
{
public long Id { get; set; }
public int ItemId { get; set; }
// public Item Item { get; set; }
public Item Item { get; set; }
public int WarehouseId { get; set; }
// public Warehouse Warehouse { get; set; }
public Warehouse Warehouse { get; set; }
public int Quantity { get; set; }
}
4 changes: 2 additions & 2 deletions src/Legacy/Legacy.Domain/Entities/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class Item : AuditableEntity

public string Name { get; set; } = default!;
public int BrandId { get; set; }
// public Brand Brand { get; set; } = default!;
public Brand Brand { get; set; } = default!;
public int CategoryId { get; set; }
// public Category Category { get; set; } = default!;
public Category Category { get; set; } = default!;
public bool IsVariant { get; set; }
public int? IsVariantOf { get; set; }
public bool Discontinued { get; set; }
Expand Down

0 comments on commit e1e5eb9

Please sign in to comment.