Add database seeder with sample data and test guide

This commit is contained in:
steinhelge
2025-11-23 21:46:18 +01:00
parent 4b61bd7c29
commit 646ab0aef1
3 changed files with 569 additions and 0 deletions
+364
View File
@@ -0,0 +1,364 @@
using Hospitality.Domain.Entities;
using Hospitality.Domain.Enums;
using Hospitality.Infrastructure.Data;
namespace Hospitality.Backend.Data;
public static class DbSeeder
{
public static async Task SeedAsync(HospitalityDbContext context)
{
// Check if already seeded
if (context.Events.Any())
{
return;
}
// Create Events
var summerFestival = new Event
{
Id = Guid.NewGuid(),
Name = "Summer Festival 2025",
StartDate = DateTime.SpecifyKind(new DateTime(2025, 7, 1, 10, 0, 0), DateTimeKind.Utc),
EndDate = DateTime.SpecifyKind(new DateTime(2025, 7, 3, 22, 0, 0), DateTimeKind.Utc),
Location = "Oslo"
};
var winterGala = new Event
{
Id = Guid.NewGuid(),
Name = "Winter Gala 2025",
StartDate = DateTime.SpecifyKind(new DateTime(2025, 12, 15, 18, 0, 0), DateTimeKind.Utc),
EndDate = DateTime.SpecifyKind(new DateTime(2025, 12, 15, 23, 0, 0), DateTimeKind.Utc),
Location = "Bergen"
};
context.Events.AddRange(summerFestival, winterGala);
// Create Products for Summer Festival
var beer = new Product
{
Id = Guid.NewGuid(),
EventId = summerFestival.Id,
Name = "Beer",
Type = ProductType.Drink
};
var wine = new Product
{
Id = Guid.NewGuid(),
EventId = summerFestival.Id,
Name = "Wine",
Type = ProductType.Drink
};
var lunch = new Product
{
Id = Guid.NewGuid(),
EventId = summerFestival.Id,
Name = "Lunch",
Type = ProductType.Meal
};
var dinner = new Product
{
Id = Guid.NewGuid(),
EventId = summerFestival.Id,
Name = "Dinner",
Type = ProductType.Meal
};
var vipAccess = new Product
{
Id = Guid.NewGuid(),
EventId = summerFestival.Id,
Name = "VIP Lounge Access",
Type = ProductType.Access
};
context.Products.AddRange(beer, wine, lunch, dinner, vipAccess);
// Create Products for Winter Gala
var champagne = new Product
{
Id = Guid.NewGuid(),
EventId = winterGala.Id,
Name = "Champagne",
Type = ProductType.Drink
};
var galaDinner = new Product
{
Id = Guid.NewGuid(),
EventId = winterGala.Id,
Name = "Gala Dinner",
Type = ProductType.Meal
};
context.Products.AddRange(champagne, galaDinner);
// Create Groups for Summer Festival
var vipSponsors = new Group
{
Id = Guid.NewGuid(),
EventId = summerFestival.Id,
Name = "VIP Sponsors",
ContactPersonName = "Sarah Johnson",
ContactEmail = "sarah@sponsors.com"
};
var staff = new Group
{
Id = Guid.NewGuid(),
EventId = summerFestival.Id,
Name = "Event Staff",
ContactPersonName = "Mike Anderson",
ContactEmail = "mike@eventstaff.com"
};
var generalAdmission = new Group
{
Id = Guid.NewGuid(),
EventId = summerFestival.Id,
Name = "General Admission",
ContactPersonName = "Lisa Chen",
ContactEmail = "lisa@tickets.com"
};
context.Groups.AddRange(vipSponsors, staff, generalAdmission);
// Create People for VIP Sponsors
var johnDoe = new Person
{
Id = Guid.NewGuid(),
GroupId = vipSponsors.Id,
QrCode = Guid.NewGuid(),
Name = "John Doe",
Email = "john.doe@example.com",
PhoneNumber = "+47 123 45 678"
};
var janeSmith = new Person
{
Id = Guid.NewGuid(),
GroupId = vipSponsors.Id,
QrCode = Guid.NewGuid(),
Name = "Jane Smith",
Email = "jane.smith@example.com",
PhoneNumber = "+47 234 56 789"
};
var bobWilson = new Person
{
Id = Guid.NewGuid(),
GroupId = vipSponsors.Id,
QrCode = Guid.NewGuid(),
Name = "Bob Wilson",
Email = "bob.wilson@example.com",
PhoneNumber = "+47 345 67 890"
};
// Create People for Staff
var aliceStaff = new Person
{
Id = Guid.NewGuid(),
GroupId = staff.Id,
QrCode = Guid.NewGuid(),
Name = "Alice Staff",
Email = "alice@staff.com",
PhoneNumber = "+47 456 78 901"
};
var charlieStaff = new Person
{
Id = Guid.NewGuid(),
GroupId = staff.Id,
QrCode = Guid.NewGuid(),
Name = "Charlie Staff",
Email = "charlie@staff.com",
PhoneNumber = "+47 567 89 012"
};
// Create People for General Admission
var emilyGuest = new Person
{
Id = Guid.NewGuid(),
GroupId = generalAdmission.Id,
QrCode = Guid.NewGuid(),
Name = "Emily Guest",
Email = "emily@guest.com",
PhoneNumber = "+47 678 90 123"
};
var davidGuest = new Person
{
Id = Guid.NewGuid(),
GroupId = generalAdmission.Id,
QrCode = Guid.NewGuid(),
Name = "David Guest",
Email = "david@guest.com",
PhoneNumber = "+47 789 01 234"
};
context.People.AddRange(johnDoe, janeSmith, bobWilson, aliceStaff, charlieStaff, emilyGuest, davidGuest);
// Assign Quotas to VIP Sponsors
var johnQuotas = new[]
{
new PersonQuota { Id = Guid.NewGuid(), PersonId = johnDoe.Id, ProductId = beer.Id, InitialAmount = 10, UsedAmount = 2 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = johnDoe.Id, ProductId = wine.Id, InitialAmount = 5, UsedAmount = 1 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = johnDoe.Id, ProductId = lunch.Id, InitialAmount = 3, UsedAmount = 1 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = johnDoe.Id, ProductId = dinner.Id, InitialAmount = 3, UsedAmount = 0 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = johnDoe.Id, ProductId = vipAccess.Id, InitialAmount = 1, UsedAmount = 0 }
};
var janeQuotas = new[]
{
new PersonQuota { Id = Guid.NewGuid(), PersonId = janeSmith.Id, ProductId = beer.Id, InitialAmount = 8, UsedAmount = 0 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = janeSmith.Id, ProductId = wine.Id, InitialAmount = 6, UsedAmount = 0 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = janeSmith.Id, ProductId = lunch.Id, InitialAmount = 3, UsedAmount = 0 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = janeSmith.Id, ProductId = dinner.Id, InitialAmount = 3, UsedAmount = 0 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = janeSmith.Id, ProductId = vipAccess.Id, InitialAmount = 1, UsedAmount = 0 }
};
var bobQuotas = new[]
{
new PersonQuota { Id = Guid.NewGuid(), PersonId = bobWilson.Id, ProductId = beer.Id, InitialAmount = 12, UsedAmount = 3 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = bobWilson.Id, ProductId = wine.Id, InitialAmount = 4, UsedAmount = 2 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = bobWilson.Id, ProductId = lunch.Id, InitialAmount = 3, UsedAmount = 2 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = bobWilson.Id, ProductId = dinner.Id, InitialAmount = 3, UsedAmount = 1 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = bobWilson.Id, ProductId = vipAccess.Id, InitialAmount = 1, UsedAmount = 1 }
};
// Assign Quotas to Staff
var aliceQuotas = new[]
{
new PersonQuota { Id = Guid.NewGuid(), PersonId = aliceStaff.Id, ProductId = beer.Id, InitialAmount = 4, UsedAmount = 1 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = aliceStaff.Id, ProductId = lunch.Id, InitialAmount = 2, UsedAmount = 1 }
};
var charlieQuotas = new[]
{
new PersonQuota { Id = Guid.NewGuid(), PersonId = charlieStaff.Id, ProductId = beer.Id, InitialAmount = 4, UsedAmount = 0 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = charlieStaff.Id, ProductId = lunch.Id, InitialAmount = 2, UsedAmount = 0 }
};
// Assign Quotas to General Admission
var emilyQuotas = new[]
{
new PersonQuota { Id = Guid.NewGuid(), PersonId = emilyGuest.Id, ProductId = beer.Id, InitialAmount = 3, UsedAmount = 1 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = emilyGuest.Id, ProductId = lunch.Id, InitialAmount = 1, UsedAmount = 0 }
};
var davidQuotas = new[]
{
new PersonQuota { Id = Guid.NewGuid(), PersonId = davidGuest.Id, ProductId = beer.Id, InitialAmount = 3, UsedAmount = 0 },
new PersonQuota { Id = Guid.NewGuid(), PersonId = davidGuest.Id, ProductId = lunch.Id, InitialAmount = 1, UsedAmount = 0 }
};
context.PersonQuotas.AddRange(johnQuotas);
context.PersonQuotas.AddRange(janeQuotas);
context.PersonQuotas.AddRange(bobQuotas);
context.PersonQuotas.AddRange(aliceQuotas);
context.PersonQuotas.AddRange(charlieQuotas);
context.PersonQuotas.AddRange(emilyQuotas);
context.PersonQuotas.AddRange(davidQuotas);
// Create some sample transactions
var transactions = new[]
{
new Transaction
{
Id = Guid.NewGuid(),
PersonId = johnDoe.Id,
ProductId = beer.Id,
Amount = 2,
Timestamp = DateTime.UtcNow.AddHours(-5)
},
new Transaction
{
Id = Guid.NewGuid(),
PersonId = johnDoe.Id,
ProductId = wine.Id,
Amount = 1,
Timestamp = DateTime.UtcNow.AddHours(-3)
},
new Transaction
{
Id = Guid.NewGuid(),
PersonId = johnDoe.Id,
ProductId = lunch.Id,
Amount = 1,
Timestamp = DateTime.UtcNow.AddHours(-2)
},
new Transaction
{
Id = Guid.NewGuid(),
PersonId = bobWilson.Id,
ProductId = beer.Id,
Amount = 3,
Timestamp = DateTime.UtcNow.AddHours(-4)
},
new Transaction
{
Id = Guid.NewGuid(),
PersonId = bobWilson.Id,
ProductId = wine.Id,
Amount = 2,
Timestamp = DateTime.UtcNow.AddHours(-3)
},
new Transaction
{
Id = Guid.NewGuid(),
PersonId = bobWilson.Id,
ProductId = lunch.Id,
Amount = 2,
Timestamp = DateTime.UtcNow.AddHours(-2)
},
new Transaction
{
Id = Guid.NewGuid(),
PersonId = bobWilson.Id,
ProductId = dinner.Id,
Amount = 1,
Timestamp = DateTime.UtcNow.AddHours(-1)
},
new Transaction
{
Id = Guid.NewGuid(),
PersonId = bobWilson.Id,
ProductId = vipAccess.Id,
Amount = 1,
Timestamp = DateTime.UtcNow.AddHours(-6)
},
new Transaction
{
Id = Guid.NewGuid(),
PersonId = aliceStaff.Id,
ProductId = beer.Id,
Amount = 1,
Timestamp = DateTime.UtcNow.AddHours(-1)
},
new Transaction
{
Id = Guid.NewGuid(),
PersonId = aliceStaff.Id,
ProductId = lunch.Id,
Amount = 1,
Timestamp = DateTime.UtcNow.AddHours(-2)
},
new Transaction
{
Id = Guid.NewGuid(),
PersonId = emilyGuest.Id,
ProductId = beer.Id,
Amount = 1,
Timestamp = DateTime.UtcNow.AddMinutes(-30)
}
};
context.Transactions.AddRange(transactions);
await context.SaveChangesAsync();
}
}
+8
View File
@@ -22,6 +22,14 @@ builder.Services.AddScoped<ITransactionService, TransactionService>();
var app = builder.Build();
// Seed database with sample data
using (var scope = app.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<HospitalityDbContext>();
await Hospitality.Backend.Data.DbSeeder.SeedAsync(context);
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{