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

Feature/bolt integration #23

Merged
merged 9 commits into from
Nov 14, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use htmx cdn
Example user in DbInitializer not working/not getting seeded

Roles seeded in DbInitializer, not needed in appsettings

Change htmx to use cdn
RespectMathias committed Nov 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 0bdd8d2b0e65ee2ec3b7b14dcf640f1881eb9224
2 changes: 1 addition & 1 deletion GameLibrary/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

entity.Property(e => e.Comment).IsRequired().HasMaxLength(1000);
entity.Property(e => e.Rating).IsRequired();
entity.HasCheckConstraint("CK_Review_Rating", "Rating >= 1 AND Rating <= 5");
entity.ToTable(t => t.HasCheckConstraint("CK_Review_Rating", "Rating >= 1 AND Rating <= 5"));
});

// UserFavorite configurations
155 changes: 74 additions & 81 deletions GameLibrary/Data/DbInitializer.cs
Original file line number Diff line number Diff line change
@@ -16,102 +16,95 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace GameLibrary.Data
namespace GameLibrary.Data;

public static class DbInitializer
{
public static class DbInitializer
/// <summary> Seeding the database. </summary>
public static void Initialize(ApplicationDbContext context, UserManager<User> userManager, RoleManager<Role> roleManager)
{
public static void Initialize(ApplicationDbContext context, UserManager<User> userManager, RoleManager<Role> roleManager)
// Apply any pending migrations
if (context.Database.GetPendingMigrations().Any())
{
// Apply any pending migrations
if (context.Database.GetPendingMigrations().Any())
{
context.Database.Migrate();
}
context.Database.Migrate();
}

// Check if we already have games
if (context.Games.Any())
{
return; // DB has been seeded
}
// Check if we already have games
if (context.Games.Any())
{
return; // DB has been seeded
}

// Ensure roles are created
var roles = new[] { "Administrator", "User" };
foreach (var role in roles)
// Ensure roles are created
var roles = new[] { "Administrator", "User" };
foreach (var role in roles)
{
if (!roleManager.RoleExistsAsync(role).Result)
{
if (!roleManager.RoleExistsAsync(role).Result)
{
roleManager.CreateAsync(new Role { Name = role }).Wait();
}
roleManager.CreateAsync(new Role { Name = role }).Wait();
}
}

// Add test user with proper password hashing
var user = new User
{
UserName = "testuser",
Email = "[email protected]",
CreatedAt = DateTime.UtcNow
};
var user = new User
{
UserName = "[email protected]",
Email = "[email protected]"
};

if (userManager.FindByNameAsync(user.UserName).Result == null)
if (userManager.FindByNameAsync(user.UserName).Result == null)
{
var result = userManager.CreateAsync(user, "Password123!").Result;
if (result.Succeeded)
{
var result = userManager.CreateAsync(user, "password123").Result;
if (result.Succeeded)
{
userManager.AddToRoleAsync(user, "User").Wait();
}
userManager.AddToRoleAsync(user, "Administrator").Wait();
}
}

context.SaveChanges(); // Save to get the user ID
context.SaveChanges();

// Add test games
var games = new Game[]
{
new Game
{
Title = "The Legend of Zelda: Breath of the Wild",
Description = "Step into a world of discovery, exploration, and adventure in The Legend of Zelda: Breath of the Wild. Travel across vast fields, through forests, and to mountain peaks as you discover what has become of the kingdom of Hyrule in this stunning Open-Air Adventure.",
Genre = "Action-Adventure",
ReleaseDate = new DateTime(2017, 3, 3),
Developer = "Nintendo EPD",
Publisher = "Nintendo"
}
// Add more games as needed
};
// Add test games
var games = new Game[]
{
new() {
Title = "The Legend of Zelda: Breath of the Wild",
Description = "Step into a world of discovery, exploration, and adventure in The Legend of Zelda: Breath of the Wild. Travel across vast fields, through forests, and to mountain peaks as you discover what has become of the kingdom of Hyrule in this stunning Open-Air Adventure.",
Genre = "Action-Adventure",
ReleaseDate = new DateTime(2017, 3, 3, 0, 0, 0, DateTimeKind.Utc),
Developer = "Nintendo EPD",
Publisher = "Nintendo"
}
};

context.Games.AddRange(games);
context.SaveChanges();
context.Games.AddRange(games);
context.SaveChanges();

// Add test reviews with validation
var reviews = new Review[]
{
new Review
{
GameId = games[0].Id,
UserId = user.Id, // Use the inherited Id property from IdentityUser<Guid>
Rating = 5,
Comment = "One of the best games I've ever played! The open world is breathtaking and there's so much to discover.",
CreatedAt = DateTime.UtcNow.AddDays(-5)
},
new Review
{
GameId = games[0].Id,
UserId = user.Id, // Use the inherited Id property from IdentityUser<Guid>
Rating = 5,
Comment = "An absolute masterpiece. The attention to detail and storytelling are unmatched.",
CreatedAt = DateTime.UtcNow.AddDays(-3)
},
new Review
{
GameId = games[0].Id,
UserId = user.Id, // Use the inherited Id property from IdentityUser<Guid>
Rating = 4,
Comment = "A visually stunning game with a deep story, though it had some bugs at launch.",
CreatedAt = DateTime.UtcNow.AddDays(-1)
}
};
// Add test reviews with validation
var reviews = new Review[]
{
new() {
GameId = games[0].Id,
UserId = user.Id, // Use the inherited Id property from IdentityUser<Guid>
Rating = 5,
Comment = "One of the best games I've ever played! The open world is breathtaking and there's so much to discover.",
CreatedAt = DateTime.UtcNow.AddDays(-5)
},
new() {
GameId = games[0].Id,
UserId = user.Id, // Use the inherited Id property from IdentityUser<Guid>
Rating = 5,
Comment = "An absolute masterpiece. The attention to detail and storytelling are unmatched.",
CreatedAt = DateTime.UtcNow.AddDays(-3)
},
new() {
GameId = games[0].Id,
UserId = user.Id, // Use the inherited Id property from IdentityUser<Guid>
Rating = 4,
Comment = "A visually stunning game with a deep story, though it had some bugs at launch.",
CreatedAt = DateTime.UtcNow.AddDays(-1)
}
};

context.Reviews.AddRange(reviews);
context.SaveChanges();
}
context.Reviews.AddRange(reviews);
context.SaveChanges();
}
}
143 changes: 0 additions & 143 deletions GameLibrary/Data/Migrations/20241111_DropUsersAndCreateSchema.cs

This file was deleted.

Loading