Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legacy Entities and Catalog Data Seeding (2024-06-24) #49

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion EventSourcingEcommerce.sln
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShoppingCart.Api", "src\Ret
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Legacy.Domain", "src\Legacy\Legacy.Domain\Legacy.Domain.csproj", "{80CEA321-B1F7-49F2-B4C0-6A41DC539BD4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Legacy.Messages", "src\Legacy\Legacy.Messages\Legacy.Messages.csproj", "{3D478A6A-7CF9-452C-9D04-80E09C7F255E}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Legacy.SharedMessages", "src\Legacy\Legacy.SharedMessages\Legacy.SharedMessages.csproj", "{3D478A6A-7CF9-452C-9D04-80E09C7F255E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
4 changes: 1 addition & 3 deletions src/Legacy/Legacy.Api/Legacy.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EasyNetQ" Version="7.8.0" />
<PackageReference Include="EasyNetQ.Management.Client" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.6" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
Expand All @@ -20,7 +18,7 @@
<ItemGroup>
<ProjectReference Include="..\Legacy.Data\Legacy.Data.csproj" />
<ProjectReference Include="..\Legacy.Application\Legacy.Application.csproj" />
<ProjectReference Include="..\Legacy.Messages\Legacy.Messages.csproj" />
<ProjectReference Include="..\Legacy.SharedMessages\Legacy.SharedMessages.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 13 additions & 0 deletions src/Legacy/Legacy.Api/Models/PaginatedItems.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Legacy.Api.Models;

public class PaginatedItems<TEntity>(int pageIndex, int pageSize, long count, IEnumerable<TEntity> items)
where TEntity : class
{
public int PageIndex { get; } = pageIndex;

public int PageSize { get; } = pageSize;

public long Count { get; } = count;

public IEnumerable<TEntity> Items { get;} = items;
}
3 changes: 3 additions & 0 deletions src/Legacy/Legacy.Api/Models/PaginatedRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Legacy.Api.Models;

public record PaginatedRequest(int PageSize = 10, int PageIndex = 0);
4 changes: 2 additions & 2 deletions src/Legacy/Legacy.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Legacy.Application.Events.Events;
using Legacy.Data.DbContexts;
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using MediatR;
using Microsoft.EntityFrameworkCore;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Legacy.Application.Events.Inventory;
using Legacy.Data.DbContexts;
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using MediatR;
using Microsoft.EntityFrameworkCore;

Expand Down
18 changes: 13 additions & 5 deletions src/Legacy/Legacy.Data/Configurations/AddressConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/Configurations/BrandConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/Configurations/CartConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/Configurations/CategoryConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/Configurations/CountryConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/Configurations/CustomerConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/Configurations/InventoryConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/Configurations/ItemConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/Configurations/ListingConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/Configurations/OrderConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/Configurations/PaymentConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/Configurations/PromotionConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/Configurations/RestrictionConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/Configurations/WarehouseConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

Expand Down
15 changes: 14 additions & 1 deletion src/Legacy/Legacy.Data/DbContexts/CatalogDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Legacy.Data.Entities;
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
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/DbContexts/CustomersDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

Expand Down
12 changes: 11 additions & 1 deletion src/Legacy/Legacy.Data/DbContexts/InventoryDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Legacy.Data.Entities;
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
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/DbContexts/ListingDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

Expand Down
2 changes: 1 addition & 1 deletion src/Legacy/Legacy.Data/DbContexts/OrderingDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Legacy.Data.Entities;
using Legacy.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

Expand Down
4 changes: 4 additions & 0 deletions src/Legacy/Legacy.Data/Legacy.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Legacy.Domain\Legacy.Domain.csproj" />
</ItemGroup>

</Project>
64 changes: 20 additions & 44 deletions src/Legacy/Legacy.Data/Seeds/CatalogSeeder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Bogus;
using Legacy.Data.DbContexts;
using Legacy.Data.Entities;
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
Loading
Loading