From 6c4ca2df3fca894a6054cdc51c0fd0d2d189fc43 Mon Sep 17 00:00:00 2001 From: "Daniel Mackay [SSW]" <2636640+danielmackay@users.noreply.github.com> Date: Thu, 16 May 2024 22:36:21 +1000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Heroes=20domain=20now=20has=20seed?= =?UTF-8?q?=20data=20(#305)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Heroes domain now has seed data --- src/Domain/Teams/Mission.cs | 5 +- .../ApplicationDbContextInitializer.cs | 122 ++++++++++++++++-- src/WebApi/appsettings.Development.json | 4 +- 3 files changed, 117 insertions(+), 14 deletions(-) diff --git a/src/Domain/Teams/Mission.cs b/src/Domain/Teams/Mission.cs index ee415773..f5733961 100644 --- a/src/Domain/Teams/Mission.cs +++ b/src/Domain/Teams/Mission.cs @@ -18,7 +18,10 @@ public class Mission : Entity internal static Mission Create(string description) { Guard.Against.NullOrWhiteSpace(description); - return new Mission { Description = description, Status = MissionStatus.InProgress }; + return new Mission + { + Id = new MissionId(Guid.NewGuid()), Description = description, Status = MissionStatus.InProgress + }; } internal void Complete() diff --git a/src/Infrastructure/Persistence/ApplicationDbContextInitializer.cs b/src/Infrastructure/Persistence/ApplicationDbContextInitializer.cs index de538864..449c2c18 100644 --- a/src/Infrastructure/Persistence/ApplicationDbContextInitializer.cs +++ b/src/Infrastructure/Persistence/ApplicationDbContextInitializer.cs @@ -2,6 +2,8 @@ using Microsoft.Extensions.Logging; using SSW.CleanArchitecture.Domain.TodoItems; using Bogus; +using SSW.CleanArchitecture.Domain.Heroes; +using SSW.CleanArchitecture.Domain.Teams; namespace SSW.CleanArchitecture.Infrastructure.Persistence; @@ -9,7 +11,58 @@ public class ApplicationDbContextInitializer( ILogger logger, ApplicationDbContext dbContext) { - private const int NumTodoItems = 20; + private readonly string[] _superHeroNames = + [ + "Superman", + "Batman", + "Wonder Woman", + "Flash", "Aquaman", + "Cyborg", + "Green Lantern", + "Shazam", + "Captain Marvel", + "Cyclops", + "Wolverine", + "Storm" + ]; + + private readonly string[] _superPowers = + [ + "Strength", + "Flight", + "Invulnerability", + "Speed", + "Heat Vision", + "X-Ray Vision", + "Hearing", + "Healing Factor", + "Agility", + "Stamina", + "Breath", + "Weapons", + "Intelligence" + ]; + + private readonly string[] _teamNames = + [ + "Marvel", + "Avengers", + "DC", + "Justice League", + "X-Men" + ]; + + private readonly string[] _missionNames = + [ + "Save the world", + "Rescue the hostages", + "Defeat the villain", + "Stop the bomb", + "Protect the city" + ]; + + private const int NumHeroes = 20; + private const int NumTeams = 5; public async Task InitializeAsync() { @@ -31,16 +84,8 @@ public async Task SeedAsync() { try { - if (dbContext.TodoItems.Any()) - return; - - var faker = new Faker() - .CustomInstantiator(f => TodoItem.Create( - f.Lorem.Sentence(3), f.Lorem.Sentence(10), f.Random.Enum(), f.Date.Future(1, DateTime.UtcNow))); - - var todoItems = faker.Generate(NumTodoItems); - await dbContext.TodoItems.AddRangeAsync(todoItems); - await dbContext.SaveChangesAsync(); + var heroes = await SeedHeroes(); + await SeedTeams(heroes); } catch (Exception e) { @@ -48,4 +93,59 @@ public async Task SeedAsync() throw; } } + + + private async Task> SeedHeroes() + { + if (dbContext.Heroes.Any()) + return []; + + var faker = new Faker() + .CustomInstantiator(f => + { + var name = f.PickRandom(_superHeroNames); + var hero = Hero.Create(name, name.Substring(0, 2)); + var powers = f.PickRandom(_superPowers, f.Random.Number(1, 3)) + .Select(p => new Power(p, f.Random.Number(1, 10))); + hero.UpdatePowers(powers); + return hero; + }); + + var heroes = faker.Generate(NumHeroes); + await dbContext.Heroes.AddRangeAsync(heroes); + await dbContext.SaveChangesAsync(); + + return heroes; + } + + private async Task SeedTeams(List heroes) + { + if (dbContext.Teams.Any()) + return; + + var faker = new Faker() + .CustomInstantiator(f => + { + var name = f.PickRandom(_teamNames); + var team = Team.Create(name); + var heroesToAdd = f.PickRandom(heroes, f.Random.Number(2, 5)); + + foreach (var hero in heroesToAdd) + team.AddHero(hero); + + var sendOnMission = f.Lorem.Random.Bool(); + + if (sendOnMission) + { + var missionName = f.PickRandom(_missionNames); + team.ExecuteMission(missionName); + } + + return team; + }); + + var teams = faker.Generate(NumTeams); + await dbContext.Teams.AddRangeAsync(teams); + await dbContext.SaveChangesAsync(); + } } \ No newline at end of file diff --git a/src/WebApi/appsettings.Development.json b/src/WebApi/appsettings.Development.json index 9c3f7079..2dfff79d 100644 --- a/src/WebApi/appsettings.Development.json +++ b/src/WebApi/appsettings.Development.json @@ -8,8 +8,8 @@ // NOTE: If any of your connection strings real passwords, you should delete them and put them in the user secrets file instead. "ConnectionStrings": { // Local DB - "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=CleanArchitecture;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True" + //"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=CleanArchitecture;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True" // Docker DB - //"DefaultConnection": "Server=localhost,1433;Initial Catalog=CleanArchitecture;Persist Security Info=False;User ID=sa;Password=yourStrong(!)Password;MultipleActiveResultSets=True;TrustServerCertificate=True;Connection Timeout=30;" + "DefaultConnection": "Server=localhost,1433;Initial Catalog=CleanArchitecture;Persist Security Info=False;User ID=sa;Password=yourStrong(!)Password;MultipleActiveResultSets=True;TrustServerCertificate=True;Connection Timeout=30;" } }