Skip to content
Open
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
4 changes: 4 additions & 0 deletions E-Commerce.Domain/E-Commerce.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.22" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions E-Commerce.Domain/IdentityModule/Address.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace E_Commerce.Domain.IdentityModule
{
public class Address
{
public int Id { get; set; } // PK
public string City { get; set; } = default!;
public string Street { get; set; } = default!;
public string Country { get; set; } = default!;
public string FirstName { get; set; } = default!;
public string LastName { get; set; } = default!;

public ApplicationUser User { get; set; } = default!;
public string UserId { get; set; } = default!;
}
}
17 changes: 17 additions & 0 deletions E-Commerce.Domain/IdentityModule/ApplicationUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace E_Commerce.Domain.IdentityModule
{
public class ApplicationUser : IdentityUser
{

public string DisplayName { get; set; } = default!;
public Address? Address { get; set; }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using E_Commerce.Domain.IdentityModule;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace E_Commerce.Persistance.IdentityData.DbContexts
{
public class StoreIdentityDbContext : IdentityDbContext<ApplicationUser>
{
public StoreIdentityDbContext(DbContextOptions<StoreIdentityDbContext> options) : base(options)
{

}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Address>().ToTable("Addresses");
builder.Entity<ApplicationUser>().ToTable("Users");
builder.Entity<IdentityRole>().ToTable("Roles");
builder.Entity<IdentityUserRole<string>>().ToTable("UserRoles");
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using E_Commerce.Domain.Contracts;
using E_Commerce.Domain.IdentityModule;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace E_Commerce.Persistance.IdentityData.IdentityData
{
public class IdentityDataInitializer : IDataInitializer
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly ILogger<IdentityDataInitializer> _logger;

public IdentityDataInitializer(UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager,
ILogger<IdentityDataInitializer> logger)
{
_userManager = userManager;
_roleManager = roleManager;
_logger = logger;
}
public async Task InitializeAsync()
{
try
{

if(!_roleManager.Roles.Any())
{
await _roleManager.CreateAsync(new IdentityRole("Admin"));
await _roleManager.CreateAsync(new IdentityRole("SuperAdmin"));
}

if(_userManager.Users.Any())
{
var User01 = new ApplicationUser()
{
DisplayName = "Mohamed Tarek" ,
UserName = "MohamedTarek" ,
Email = "[email protected]" ,
PhoneNumber = "0123456789"
};
var User02 = new ApplicationUser()
{
DisplayName = "Salma Tarek",
UserName = "SalmaTarek",
Email = "[email protected]",
PhoneNumber = "0123456559"
};

await _userManager.CreateAsync(User01, "P@ssw0rd");
await _userManager.CreateAsync(User02, "P@ssw0rd");

await _userManager.AddToRoleAsync(User01, "Admin");
await _userManager.AddToRoleAsync(User02, "SuperAdmin");
}
}
catch (Exception ex)
{
_logger.LogError($"Error During Seeding Identity DataBase : {ex}");
}
}
}
}
Loading