Skip to content

Commit

Permalink
Merge pull request #5 from duongminh318/featrure/aspnetcore_identity_…
Browse files Browse the repository at this point in the history
…database

create_identity_user_role
  • Loading branch information
duongminh318 authored Jul 5, 2024
2 parents 6fef8d7 + 51ac9c9 commit 59e6833
Show file tree
Hide file tree
Showing 18 changed files with 2,560 additions and 5 deletions.
20 changes: 20 additions & 0 deletions eShopSolution.Data/Configurations/AppRoleConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using eShopSolution.Data.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Text;

namespace eShopSolution.Data.Configurations
{
public class AppRoleConfiguration : IEntityTypeConfiguration<AppRole>
{
public void Configure(EntityTypeBuilder<AppRole> builder)
{
builder.ToTable("AppRoles");

builder.Property(x => x.Description).HasMaxLength(200).IsRequired();

}
}
}
22 changes: 22 additions & 0 deletions eShopSolution.Data/Configurations/AppUserConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using eShopSolution.Data.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Text;

namespace eShopSolution.Data.Configurations
{
public class AppUserConfiguration : IEntityTypeConfiguration<AppUser>
{
// cấu hình lại một số thuộc tình của identity
public void Configure(EntityTypeBuilder<AppUser> builder)
{
builder.ToTable("AppUsers");
builder.Property(x => x.FirstName).IsRequired().HasMaxLength(200);
builder.Property(x => x.LastName).IsRequired().HasMaxLength(200);
builder.Property(x => x.Dob).IsRequired();

}
}
}
2 changes: 1 addition & 1 deletion eShopSolution.Data/Configurations/CartConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void Configure(EntityTypeBuilder<Cart> builder)


builder.HasOne(x => x.Product).WithMany(x => x.Carts).HasForeignKey(x => x.ProductId);

builder.HasOne(x => x.AppUser).WithMany(x => x.Carts).HasForeignKey(x => x.UserId);
}
}
}
2 changes: 1 addition & 1 deletion eShopSolution.Data/Configurations/OrderConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Configure(EntityTypeBuilder<Order> builder)

builder.Property(x => x.ShipPhoneNumber).IsRequired().HasMaxLength(200);


builder.HasOne(x => x.AppUser).WithMany(x => x.Orders).HasForeignKey(x => x.UserId);
}
}
}
2 changes: 2 additions & 0 deletions eShopSolution.Data/Configurations/TransactionConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public void Configure(EntityTypeBuilder<Transaction> builder)
builder.HasKey(x => x.Id);

builder.Property(x => x.Id).UseIdentityColumn();

builder.HasOne(x => x.AppUser).WithMany(x => x.Transactions).HasForeignKey(x => x.UserId);
}
}
}
17 changes: 16 additions & 1 deletion eShopSolution.Data/EF/EShopDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using eShopSolution.Data.Configurations;
using eShopSolution.Data.Entities;
using eShopSolution.Data.Extensions;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace eShopSolution.Data.EF
{
public class EShopDbContext : DbContext
public class EShopDbContext : IdentityDbContext<AppUser, AppRole, Guid>
{
public EShopDbContext(DbContextOptions options) : base(options)
{
Expand All @@ -34,6 +36,19 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.ApplyConfiguration(new PromotionConfiguration());
modelBuilder.ApplyConfiguration(new TransactionConfiguration());


//identity configuration
modelBuilder.ApplyConfiguration(new AppUserConfiguration());
modelBuilder.ApplyConfiguration(new AppRoleConfiguration());

// configuration cho mấy thằng identity lẻ
modelBuilder.Entity<IdentityUserClaim<Guid>>().ToTable("AppUserClaims");
modelBuilder.Entity<IdentityUserRole<Guid>>().ToTable("AppUserRoles").HasKey(x => new { x.UserId, x.RoleId });
modelBuilder.Entity<IdentityUserLogin<Guid>>().ToTable("AppUserLogins").HasKey(x => x.UserId);

modelBuilder.Entity<IdentityRoleClaim<Guid>>().ToTable("AppRoleClaims");
modelBuilder.Entity<IdentityUserToken<Guid>>().ToTable("AppUserTokens").HasKey(x => x.UserId);

// Data Seeding
modelBuilder.Seed();

Expand Down
12 changes: 12 additions & 0 deletions eShopSolution.Data/Entities/AppRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Text;

namespace eShopSolution.Data.Entities
{
public class AppRole : IdentityRole<Guid>
{
public string Description { get; set; }
}
}
23 changes: 23 additions & 0 deletions eShopSolution.Data/Entities/AppUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Text;

namespace eShopSolution.Data.Entities
{
// tuỳ chỉnh lại những cái bảng identity tạo ra
public class AppUser : IdentityUser<Guid>
{
public string FirstName { get; set; }

public string LastName { get; set; }

public DateTime Dob { get; set; }

public List<Cart> Carts { get; set; }

public List<Order> Orders { get; set; }

public List<Transaction> Transactions { get; set; }
}
}
2 changes: 2 additions & 0 deletions eShopSolution.Data/Entities/Cart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public class Cart
public Product Product { get; set; }

public DateTime DateCreated { get; set; }

public AppUser AppUser { get; set; }
}
}
1 change: 1 addition & 0 deletions eShopSolution.Data/Entities/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public class Order
public OrderStatus Status { set; get; }

public List<OrderDetail> OrderDetails { get; set; }
public AppUser AppUser { get; set; }
}
}
4 changes: 4 additions & 0 deletions eShopSolution.Data/Entities/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ public class Transaction
public TransactionStatus Status { set; get; }
public string Provider { set; get; }

public Guid UserId { get; set; }

public AppUser AppUser { get; set; }

}
}
35 changes: 35 additions & 0 deletions eShopSolution.Data/Extensions/ModelBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using eShopSolution.Data.Entities;
using eShopSolution.Data.Enums;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -83,6 +84,40 @@ public static void Seed(this ModelBuilder modelBuilder)
modelBuilder.Entity<ProductInCategory>().HasData(
new ProductInCategory() { ProductId = 1, CategoryId = 1 }
);


// any guid
var roleId = new Guid("8D04DCE2-969A-435D-BBA4-DF3F325983DC");
var adminId = new Guid("69BD714F-9576-45BA-B5B7-F00649BE00DE");
modelBuilder.Entity<AppRole>().HasData(new AppRole
{
Id = roleId,
Name = "admin",
NormalizedName = "admin",
Description = "Administrator role"
});

var hasher = new PasswordHasher<AppUser>();
modelBuilder.Entity<AppUser>().HasData(new AppUser
{
Id = adminId,
UserName = "admin",
NormalizedUserName = "admin",
Email = "[email protected]",
NormalizedEmail = "[email protected]",
EmailConfirmed = true,
PasswordHash = hasher.HashPassword(null, "Abcd1234$"),
SecurityStamp = string.Empty,
FirstName = "Toan",
LastName = "Bach",
Dob = new DateTime(2020, 01, 31)
});

modelBuilder.Entity<IdentityUserRole<Guid>>().HasData(new IdentityUserRole<Guid>
{
RoleId = roleId,
UserId = adminId
});
}


Expand Down
Loading

0 comments on commit 59e6833

Please sign in to comment.