Skip to content

Commit

Permalink
Merge pull request #3 from tungcbh/feat/multiImage
Browse files Browse the repository at this point in the history
Feat/multi image
  • Loading branch information
tungcbh authored Sep 4, 2024
2 parents 4b4937b + 59f8fa2 commit 0d1c8f9
Show file tree
Hide file tree
Showing 79 changed files with 4,438 additions and 611 deletions.
64 changes: 64 additions & 0 deletions Bulky.DataAccess/DBInitializer/DBInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Bulky.DataAccess.Data;
using Bulky.Models;
using Bulky.Utility;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace Bulky.DataAccess.DBInitializer
{
public class DBInitializer : IDBInitializer
{
private readonly UserManager<IdentityUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly ApplicationDbContext _db;

public DBInitializer(UserManager<IdentityUser> userManager, RoleManager<IdentityRole> roleManager, ApplicationDbContext db)
{
_userManager = userManager;
_roleManager = roleManager;
_db = db;
}

public void Initialize()
{
//Migrate
try
{
if (_db.Database.GetPendingMigrations().Count() > 0)
{
_db.Database.Migrate();
}
}
catch (Exception ex)
{

}

//Create roles
if (!_roleManager.RoleExistsAsync(SD.Role_Customer).GetAwaiter().GetResult())
{
_roleManager.CreateAsync(new IdentityRole(SD.Role_Customer)).GetAwaiter().GetResult();
_roleManager.CreateAsync(new IdentityRole(SD.Role_Company)).GetAwaiter().GetResult();
_roleManager.CreateAsync(new IdentityRole(SD.Role_Employee)).GetAwaiter().GetResult();
_roleManager.CreateAsync(new IdentityRole(SD.Role_Admin)).GetAwaiter().GetResult();

//Create admin user
_userManager.CreateAsync(new ApplicationUser
{
UserName = "admin",
Email = "[email protected]",
Name = "Admin",
PhoneNumber = "1234567890",
StreetAddress = "ABC",
State = "DEF",
PostalCode = "1234567890",
City = "NewYork",
}, "Abcd1234@@").GetAwaiter().GetResult();

ApplicationUser user = _db.ApplicationUsers.FirstOrDefault(u => u.UserName == "admin");
_userManager.AddToRoleAsync(user, SD.Role_Admin).GetAwaiter().GetResult();
}
return;
}
}
}
7 changes: 7 additions & 0 deletions Bulky.DataAccess/DBInitializer/IDBInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Bulky.DataAccess.DBInitializer
{
public interface IDBInitializer
{
void Initialize();
}
}
7 changes: 1 addition & 6 deletions Bulky.DataAccess/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : ba

public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<ProductImage> ProductImages { get; set; }
public DbSet<Company> Companies { get; set; }
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<ShoppingCart> ShoppingCarts { get; set; }
Expand Down Expand Up @@ -43,7 +44,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
Price50 = 85,
Price100 = 80,
CategoryId = 1,
ImageURL = ""
},
new Product
{
Expand All @@ -57,7 +57,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
Price50 = 25,
Price100 = 20,
CategoryId = 2,
ImageURL = ""
},
new Product
{
Expand All @@ -71,7 +70,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
Price50 = 40,
Price100 = 35,
CategoryId = 3,
ImageURL = ""
},
new Product
{
Expand All @@ -85,7 +83,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
Price50 = 60,
Price100 = 55,
CategoryId = 1,
ImageURL = ""
},
new Product
{
Expand All @@ -99,7 +96,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
Price50 = 25,
Price100 = 20,
CategoryId = 2,
ImageURL = ""
},
new Product
{
Expand All @@ -113,7 +109,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
Price50 = 22,
Price100 = 20,
CategoryId = 3,
ImageURL = ""
}
);

Expand Down
Loading

0 comments on commit 0d1c8f9

Please sign in to comment.