diff --git a/Bulky.DataAccess/Data/ApplicationDbContext.cs b/Bulky.DataAccess/Data/ApplicationDbContext.cs index 3f40a6f..8eae94d 100644 --- a/Bulky.DataAccess/Data/ApplicationDbContext.cs +++ b/Bulky.DataAccess/Data/ApplicationDbContext.cs @@ -20,6 +20,7 @@ public ApplicationDbContext(DbContextOptions options) : ba public DbSet shoppingCarts { get; set; } public DbSet orderHeaders { get; set; } public DbSet orderDetails { get; set; } + public DbSet productImages { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -76,8 +77,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) Price = 90, Price50 = 85, Price100 = 80, - CategoryId = 1, - ImageURL = "" + CategoryId = 1 }, new Product { @@ -90,8 +90,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) Price = 30, Price50 = 25, Price100 = 20, - CategoryId = 1, - ImageURL = "" + CategoryId = 1 }, new Product { @@ -104,8 +103,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) Price = 50, Price50 = 40, Price100 = 35, - CategoryId = 1, - ImageURL = "" + CategoryId = 1 }, new Product { @@ -118,8 +116,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) Price = 65, Price50 = 60, Price100 = 55, - CategoryId = 2, - ImageURL = "" + CategoryId = 2 }, new Product { @@ -132,8 +129,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) Price = 27, Price50 = 25, Price100 = 20, - CategoryId = 2, - ImageURL = "" + CategoryId = 2 }, new Product { @@ -146,8 +142,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) Price = 23, Price50 = 22, Price100 = 20, - CategoryId = 3, - ImageURL = "" + CategoryId = 3 } ); } diff --git a/Bulky.DataAccess/Migrations/20230901090757_removeImageurlFormProductTableAndAddProductImageTableInDb.Designer.cs b/Bulky.DataAccess/Migrations/20230901090757_removeImageurlFormProductTableAndAddProductImageTableInDb.Designer.cs new file mode 100644 index 0000000..c7e26e8 --- /dev/null +++ b/Bulky.DataAccess/Migrations/20230901090757_removeImageurlFormProductTableAndAddProductImageTableInDb.Designer.cs @@ -0,0 +1,789 @@ +// +using System; +using BulkyWeb.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BulkyWeb.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20230901090757_removeImageurlFormProductTableAndAddProductImageTableInDb")] + partial class removeImageurlFormProductTableAndAddProductImageTableInDb + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Bulky.Model.Models.Company", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("City") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PhoneNumber") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PostalCode") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("State") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("StreetAddress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("companies"); + + b.HasData( + new + { + Id = 1, + City = "Tech City", + Name = "Tech Solution", + PhoneNumber = "6669990000", + PostalCode = "12121", + State = "IL", + StreetAddress = "123 Tech St" + }, + new + { + Id = 2, + City = "Vid City", + Name = "Vivid Books", + PhoneNumber = "7779990000", + PostalCode = "66666", + State = "IL", + StreetAddress = "999 Vid St" + }, + new + { + Id = 3, + City = "Lala land", + Name = "Readers Club", + PhoneNumber = "1113335555", + PostalCode = "99999", + State = "NY", + StreetAddress = "999 Main St" + }); + }); + + modelBuilder.Entity("Bulky.Model.Models.OrderDetail", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("OrderHeaderId") + .HasColumnType("int"); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("OrderHeaderId"); + + b.HasIndex("ProductId"); + + b.ToTable("orderDetails"); + }); + + modelBuilder.Entity("Bulky.Model.Models.OrderHeader", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ApplicationUserId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("Carrier") + .HasColumnType("nvarchar(max)"); + + b.Property("City") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OrderDate") + .HasColumnType("datetime2"); + + b.Property("OrderStatus") + .HasColumnType("nvarchar(max)"); + + b.Property("OrderTotal") + .HasColumnType("float"); + + b.Property("PaymentDate") + .HasColumnType("datetime2"); + + b.Property("PaymentDueDate") + .HasColumnType("Date"); + + b.Property("PaymentIntentId") + .HasColumnType("nvarchar(max)"); + + b.Property("PaymentStatus") + .HasColumnType("nvarchar(max)"); + + b.Property("PhoneNumber") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PostalCode") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SessionId") + .HasColumnType("nvarchar(max)"); + + b.Property("ShippingDate") + .HasColumnType("datetime2"); + + b.Property("State") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("StreetAddress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("TrakingNumber") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("ApplicationUserId"); + + b.ToTable("orderHeaders"); + }); + + modelBuilder.Entity("Bulky.Model.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Author") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("CategoryId") + .HasColumnType("int"); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ISBN") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ListPrice") + .HasColumnType("float"); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("Price100") + .HasColumnType("float"); + + b.Property("Price50") + .HasColumnType("float"); + + b.Property("Title") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("CategoryId"); + + b.ToTable("Products"); + + b.HasData( + new + { + Id = 1, + Author = "Billy Spark", + CategoryId = 1, + Description = "Praesent vitae sodales libero. Praesent molestie orci augue, vitae euismod velit sollicitudin ac. Praesent vestibulum facilisis nibh ut ultricies.\r\n\r\nNunc malesuada viverra ipsum sit amet tincidunt. ", + ISBN = "SWD9999001", + ListPrice = 99.0, + Price = 90.0, + Price100 = 80.0, + Price50 = 85.0, + Title = "Fortune of Time" + }, + new + { + Id = 2, + Author = "Nancy Hoover", + CategoryId = 1, + Description = "Praesent vitae sodales libero. Praesent molestie orci augue, vitae euismod velit sollicitudin ac. Praesent vestibulum facilisis nibh ut ultricies.\r\n\r\nNunc malesuada viverra ipsum sit amet tincidunt. ", + ISBN = "CAW777777701", + ListPrice = 40.0, + Price = 30.0, + Price100 = 20.0, + Price50 = 25.0, + Title = "Dark Skies" + }, + new + { + Id = 3, + Author = "Julian Button", + CategoryId = 1, + Description = "Praesent vitae sodales libero. Praesent molestie orci augue, vitae euismod velit sollicitudin ac. Praesent vestibulum facilisis nibh ut ultricies.\r\n\r\nNunc malesuada viverra ipsum sit amet tincidunt. ", + ISBN = "RITO5555501", + ListPrice = 55.0, + Price = 50.0, + Price100 = 35.0, + Price50 = 40.0, + Title = "Vanish in the Sunset" + }, + new + { + Id = 4, + Author = "Abby Muscles", + CategoryId = 2, + Description = "Praesent vitae sodales libero. Praesent molestie orci augue, vitae euismod velit sollicitudin ac. Praesent vestibulum facilisis nibh ut ultricies.\r\n\r\nNunc malesuada viverra ipsum sit amet tincidunt. ", + ISBN = "WS3333333301", + ListPrice = 70.0, + Price = 65.0, + Price100 = 55.0, + Price50 = 60.0, + Title = "Cotton Candy" + }, + new + { + Id = 5, + Author = "Ron Parker", + CategoryId = 2, + Description = "Praesent vitae sodales libero. Praesent molestie orci augue, vitae euismod velit sollicitudin ac. Praesent vestibulum facilisis nibh ut ultricies.\r\n\r\nNunc malesuada viverra ipsum sit amet tincidunt. ", + ISBN = "SOTJ1111111101", + ListPrice = 30.0, + Price = 27.0, + Price100 = 20.0, + Price50 = 25.0, + Title = "Rock in the Ocean" + }, + new + { + Id = 6, + Author = "Laura Phantom", + CategoryId = 3, + Description = "Praesent vitae sodales libero. Praesent molestie orci augue, vitae euismod velit sollicitudin ac. Praesent vestibulum facilisis nibh ut ultricies.\r\n\r\nNunc malesuada viverra ipsum sit amet tincidunt. ", + ISBN = "FOT000000001", + ListPrice = 25.0, + Price = 23.0, + Price100 = 20.0, + Price50 = 22.0, + Title = "Leaves and Wonders" + }); + }); + + modelBuilder.Entity("Bulky.Model.Models.ProductImage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ImageURL") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.ToTable("productImages"); + }); + + modelBuilder.Entity("Bulky.Model.Models.ShoppingCart", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ApplicationUserId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ApplicationUserId"); + + b.HasIndex("ProductId"); + + b.ToTable("shoppingCarts"); + }); + + modelBuilder.Entity("BulkyWeb.Models.Category", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DisplayOrder") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + + b.HasData( + new + { + Id = 1, + DisplayOrder = 1, + Name = "Action" + }, + new + { + Id = 2, + DisplayOrder = 2, + Name = "SciFi" + }, + new + { + Id = 3, + DisplayOrder = 3, + Name = "History" + }); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b => + { + b.Property("Id") + .HasColumnType("nvarchar(450)"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex") + .HasFilter("[NormalizedName] IS NOT NULL"); + + b.ToTable("AspNetRoles", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClaimType") + .HasColumnType("nvarchar(max)"); + + b.Property("ClaimValue") + .HasColumnType("nvarchar(max)"); + + b.Property("RoleId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b => + { + b.Property("Id") + .HasColumnType("nvarchar(450)"); + + b.Property("AccessFailedCount") + .HasColumnType("int"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("nvarchar(max)"); + + b.Property("Discriminator") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("bit"); + + b.Property("LockoutEnabled") + .HasColumnType("bit"); + + b.Property("LockoutEnd") + .HasColumnType("datetimeoffset"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property("PasswordHash") + .HasColumnType("nvarchar(max)"); + + b.Property("PhoneNumber") + .HasColumnType("nvarchar(max)"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("bit"); + + b.Property("SecurityStamp") + .HasColumnType("nvarchar(max)"); + + b.Property("TwoFactorEnabled") + .HasColumnType("bit"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex") + .HasFilter("[NormalizedUserName] IS NOT NULL"); + + b.ToTable("AspNetUsers", (string)null); + + b.HasDiscriminator("Discriminator").HasValue("IdentityUser"); + + b.UseTphMappingStrategy(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClaimType") + .HasColumnType("nvarchar(max)"); + + b.Property("ClaimValue") + .HasColumnType("nvarchar(max)"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("nvarchar(450)"); + + b.Property("ProviderKey") + .HasColumnType("nvarchar(450)"); + + b.Property("ProviderDisplayName") + .HasColumnType("nvarchar(max)"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("nvarchar(450)"); + + b.Property("RoleId") + .HasColumnType("nvarchar(450)"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("nvarchar(450)"); + + b.Property("LoginProvider") + .HasColumnType("nvarchar(450)"); + + b.Property("Name") + .HasColumnType("nvarchar(450)"); + + b.Property("Value") + .HasColumnType("nvarchar(max)"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens", (string)null); + }); + + modelBuilder.Entity("Bulky.Model.Models.ApplicationUser", b => + { + b.HasBaseType("Microsoft.AspNetCore.Identity.IdentityUser"); + + b.Property("City") + .HasColumnType("nvarchar(max)"); + + b.Property("CompanyId") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PostalCode") + .HasColumnType("nvarchar(max)"); + + b.Property("State") + .HasColumnType("nvarchar(max)"); + + b.Property("StreetAddress") + .HasColumnType("nvarchar(max)"); + + b.HasIndex("CompanyId"); + + b.HasDiscriminator().HasValue("ApplicationUser"); + }); + + modelBuilder.Entity("Bulky.Model.Models.OrderDetail", b => + { + b.HasOne("Bulky.Model.Models.OrderHeader", "OrderHeader") + .WithMany() + .HasForeignKey("OrderHeaderId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Bulky.Model.Models.Product", "Product") + .WithMany() + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("OrderHeader"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("Bulky.Model.Models.OrderHeader", b => + { + b.HasOne("Bulky.Model.Models.ApplicationUser", "ApplicationUser") + .WithMany() + .HasForeignKey("ApplicationUserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("ApplicationUser"); + }); + + modelBuilder.Entity("Bulky.Model.Models.Product", b => + { + b.HasOne("BulkyWeb.Models.Category", "Category") + .WithMany() + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Category"); + }); + + modelBuilder.Entity("Bulky.Model.Models.ProductImage", b => + { + b.HasOne("Bulky.Model.Models.Product", "Product") + .WithMany("ProductImages") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("Bulky.Model.Models.ShoppingCart", b => + { + b.HasOne("Bulky.Model.Models.ApplicationUser", "ApplicationUser") + .WithMany() + .HasForeignKey("ApplicationUserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Bulky.Model.Models.Product", "Product") + .WithMany() + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("ApplicationUser"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Bulky.Model.Models.ApplicationUser", b => + { + b.HasOne("Bulky.Model.Models.Company", "Company") + .WithMany() + .HasForeignKey("CompanyId"); + + b.Navigation("Company"); + }); + + modelBuilder.Entity("Bulky.Model.Models.Product", b => + { + b.Navigation("ProductImages"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Bulky.DataAccess/Migrations/20230901090757_removeImageurlFormProductTableAndAddProductImageTableInDb.cs b/Bulky.DataAccess/Migrations/20230901090757_removeImageurlFormProductTableAndAddProductImageTableInDb.cs new file mode 100644 index 0000000..3708c03 --- /dev/null +++ b/Bulky.DataAccess/Migrations/20230901090757_removeImageurlFormProductTableAndAddProductImageTableInDb.cs @@ -0,0 +1,122 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BulkyWeb.Migrations +{ + /// + public partial class removeImageurlFormProductTableAndAddProductImageTableInDb : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_AspNetUsers_companies_CompanyId", + table: "AspNetUsers"); + + migrationBuilder.DropColumn( + name: "ImageURL", + table: "Products"); + + migrationBuilder.CreateTable( + name: "productImages", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ImageURL = table.Column(type: "nvarchar(max)", nullable: false), + ProductId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_productImages", x => x.Id); + table.ForeignKey( + name: "FK_productImages_Products_ProductId", + column: x => x.ProductId, + principalTable: "Products", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_productImages_ProductId", + table: "productImages", + column: "ProductId"); + + migrationBuilder.AddForeignKey( + name: "FK_AspNetUsers_companies_CompanyId", + table: "AspNetUsers", + column: "CompanyId", + principalTable: "companies", + principalColumn: "Id"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_AspNetUsers_companies_CompanyId", + table: "AspNetUsers"); + + migrationBuilder.DropTable( + name: "productImages"); + + migrationBuilder.AddColumn( + name: "ImageURL", + table: "Products", + type: "nvarchar(max)", + nullable: false, + defaultValue: ""); + + migrationBuilder.UpdateData( + table: "Products", + keyColumn: "Id", + keyValue: 1, + column: "ImageURL", + value: ""); + + migrationBuilder.UpdateData( + table: "Products", + keyColumn: "Id", + keyValue: 2, + column: "ImageURL", + value: ""); + + migrationBuilder.UpdateData( + table: "Products", + keyColumn: "Id", + keyValue: 3, + column: "ImageURL", + value: ""); + + migrationBuilder.UpdateData( + table: "Products", + keyColumn: "Id", + keyValue: 4, + column: "ImageURL", + value: ""); + + migrationBuilder.UpdateData( + table: "Products", + keyColumn: "Id", + keyValue: 5, + column: "ImageURL", + value: ""); + + migrationBuilder.UpdateData( + table: "Products", + keyColumn: "Id", + keyValue: 6, + column: "ImageURL", + value: ""); + + migrationBuilder.AddForeignKey( + name: "FK_AspNetUsers_companies_CompanyId", + table: "AspNetUsers", + column: "CompanyId", + principalTable: "companies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/Bulky.DataAccess/Migrations/ApplicationDbContextModelSnapshot.cs b/Bulky.DataAccess/Migrations/ApplicationDbContextModelSnapshot.cs index 4a14b46..f14d857 100644 --- a/Bulky.DataAccess/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/Bulky.DataAccess/Migrations/ApplicationDbContextModelSnapshot.cs @@ -219,10 +219,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) .IsRequired() .HasColumnType("nvarchar(max)"); - b.Property("ImageURL") - .IsRequired() - .HasColumnType("nvarchar(max)"); - b.Property("ListPrice") .HasColumnType("float"); @@ -253,7 +249,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) CategoryId = 1, Description = "Praesent vitae sodales libero. Praesent molestie orci augue, vitae euismod velit sollicitudin ac. Praesent vestibulum facilisis nibh ut ultricies.\r\n\r\nNunc malesuada viverra ipsum sit amet tincidunt. ", ISBN = "SWD9999001", - ImageURL = "", ListPrice = 99.0, Price = 90.0, Price100 = 80.0, @@ -267,7 +262,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) CategoryId = 1, Description = "Praesent vitae sodales libero. Praesent molestie orci augue, vitae euismod velit sollicitudin ac. Praesent vestibulum facilisis nibh ut ultricies.\r\n\r\nNunc malesuada viverra ipsum sit amet tincidunt. ", ISBN = "CAW777777701", - ImageURL = "", ListPrice = 40.0, Price = 30.0, Price100 = 20.0, @@ -281,7 +275,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) CategoryId = 1, Description = "Praesent vitae sodales libero. Praesent molestie orci augue, vitae euismod velit sollicitudin ac. Praesent vestibulum facilisis nibh ut ultricies.\r\n\r\nNunc malesuada viverra ipsum sit amet tincidunt. ", ISBN = "RITO5555501", - ImageURL = "", ListPrice = 55.0, Price = 50.0, Price100 = 35.0, @@ -295,7 +288,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) CategoryId = 2, Description = "Praesent vitae sodales libero. Praesent molestie orci augue, vitae euismod velit sollicitudin ac. Praesent vestibulum facilisis nibh ut ultricies.\r\n\r\nNunc malesuada viverra ipsum sit amet tincidunt. ", ISBN = "WS3333333301", - ImageURL = "", ListPrice = 70.0, Price = 65.0, Price100 = 55.0, @@ -309,7 +301,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) CategoryId = 2, Description = "Praesent vitae sodales libero. Praesent molestie orci augue, vitae euismod velit sollicitudin ac. Praesent vestibulum facilisis nibh ut ultricies.\r\n\r\nNunc malesuada viverra ipsum sit amet tincidunt. ", ISBN = "SOTJ1111111101", - ImageURL = "", ListPrice = 30.0, Price = 27.0, Price100 = 20.0, @@ -323,7 +314,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) CategoryId = 3, Description = "Praesent vitae sodales libero. Praesent molestie orci augue, vitae euismod velit sollicitudin ac. Praesent vestibulum facilisis nibh ut ultricies.\r\n\r\nNunc malesuada viverra ipsum sit amet tincidunt. ", ISBN = "FOT000000001", - ImageURL = "", ListPrice = 25.0, Price = 23.0, Price100 = 20.0, @@ -332,6 +322,28 @@ protected override void BuildModel(ModelBuilder modelBuilder) }); }); + modelBuilder.Entity("Bulky.Model.Models.ProductImage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ImageURL") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.ToTable("productImages"); + }); + modelBuilder.Entity("Bulky.Model.Models.ShoppingCart", b => { b.Property("Id") @@ -613,7 +625,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnType("nvarchar(max)"); b.Property("CompanyId") - .IsRequired() .HasColumnType("int"); b.Property("Name") @@ -675,6 +686,17 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("Category"); }); + modelBuilder.Entity("Bulky.Model.Models.ProductImage", b => + { + b.HasOne("Bulky.Model.Models.Product", "Product") + .WithMany("ProductImages") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Product"); + }); + modelBuilder.Entity("Bulky.Model.Models.ShoppingCart", b => { b.HasOne("Bulky.Model.Models.ApplicationUser", "ApplicationUser") @@ -749,12 +771,15 @@ protected override void BuildModel(ModelBuilder modelBuilder) { b.HasOne("Bulky.Model.Models.Company", "Company") .WithMany() - .HasForeignKey("CompanyId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); + .HasForeignKey("CompanyId"); b.Navigation("Company"); }); + + modelBuilder.Entity("Bulky.Model.Models.Product", b => + { + b.Navigation("ProductImages"); + }); #pragma warning restore 612, 618 } } diff --git a/Bulky.DataAccess/Repository/ApplicationUserRepository.cs b/Bulky.DataAccess/Repository/ApplicationUserRepository.cs index e817c52..d3bd08b 100644 --- a/Bulky.DataAccess/Repository/ApplicationUserRepository.cs +++ b/Bulky.DataAccess/Repository/ApplicationUserRepository.cs @@ -12,8 +12,15 @@ namespace Bulky.DataAccess.Repository public class ApplicationUserRepository : Repository, IApplicationUserRepository { + private readonly ApplicationDbContext _db; public ApplicationUserRepository(ApplicationDbContext db) : base(db) { + _db = db; + } + + public void Update(ApplicationUser applicationUser) + { + _db.ApplicationUsers.Update(applicationUser); } } } diff --git a/Bulky.DataAccess/Repository/IRepositoy/IApplicationUserRepository.cs b/Bulky.DataAccess/Repository/IRepositoy/IApplicationUserRepository.cs index c8b4e23..1304259 100644 --- a/Bulky.DataAccess/Repository/IRepositoy/IApplicationUserRepository.cs +++ b/Bulky.DataAccess/Repository/IRepositoy/IApplicationUserRepository.cs @@ -9,5 +9,6 @@ namespace Bulky.DataAccess.Repository.IRepositoy { public interface IApplicationUserRepository : IRepository { + public void Update(ApplicationUser applicationUser); } } diff --git a/Bulky.DataAccess/Repository/IRepositoy/IProductImageRepository.cs b/Bulky.DataAccess/Repository/IRepositoy/IProductImageRepository.cs new file mode 100644 index 0000000..035b3f4 --- /dev/null +++ b/Bulky.DataAccess/Repository/IRepositoy/IProductImageRepository.cs @@ -0,0 +1,15 @@ +using Bulky.Model.Models; +using BulkyWeb.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulky.DataAccess.Repository.IRepositoy +{ + public interface IProductImageRepository : IRepository + { + void Update(ProductImage productImage); + } +} diff --git a/Bulky.DataAccess/Repository/IRepositoy/IUnitOfWork.cs b/Bulky.DataAccess/Repository/IRepositoy/IUnitOfWork.cs index 10c276a..1187f1d 100644 --- a/Bulky.DataAccess/Repository/IRepositoy/IUnitOfWork.cs +++ b/Bulky.DataAccess/Repository/IRepositoy/IUnitOfWork.cs @@ -18,6 +18,8 @@ public interface IUnitOfWork IOrderHeaderRepository OrderHeaderRepository { get; } IOrderDetailRepository OrderDetailRepository { get; } + + IProductImageRepository ProductImageRepository { get; } void Save(); } } diff --git a/Bulky.DataAccess/Repository/ProductImageRepository.cs b/Bulky.DataAccess/Repository/ProductImageRepository.cs new file mode 100644 index 0000000..a7ab22f --- /dev/null +++ b/Bulky.DataAccess/Repository/ProductImageRepository.cs @@ -0,0 +1,28 @@ +using Bulky.DataAccess.Repository.IRepositoy; +using Bulky.Model.Models; +using BulkyWeb.Data; +using BulkyWeb.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulky.DataAccess.Repository +{ + public class ProductImageRepository : Repository, IProductImageRepository + { + + private readonly ApplicationDbContext _db; + + public ProductImageRepository(ApplicationDbContext db) : base(db) + { + _db = db; + } + + public void Update(ProductImage productImage) + { + _db.productImages.Update(productImage); + } + } +} diff --git a/Bulky.DataAccess/Repository/ProductRepository.cs b/Bulky.DataAccess/Repository/ProductRepository.cs index 4fbd1ce..eac2307 100644 --- a/Bulky.DataAccess/Repository/ProductRepository.cs +++ b/Bulky.DataAccess/Repository/ProductRepository.cs @@ -32,10 +32,11 @@ public void Update(Product product) productFormDb.ISBN = product.ISBN; productFormDb.Author = product.Author; productFormDb.CategoryId = product.CategoryId; - if(product.ImageURL != null) + productFormDb.ProductImages = product.ProductImages; + /* if (product.ImageURL != null) { productFormDb.ImageURL = product.ImageURL; - } + }*/ } _db.Products.Update(productFormDb); } diff --git a/Bulky.DataAccess/Repository/Repository.cs b/Bulky.DataAccess/Repository/Repository.cs index df254dc..c8dd65d 100644 --- a/Bulky.DataAccess/Repository/Repository.cs +++ b/Bulky.DataAccess/Repository/Repository.cs @@ -26,27 +26,30 @@ public void Add(T item) dbSet.Add(item); } - public T Get(Expression> filter, string? includeProperties = null, bool track = false) + public T Get(Expression> filter, string? includeProperties = null, bool tracked = false) { IQueryable query; - if (track) + if (tracked) { query = dbSet; + } else { query = dbSet.AsNoTracking(); } - - if (!(string.IsNullOrEmpty(includeProperties))) + + query = query.Where(filter); + if (!string.IsNullOrEmpty(includeProperties)) { - foreach (var property in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) + foreach (var includeProp in includeProperties + .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { - query = query.Include(property); + query = query.Include(includeProp); } } - query = query.Where(filter); return query.FirstOrDefault(); + } public IEnumerable GetAll(Expression>? filter, string? includeProperties = null) @@ -60,7 +63,7 @@ public IEnumerable GetAll(Expression>? filter, string? includeP { foreach (var property in includeProperties.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries)) { - query = query.Include(property); + query = query.Include(property.Trim()); } } diff --git a/Bulky.DataAccess/Repository/UnitOfWork .cs b/Bulky.DataAccess/Repository/UnitOfWork .cs index 876ecf9..5372ea5 100644 --- a/Bulky.DataAccess/Repository/UnitOfWork .cs +++ b/Bulky.DataAccess/Repository/UnitOfWork .cs @@ -19,6 +19,8 @@ public class UnitOfWork : IUnitOfWork public IOrderDetailRepository OrderDetailRepository { get; private set; } public IOrderHeaderRepository OrderHeaderRepository { get; private set; } + public IProductImageRepository ProductImageRepository { get; private set; } + private readonly ApplicationDbContext _db; public UnitOfWork(ApplicationDbContext db) @@ -32,6 +34,7 @@ public UnitOfWork(ApplicationDbContext db) ApplicationUserRepository = new ApplicationUserRepository(db); OrderDetailRepository = new OrderDetailRepository(db); OrderHeaderRepository = new OrderHeaderRepository(db); + ProductImageRepository = new ProductImageRepository(db); } public void Save() diff --git a/Bulky.Model/Models/Product.cs b/Bulky.Model/Models/Product.cs index 78f3d95..d0ad691 100644 --- a/Bulky.Model/Models/Product.cs +++ b/Bulky.Model/Models/Product.cs @@ -50,7 +50,8 @@ public class Product public Category Category { get; set; } [ValidateNever] - [Display(Name = "Image URL")] - public string ImageURL { get; set; } + public List ProductImages { get; set; } + } + } diff --git a/Bulky.Model/Models/ProductImage.cs b/Bulky.Model/Models/ProductImage.cs new file mode 100644 index 0000000..23402e2 --- /dev/null +++ b/Bulky.Model/Models/ProductImage.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulky.Model.Models +{ + public class ProductImage + { + public int Id { get; set; } + [Required] + public string ImageURL { get; set; } + public int ProductId { get; set; } + [ForeignKey("ProductId")] + public Product Product { get; set; } + } +} diff --git a/Bulky.Model/ViewModels/ProductVM.cs b/Bulky.Model/ViewModels/ProductVM.cs index 987b676..ecf8dc7 100644 --- a/Bulky.Model/ViewModels/ProductVM.cs +++ b/Bulky.Model/ViewModels/ProductVM.cs @@ -12,8 +12,8 @@ namespace Bulky.Model.ViewModels { public class ProductVM { - public Product product { get; set; } + public Product Product { get; set; } [ValidateNever] - public IEnumerable categoryList { get; set; } + public IEnumerable CategoryList { get; set; } } } diff --git a/Bulky.Model/ViewModels/RoleManagementVM.cs b/Bulky.Model/ViewModels/RoleManagementVM.cs new file mode 100644 index 0000000..9a6281e --- /dev/null +++ b/Bulky.Model/ViewModels/RoleManagementVM.cs @@ -0,0 +1,17 @@ +using Bulky.Model.Models; +using Microsoft.AspNetCore.Mvc.Rendering; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulky.Model.ViewModels +{ + public class RoleManagementVM + { + public ApplicationUser ApplicationUser { get; set; } + public IEnumerable CompanyList { get; set; } + public IEnumerable RoleList { get; set; } + } +} diff --git a/BulkyWeb/Areas/Admin/Controllers/ProductController.cs b/BulkyWeb/Areas/Admin/Controllers/ProductController.cs index 4f4add1..7ee7299 100644 --- a/BulkyWeb/Areas/Admin/Controllers/ProductController.cs +++ b/BulkyWeb/Areas/Admin/Controllers/ProductController.cs @@ -38,20 +38,20 @@ public IActionResult Upsert(int? Id) if (Id == null || Id == 0) { - //inster + //instert ProductVM productVM = new() { - product = new Product(), - categoryList = CategoryList + Product = new Product(), + CategoryList = CategoryList }; return View(productVM); } else { - Product product = _unitOfWork.ProductRepository.Get(u => u.Id == Id); + Product product = _unitOfWork.ProductRepository.Get(u => u.Id == Id, includeProperties: "ProductImages"); ProductVM productVM = new() { - product = product, - categoryList = CategoryList + Product = product, + CategoryList = CategoryList }; return View(productVM); } @@ -59,54 +59,86 @@ public IActionResult Upsert(int? Id) } [HttpPost] - public IActionResult Upsert(ProductVM productVM, IFormFile? imageFile) + public IActionResult Upsert(ProductVM productVM, List imageFiles) { - if (ModelState.IsValid) { - string wwwRootPath = _webHostEnvironment.WebRootPath; - if (imageFile != null) + if (ModelState.IsValid) { - string fileName = Guid.NewGuid().ToString() + Path.GetExtension(imageFile.FileName); - string productpath = Path.Combine(wwwRootPath, @"images\product"); + string wwwRootPath = _webHostEnvironment.WebRootPath; - if (!(string.IsNullOrEmpty(productVM.product.ImageURL))) + if (productVM.Product.Id == 0 || productVM.Product.Id == null) { - var oldImagePath = Path.Combine(wwwRootPath, productVM.product.ImageURL.TrimStart('\\')); - if (System.IO.File.Exists(oldImagePath)) - { - System.IO.File.Delete(oldImagePath); - } - } + _unitOfWork.ProductRepository.Add(productVM.Product); - using (var fileStream = new FileStream(Path.Combine(productpath, fileName), FileMode.Create)) + } + else { - imageFile.CopyTo(fileStream); + _unitOfWork.ProductRepository.Update(productVM.Product); } - productVM.product.ImageURL = @"\images\product\" + fileName; + _unitOfWork.Save(); + + + if (imageFiles != null) + { + foreach (IFormFile imageFile in imageFiles) + { + string fileName = Guid.NewGuid().ToString() + Path.GetExtension(imageFile.FileName); + string productpath = @"images\products\product-" + productVM.Product.Id; + string finalPath = Path.Combine(wwwRootPath,productpath); + + if (!Directory.Exists(finalPath)) + { + Directory.CreateDirectory(finalPath); + } + + using (var fileStream = new FileStream(Path.Combine(finalPath, fileName), FileMode.Create)) + { + imageFile.CopyTo(fileStream); + } + + ProductImage productImage = new() + { + ImageURL = @"\" + productpath + @"\" + fileName, + ProductId = productVM.Product.Id, + }; + + if (productVM.Product.ProductImages == null) + productVM.Product.ProductImages = new List(); + + + productVM.Product.ProductImages.Add(productImage); + } + + _unitOfWork.ProductRepository.Update(productVM.Product); + _unitOfWork.Save(); - } - if (productVM.product.Id == 0 || productVM.product.Id == null) - { - _unitOfWork.ProductRepository.Add(productVM.product); - TempData["success"] = "PRODUCT CREATED SUCCESSFULLY"; - } - else - { - _unitOfWork.ProductRepository.Update(productVM.product); - TempData["success"] = "PRODUCT UPDATED SUCCESSFULLY"; } - _unitOfWork.Save(); - return RedirectToAction("Index"); + TempData["success"] = "PRODUCT CREATED/UPDATED SUCCESSFULLY"; + return RedirectToAction("Index"); } - else + return View(productVM); + } + + public IActionResult DeleteImage(int imageId) + { + ProductImage imageToBeDeleted = _unitOfWork.ProductImageRepository.Get(u => u.Id == imageId); + int productId = imageToBeDeleted.ProductId; + if(imageToBeDeleted != null) { - productVM.categoryList = _unitOfWork.CategoryRepository.GetAll().Select(u => new SelectListItem + if (!(string.IsNullOrEmpty(imageToBeDeleted.ImageURL))) { - Text = u.Name, - Value = u.Id.ToString() - }); - return View(productVM); - } + var oldImagePath = Path.Combine(_webHostEnvironment.WebRootPath, imageToBeDeleted.ImageURL.TrimStart('\\')); + if (System.IO.File.Exists(oldImagePath)) + { + System.IO.File.Delete(oldImagePath); + } + } + _unitOfWork.ProductImageRepository.Remove(imageToBeDeleted); + _unitOfWork.Save(); + TempData["success"] = "IMAGE DELETED SUCCESSFULLY"; + } + + return RedirectToAction(nameof(Upsert), new { id = productId }); } @@ -130,20 +162,26 @@ public IActionResult Delete(int? id) { return Json(new {success = false, message = "Delete Fail !"}); } - if (!(string.IsNullOrEmpty(productTobeDeleted.ImageURL))) + + string productpath = @"images\products\product-" + id; + string finalPath = Path.Combine(_webHostEnvironment.WebRootPath, productpath); + + if (Directory.Exists(finalPath)) { - var oldImagePath = Path.Combine(_webHostEnvironment.WebRootPath, productTobeDeleted.ImageURL.TrimStart('\\')); - if (System.IO.File.Exists(oldImagePath)) + string[] filepaths = Directory.GetFiles(finalPath); + foreach (string filepath in filepaths) { - System.IO.File.Delete(oldImagePath); + System.IO.File.Delete(filepath); } + Directory.Delete(finalPath); } - _unitOfWork.ProductRepository.Remove(productTobeDeleted); + + _unitOfWork.ProductRepository.Remove(productTobeDeleted); _unitOfWork.Save(); - TempData["success"] = "CATEGORY DELETED SUCCESSFULLY"; + TempData["success"] = "PRODUCT DELETED SUCCESSFULLY"; return Json(new { success = true, message = "Delete successfully" }); } - return Json(new { success = false, message = "Delete Fail !"}); + return Json(new { success = false, message = "Delete Fail !" }); } diff --git a/BulkyWeb/Areas/Admin/Controllers/UserController.cs b/BulkyWeb/Areas/Admin/Controllers/UserController.cs index d8a8ce7..30af18d 100644 --- a/BulkyWeb/Areas/Admin/Controllers/UserController.cs +++ b/BulkyWeb/Areas/Admin/Controllers/UserController.cs @@ -1,10 +1,14 @@ using Bulky.DataAccess.Repository.IRepositoy; using Bulky.Model.Models; +using Bulky.Model.ViewModels; using Bulky.Utility; using BulkyWeb.Data; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; +using Octokit; namespace BulkyWeb.Areas.Admin.Controllers { @@ -13,48 +17,75 @@ namespace BulkyWeb.Areas.Admin.Controllers public class UserController : Controller { - private readonly ApplicationDbContext _db; + private readonly IUnitOfWork _unitOfWork; + private readonly UserManager _userManager; + private readonly RoleManager _roleManager; - public UserController(ApplicationDbContext db) + public UserController(IUnitOfWork unitOfWork, UserManager userManager, RoleManager roleManager) { - _db = db; + _userManager = userManager; + _unitOfWork = unitOfWork; + _roleManager = roleManager; } public IActionResult Index() { return View(); } - //public IActionResult Upsert(int? Id) { - // if (Id != null || Id != 0) { - // Company company = _unitOfWork.CompanyRepository.Get(u => u.Id == Id); - // if (company != null) - // { - // return View(company); - // } - // } - // return View(new Company()); - //} - - //[HttpPost] - //public IActionResult Upsert(Company company) - //{ - // if (ModelState.IsValid) - // { - // if(company.Id == 0) - // { - // _unitOfWork.CompanyRepository.Add(company); - // TempData["success"] = "COMPANY CREATED SUCCESSFULLY"; - // } - // else - // { - // _unitOfWork.CompanyRepository.Update(company); - // TempData["success"] = "COMPANY UPDATED SUCCESSFULLY"; - // } - // _unitOfWork.Save(); - // return RedirectToAction("Index"); - // } - // return View(company); - //} + public IActionResult RoleManagement(string userId) + { + + RoleManagementVM roleManagementVM = new() + { + ApplicationUser = _unitOfWork.ApplicationUserRepository.Get(u => u.Id == userId, includeProperties:"Company"), + RoleList = _roleManager.Roles.Select(i => new SelectListItem{ + Text = i.Name, + Value = i.Name.ToString(), + }), + CompanyList = _unitOfWork.CompanyRepository.GetAll().Select(i => new SelectListItem + { + Text = i.Name, + Value = i.Id.ToString(), + }) + }; + + roleManagementVM.ApplicationUser.Role = _userManager.GetRolesAsync(_unitOfWork.ApplicationUserRepository.Get(u=>u.Id == userId)).GetAwaiter().GetResult().FirstOrDefault(); + + return View(roleManagementVM); + } + + + [HttpPost] + public IActionResult RoleManagement(RoleManagementVM roleManagementVM) + { + + string oldRole = _userManager.GetRolesAsync(_unitOfWork.ApplicationUserRepository.Get(u => u.Id == roleManagementVM.ApplicationUser.Id)).GetAwaiter().GetResult().FirstOrDefault(); + + + if (roleManagementVM.ApplicationUser.Role != oldRole) + { + //Role is updated + ApplicationUser applicationUser = _unitOfWork.ApplicationUserRepository.Get(u => u.Id == roleManagementVM.ApplicationUser.Id); + if(roleManagementVM.ApplicationUser.Role == SD.Role_Company) + { + applicationUser.CompanyId = roleManagementVM.ApplicationUser.CompanyId; + } + if(oldRole == SD.Role_Company){ + applicationUser.CompanyId = null; + } + _unitOfWork.ApplicationUserRepository.Update(applicationUser); + _unitOfWork.Save(); + + _userManager.RemoveFromRoleAsync(applicationUser, oldRole).GetAwaiter().GetResult(); + _userManager.AddToRoleAsync(applicationUser, roleManagementVM.ApplicationUser.Role).GetAwaiter().GetResult() ; + } + + TempData["Success"] = "Role Updated."; + + return RedirectToAction("Index"); + } + + #region API @@ -91,7 +122,7 @@ public IActionResult LockUnlock([FromBody] string id) { return Json(new { success = false, message = "Error while locking/unlocking" }); } - if(userObj.LockoutEnd != null || userObj.LockoutEnd > DateTime.Now) + if(userObj.LockoutEnd > DateTime.Now) { userObj.LockoutEnd = DateTime.Now; } diff --git a/BulkyWeb/Areas/Admin/Views/Product/Upsert.cshtml b/BulkyWeb/Areas/Admin/Views/Product/Upsert.cshtml index 4d8a11d..36cf631 100644 --- a/BulkyWeb/Areas/Admin/Views/Product/Upsert.cshtml +++ b/BulkyWeb/Areas/Admin/Views/Product/Upsert.cshtml @@ -2,82 +2,109 @@
-

@((@Model.product.Id == null || @Model.product.Id == 0) ? "CERATE" : "UPADTE") PRODUCT

+

@((@Model.Product.Id == null || @Model.Product.Id == 0) ? "CERATE" : "UPADTE") PRODUCT

-
-
- - -
-
-
- - - -
-
- - - -
-
- - - +
+ + + +
+
+
+ @*
*@ +
+ + + +
+
+ + +
+ +
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+
+ + +
+
+
+ @if (Model.Product.Id != 0) + { + + } + else + { + + } +
+ +
+ + +
-
- - - +
+ @if (Model.Product.ProductImages != null) + { + foreach (var image in Model.Product.ProductImages) + { + + } + }
-
-
-
-
- -
- - - -
-
- - - -
-
- - - -
-
- - - -
-
- - - - - -
-
- - -
-
- @if (@Model.product.Id == null || @Model.product.Id == 0) - { - - }else{ - - } - Back to List -
+ +
diff --git a/BulkyWeb/Areas/Admin/Views/User/RoleManagement.cshtml b/BulkyWeb/Areas/Admin/Views/User/RoleManagement.cshtml new file mode 100644 index 0000000..63e0927 --- /dev/null +++ b/BulkyWeb/Areas/Admin/Views/User/RoleManagement.cshtml @@ -0,0 +1,52 @@ +@model RoleManagementVM + +
+
+

Role Management

+
+
+
+ +
+ + +
+
+ + +
+ @{ + var companyVisible = "display:none"; + } + @if (Model.ApplicationUser.Role == SD.Role_Company) + { + companyVisible = "display:block"; + } +
+ + +
+
+ + Back to List +
+
+
+ +@section Scripts{ + @{ + + + } +} diff --git a/BulkyWeb/Areas/Customer/Controllers/CartController.cs b/BulkyWeb/Areas/Customer/Controllers/CartController.cs index 7c6b4c2..823a6de 100644 --- a/BulkyWeb/Areas/Customer/Controllers/CartController.cs +++ b/BulkyWeb/Areas/Customer/Controllers/CartController.cs @@ -36,8 +36,11 @@ public IActionResult Index() OrderHeader = new() }; + IEnumerable productImages = _unitOfWork.ProductImageRepository.GetAll(); + foreach(ShoppingCart cart in shoppingCartVM.ShoppingCartList) { + cart.Product.ProductImages = productImages.Where(x => x.Id == cart.Product.Id).ToList(); cart.Price = GetPriceBasedOnQuantity(cart); shoppingCartVM.OrderHeader.OrderTotal += (cart.Price * cart.Count); } diff --git a/BulkyWeb/Areas/Customer/Controllers/HomeController.cs b/BulkyWeb/Areas/Customer/Controllers/HomeController.cs index 74405fd..41fc76e 100644 --- a/BulkyWeb/Areas/Customer/Controllers/HomeController.cs +++ b/BulkyWeb/Areas/Customer/Controllers/HomeController.cs @@ -24,7 +24,7 @@ public HomeController(IUnitOfWork unitOfWOrk) public IActionResult Index() { - List productList = _unitOfWork.ProductRepository.GetAll(includeProperties: "Category").ToList(); + List productList = _unitOfWork.ProductRepository.GetAll(includeProperties: "Category, ProductImages").ToList(); return View(productList); } @@ -32,12 +32,12 @@ public IActionResult Details(int id) { ShoppingCart shoppingCart = new() { - Product = _unitOfWork.ProductRepository.Get(u => u.Id == id, includeProperties: "Category"), - ProductId = id, - Count = 1 + Product = _unitOfWork.ProductRepository.Get(u => u.Id == id, includeProperties: "Category,ProductImages"), + Count = 1, + ProductId = id }; - return View(shoppingCart); + } diff --git a/BulkyWeb/Areas/Customer/Views/Cart/Index.cshtml b/BulkyWeb/Areas/Customer/Views/Cart/Index.cshtml index 2138855..d3272a3 100644 --- a/BulkyWeb/Areas/Customer/Views/Cart/Index.cshtml +++ b/BulkyWeb/Areas/Customer/Views/Cart/Index.cshtml @@ -25,7 +25,16 @@ @foreach(ShoppingCart cart in Model.ShoppingCartList){
- + @if (cart.Product.ProductImages != null && cart.Product.ProductImages.Count() > 0) + { + + } + else + { + + + } +
@cart.Product.Title
diff --git a/BulkyWeb/Areas/Customer/Views/Home/Details.cshtml b/BulkyWeb/Areas/Customer/Views/Home/Details.cshtml index b26633d..a435e37 100644 --- a/BulkyWeb/Areas/Customer/Views/Home/Details.cshtml +++ b/BulkyWeb/Areas/Customer/Views/Home/Details.cshtml @@ -22,7 +22,41 @@
- + + + + @if (Model.Product.ProductImages != null && Model.Product.ProductImages.Count() > 0) + { + + + } + + +
diff --git a/BulkyWeb/Areas/Customer/Views/Home/Index.cshtml b/BulkyWeb/Areas/Customer/Views/Home/Index.cshtml index 73b28cd..21ffc8b 100644 --- a/BulkyWeb/Areas/Customer/Views/Home/Index.cshtml +++ b/BulkyWeb/Areas/Customer/Views/Home/Index.cshtml @@ -6,7 +6,15 @@
- + @if (product.ProductImages != null && product.ProductImages.Count() > 0) + { + + } + else{ + + + } +

@product.Title

diff --git a/BulkyWeb/BulkyWeb.csproj b/BulkyWeb/BulkyWeb.csproj index 7668a13..0320017 100644 --- a/BulkyWeb/BulkyWeb.csproj +++ b/BulkyWeb/BulkyWeb.csproj @@ -28,6 +28,7 @@ + diff --git a/BulkyWeb/wwwroot/images/product/19b7bfd0-431c-4c3f-87e8-1a3e676a50b9.jpg b/BulkyWeb/wwwroot/images/product/19b7bfd0-431c-4c3f-87e8-1a3e676a50b9.jpg new file mode 100644 index 0000000..da750fd Binary files /dev/null and b/BulkyWeb/wwwroot/images/product/19b7bfd0-431c-4c3f-87e8-1a3e676a50b9.jpg differ diff --git a/BulkyWeb/wwwroot/images/product/5caafdcb-d465-4c87-9127-366839ba6c84.jpg b/BulkyWeb/wwwroot/images/product/5caafdcb-d465-4c87-9127-366839ba6c84.jpg new file mode 100644 index 0000000..15e9c81 Binary files /dev/null and b/BulkyWeb/wwwroot/images/product/5caafdcb-d465-4c87-9127-366839ba6c84.jpg differ diff --git a/BulkyWeb/wwwroot/images/product/da8f855c-16b5-43e2-a449-aea1a1d107f9.jpg b/BulkyWeb/wwwroot/images/product/da8f855c-16b5-43e2-a449-aea1a1d107f9.jpg new file mode 100644 index 0000000..6223247 Binary files /dev/null and b/BulkyWeb/wwwroot/images/product/da8f855c-16b5-43e2-a449-aea1a1d107f9.jpg differ diff --git a/BulkyWeb/wwwroot/images/product/db5ef486-2e0d-4063-8dd3-ed75279b0cf6.png b/BulkyWeb/wwwroot/images/product/db5ef486-2e0d-4063-8dd3-ed75279b0cf6.png new file mode 100644 index 0000000..b5839a5 Binary files /dev/null and b/BulkyWeb/wwwroot/images/product/db5ef486-2e0d-4063-8dd3-ed75279b0cf6.png differ diff --git a/BulkyWeb/wwwroot/images/products/product-1/1e31a7a1-31a0-483b-8385-2cb339f2fe0f.jpg b/BulkyWeb/wwwroot/images/products/product-1/1e31a7a1-31a0-483b-8385-2cb339f2fe0f.jpg new file mode 100644 index 0000000..13498ba Binary files /dev/null and b/BulkyWeb/wwwroot/images/products/product-1/1e31a7a1-31a0-483b-8385-2cb339f2fe0f.jpg differ diff --git a/BulkyWeb/wwwroot/images/products/product-1/e3c9054b-d4d7-4aa8-992b-754d9d2b9b50.jpg b/BulkyWeb/wwwroot/images/products/product-1/e3c9054b-d4d7-4aa8-992b-754d9d2b9b50.jpg new file mode 100644 index 0000000..39cbe24 Binary files /dev/null and b/BulkyWeb/wwwroot/images/products/product-1/e3c9054b-d4d7-4aa8-992b-754d9d2b9b50.jpg differ diff --git a/BulkyWeb/wwwroot/images/products/product-2/39288f30-5ac7-44db-95e4-09c1ce8b4843.jpg b/BulkyWeb/wwwroot/images/products/product-2/39288f30-5ac7-44db-95e4-09c1ce8b4843.jpg new file mode 100644 index 0000000..15e9c81 Binary files /dev/null and b/BulkyWeb/wwwroot/images/products/product-2/39288f30-5ac7-44db-95e4-09c1ce8b4843.jpg differ diff --git a/BulkyWeb/wwwroot/images/products/product-2/ddc7487a-953d-450d-ac72-edbea4f256ba.jpg b/BulkyWeb/wwwroot/images/products/product-2/ddc7487a-953d-450d-ac72-edbea4f256ba.jpg new file mode 100644 index 0000000..6223247 Binary files /dev/null and b/BulkyWeb/wwwroot/images/products/product-2/ddc7487a-953d-450d-ac72-edbea4f256ba.jpg differ diff --git a/BulkyWeb/wwwroot/images/products/product-3/033fd302-3378-478c-b546-c0f2c7f83ecc.jpg b/BulkyWeb/wwwroot/images/products/product-3/033fd302-3378-478c-b546-c0f2c7f83ecc.jpg new file mode 100644 index 0000000..dec59a3 Binary files /dev/null and b/BulkyWeb/wwwroot/images/products/product-3/033fd302-3378-478c-b546-c0f2c7f83ecc.jpg differ diff --git a/BulkyWeb/wwwroot/images/products/product-3/7c2a8b65-c49e-4498-bfff-21712f187f86.jpg b/BulkyWeb/wwwroot/images/products/product-3/7c2a8b65-c49e-4498-bfff-21712f187f86.jpg new file mode 100644 index 0000000..e8aefde Binary files /dev/null and b/BulkyWeb/wwwroot/images/products/product-3/7c2a8b65-c49e-4498-bfff-21712f187f86.jpg differ diff --git a/BulkyWeb/wwwroot/images/products/product-4/0ca74451-bbdd-413e-8525-d3e81f62550d.jpg b/BulkyWeb/wwwroot/images/products/product-4/0ca74451-bbdd-413e-8525-d3e81f62550d.jpg new file mode 100644 index 0000000..da750fd Binary files /dev/null and b/BulkyWeb/wwwroot/images/products/product-4/0ca74451-bbdd-413e-8525-d3e81f62550d.jpg differ diff --git a/BulkyWeb/wwwroot/images/products/product-4/b5065b24-553a-4615-b0bd-6698bcbab334.jpg b/BulkyWeb/wwwroot/images/products/product-4/b5065b24-553a-4615-b0bd-6698bcbab334.jpg new file mode 100644 index 0000000..02820ad Binary files /dev/null and b/BulkyWeb/wwwroot/images/products/product-4/b5065b24-553a-4615-b0bd-6698bcbab334.jpg differ diff --git a/BulkyWeb/wwwroot/images/products/product-5/3f88f137-dabb-48e9-8f0e-722b981fa5fd.jpg b/BulkyWeb/wwwroot/images/products/product-5/3f88f137-dabb-48e9-8f0e-722b981fa5fd.jpg new file mode 100644 index 0000000..991fad3 Binary files /dev/null and b/BulkyWeb/wwwroot/images/products/product-5/3f88f137-dabb-48e9-8f0e-722b981fa5fd.jpg differ diff --git a/BulkyWeb/wwwroot/images/products/product-5/66596575-c20c-4cb9-a023-5baf5da05d42.jpg b/BulkyWeb/wwwroot/images/products/product-5/66596575-c20c-4cb9-a023-5baf5da05d42.jpg new file mode 100644 index 0000000..9a84ebf Binary files /dev/null and b/BulkyWeb/wwwroot/images/products/product-5/66596575-c20c-4cb9-a023-5baf5da05d42.jpg differ diff --git a/BulkyWeb/wwwroot/images/products/product-6/2afb4627-993b-4391-a923-486ec2cc81fd.jpg b/BulkyWeb/wwwroot/images/products/product-6/2afb4627-993b-4391-a923-486ec2cc81fd.jpg new file mode 100644 index 0000000..c247655 Binary files /dev/null and b/BulkyWeb/wwwroot/images/products/product-6/2afb4627-993b-4391-a923-486ec2cc81fd.jpg differ diff --git a/BulkyWeb/wwwroot/images/products/product-6/df76645a-59cc-4da5-ae99-385c60e9009c.jpg b/BulkyWeb/wwwroot/images/products/product-6/df76645a-59cc-4da5-ae99-385c60e9009c.jpg new file mode 100644 index 0000000..5f0e203 Binary files /dev/null and b/BulkyWeb/wwwroot/images/products/product-6/df76645a-59cc-4da5-ae99-385c60e9009c.jpg differ diff --git a/BulkyWeb/wwwroot/js/user.js b/BulkyWeb/wwwroot/js/user.js index d93ff42..80248fa 100644 --- a/BulkyWeb/wwwroot/js/user.js +++ b/BulkyWeb/wwwroot/js/user.js @@ -14,46 +14,46 @@ function LoadDataTable() { { data: 'company.name', "width": "15%" }, { data: 'role', "width": "10%" }, { - data: { id :'id', lockoutEnd : 'lockoutEnd'}, + data: { id: 'id', lockoutEnd: 'lockoutEnd' }, "render": function (data) { var today = new Date().getTime(); var lockout = new Date(data.lockoutEnd).getTime(); if (today < lockout) { return ` ` } else { return ` ` } }, "width": "25%" - + } ] }); } -function lockunlock(id){ +function lockunlock(id) { $.ajax({ url: "/Admin/User/LockUnlock", type: "POST", data: JSON.stringify(id), - contentType : "application/json", + contentType: "application/json", success: (data) => { if (data.success) { toastr.success(data.message);