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
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
48 changes: 48 additions & 0 deletions GameLibrary/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,52 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Game> Games => Set<Game>();
public DbSet<Review> Reviews => Set<Review>();
public DbSet<UserFavorite> UserFavorites => Set<UserFavorite>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Game configurations
modelBuilder.Entity<Game>(entity =>
{
entity.Property(e => e.Title).IsRequired().HasMaxLength(100);
entity.Property(e => e.Description).IsRequired().HasMaxLength(2000);
entity.Property(e => e.Genre).IsRequired().HasMaxLength(50);
entity.Property(e => e.Developer).IsRequired().HasMaxLength(100);
entity.Property(e => e.Publisher).IsRequired().HasMaxLength(100);
entity.Property(e => e.ImageUrl).HasMaxLength(500);
entity.Property(e => e.Rating).HasPrecision(3, 1);
});
// Review configurations
modelBuilder.Entity<Review>(entity =>
{
entity.HasOne(r => r.Game)
.WithMany(g => g.Reviews)
.HasForeignKey(r => r.GameId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasOne(r => r.User)
.WithMany(u => u.Reviews)
.HasForeignKey(r => r.UserId)
.OnDelete(DeleteBehavior.Cascade);
entity.Property(e => e.Comment).IsRequired().HasMaxLength(1000);
entity.Property(e => e.Rating).IsRequired();
entity.ToTable(t => t.HasCheckConstraint("CK_Review_Rating", "Rating >= 1 AND Rating <= 5"));
});
// UserFavorite configurations
modelBuilder.Entity<UserFavorite>(entity =>
{
entity.HasOne(uf => uf.User)
.WithMany(u => u.Favorites)
.HasForeignKey(uf => uf.UserId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasOne(uf => uf.Game)
.WithMany(g => g.UserFavorites)
.HasForeignKey(uf => uf.GameId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasIndex(e => new { e.UserId, e.GameId })
.IsUnique()
.HasDatabaseName("IX_UserFavorites_UserGame");
});
}
}
117 changes: 117 additions & 0 deletions GameLibrary/Data/DbInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2024 Web.Tech. Group17
//
// Licensed under the Apache License, Version 2.0 (the "License"):
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using GameLibrary.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace GameLibrary.Data;

public static class DbInitializer
{
/// <summary> Seeding the database. </summary>
public static void Initialize(this IApplicationBuilder app)
{
using IServiceScope scope = app.ApplicationServices.CreateScope();
var services = scope.ServiceProvider;
using ApplicationDbContext context = services.GetRequiredService<ApplicationDbContext>();
var userManager = services.GetRequiredService<UserManager<User>>();
var roleManager = services.GetRequiredService<RoleManager<Role>>();

// Apply any pending migrations
if (context.Database.GetPendingMigrations().Any())
{
context.Database.Migrate();
}

// 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)
{
if (!roleManager.RoleExistsAsync(role).Result)
{
roleManager.CreateAsync(new Role { Name = role }).Wait();
}
}

var user = new User
{
UserName = "[email protected]",
Email = "[email protected]",
EmailConfirmed = true
};

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

context.SaveChanges();

// 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();

// 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();
}
}
Binary file modified GameLibrary/Database.db
Binary file not shown.
38 changes: 18 additions & 20 deletions GameLibrary/GameLibrary.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
ο»Ώ<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3"/>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.10"/>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.10"/>
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.10"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.10"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
Loading