From 17342c602dfe984033669dc32f346938cca05f01 Mon Sep 17 00:00:00 2001 From: aliarmaganuygun Date: Tue, 15 Oct 2024 15:05:51 +0300 Subject: [PATCH] Company CRUD operations has been done --- Bulky.DataAccess/Data/ApplicationDbContext.cs | 6 + ...318_AddCompanyToDbAndSeedTable.Designer.cs | 551 ++++++++++++++++++ ...241015120318_AddCompanyToDbAndSeedTable.cs | 122 ++++ .../ApplicationDbContextModelSnapshot.cs | 64 +- .../Repository/CompanyRepository.cs | 21 + .../IRepository/ICompanyRepository.cs | 9 + .../Repository/IRepository/IUnitOfWork.cs | 1 + Bulky.DataAccess/Repository/Repository.cs | 7 +- Bulky.DataAccess/Repository/UnitOfWork.cs | 2 + Bulky.Models/Company.cs | 18 + .../Admin/Controllers/CompanyController.cs | 90 +++ .../Areas/Admin/Views/Company/Index.cshtml | 36 ++ .../Areas/Admin/Views/Company/Upsert.cshtml | 72 +++ BulkyWeb/Views/Shared/_Layout.cshtml | 4 + BulkyWeb/wwwroot/js/company.js | 54 ++ 15 files changed, 1045 insertions(+), 12 deletions(-) create mode 100644 Bulky.DataAccess/Migrations/20241015120318_AddCompanyToDbAndSeedTable.Designer.cs create mode 100644 Bulky.DataAccess/Migrations/20241015120318_AddCompanyToDbAndSeedTable.cs create mode 100644 Bulky.DataAccess/Repository/CompanyRepository.cs create mode 100644 Bulky.DataAccess/Repository/IRepository/ICompanyRepository.cs create mode 100644 Bulky.Models/Company.cs create mode 100644 BulkyWeb/Areas/Admin/Controllers/CompanyController.cs create mode 100644 BulkyWeb/Areas/Admin/Views/Company/Index.cshtml create mode 100644 BulkyWeb/Areas/Admin/Views/Company/Upsert.cshtml create mode 100644 BulkyWeb/wwwroot/js/company.js diff --git a/Bulky.DataAccess/Data/ApplicationDbContext.cs b/Bulky.DataAccess/Data/ApplicationDbContext.cs index 4c29ecc..78b3013 100644 --- a/Bulky.DataAccess/Data/ApplicationDbContext.cs +++ b/Bulky.DataAccess/Data/ApplicationDbContext.cs @@ -13,6 +13,7 @@ public ApplicationDbContext(DbContextOptions options) : ba // Add DbSet properties public DbSet Categories { get; set; } + public DbSet Companies { get; set; } public DbSet Products { get; set; } public DbSet ApplicationUsers { get; set; } @@ -20,6 +21,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); + modelBuilder.Entity().HasData( + new Company { Id = 1, Name = "Vivid Books", StreetAddress = "123 Main St", City = "New York", State = "NY", PostalCode = "10000" }, + new Company { Id = 2, Name = "Bookworm", StreetAddress = "456 Elm St", City = "New York", State = "NY", PostalCode = "10000" } + ); + modelBuilder.Entity().HasData( new Category { Id = 1, Name = "Action", DisplayOrder = 1 }, new Category { Id = 2, Name = "SciFi", DisplayOrder = 2 }, diff --git a/Bulky.DataAccess/Migrations/20241015120318_AddCompanyToDbAndSeedTable.Designer.cs b/Bulky.DataAccess/Migrations/20241015120318_AddCompanyToDbAndSeedTable.Designer.cs new file mode 100644 index 0000000..9896f27 --- /dev/null +++ b/Bulky.DataAccess/Migrations/20241015120318_AddCompanyToDbAndSeedTable.Designer.cs @@ -0,0 +1,551 @@ +// +using System; +using BookBazaar.DataAccess.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BookBazaar.DataAccess.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20241015120318_AddCompanyToDbAndSeedTable")] + partial class AddCompanyToDbAndSeedTable + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BookBazaar.Models.Category", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DisplayOrder") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(30) + .HasColumnType("nvarchar(30)"); + + 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("BookBazaar.Models.Company", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("City") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PhoneNumber") + .HasColumnType("nvarchar(max)"); + + b.Property("PostalCode") + .HasColumnType("nvarchar(max)"); + + b.Property("State") + .HasColumnType("nvarchar(max)"); + + b.Property("StreetAddress") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Companies"); + + b.HasData( + new + { + Id = 1, + City = "New York", + Name = "Vivid Books", + PostalCode = "10000", + State = "NY", + StreetAddress = "123 Main St" + }, + new + { + Id = 2, + City = "New York", + Name = "Bookworm", + PostalCode = "10000", + State = "NY", + StreetAddress = "456 Elm St" + }); + }); + + modelBuilder.Entity("BookBazaar.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("ImageUrl") + .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", + ImageUrl = "", + 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", + ImageUrl = "", + 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", + ImageUrl = "", + 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", + ImageUrl = "", + 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", + ImageUrl = "", + 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", + ImageUrl = "", + ListPrice = 25.0, + Price = 23.0, + Price100 = 20.0, + Price50 = 22.0, + Title = "Leaves and Wonders" + }); + }); + + 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() + .HasMaxLength(21) + .HasColumnType("nvarchar(21)"); + + 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().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("BookBazaar.Models.ApplicationUser", b => + { + b.HasBaseType("Microsoft.AspNetCore.Identity.IdentityUser"); + + b.Property("City") + .HasColumnType("nvarchar(max)"); + + 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.HasDiscriminator().HasValue("ApplicationUser"); + }); + + modelBuilder.Entity("BookBazaar.Models.Product", b => + { + b.HasOne("BookBazaar.Models.Category", "Category") + .WithMany() + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Category"); + }); + + 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(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Bulky.DataAccess/Migrations/20241015120318_AddCompanyToDbAndSeedTable.cs b/Bulky.DataAccess/Migrations/20241015120318_AddCompanyToDbAndSeedTable.cs new file mode 100644 index 0000000..89f5811 --- /dev/null +++ b/Bulky.DataAccess/Migrations/20241015120318_AddCompanyToDbAndSeedTable.cs @@ -0,0 +1,122 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional + +namespace BookBazaar.DataAccess.Migrations +{ + /// + public partial class AddCompanyToDbAndSeedTable : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "Name", + table: "AspNetUserTokens", + type: "nvarchar(450)", + nullable: false, + oldClrType: typeof(string), + oldType: "nvarchar(128)", + oldMaxLength: 128); + + migrationBuilder.AlterColumn( + name: "LoginProvider", + table: "AspNetUserTokens", + type: "nvarchar(450)", + nullable: false, + oldClrType: typeof(string), + oldType: "nvarchar(128)", + oldMaxLength: 128); + + migrationBuilder.AlterColumn( + name: "ProviderKey", + table: "AspNetUserLogins", + type: "nvarchar(450)", + nullable: false, + oldClrType: typeof(string), + oldType: "nvarchar(128)", + oldMaxLength: 128); + + migrationBuilder.AlterColumn( + name: "LoginProvider", + table: "AspNetUserLogins", + type: "nvarchar(450)", + nullable: false, + oldClrType: typeof(string), + oldType: "nvarchar(128)", + oldMaxLength: 128); + + migrationBuilder.CreateTable( + name: "Companies", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + StreetAddress = table.Column(type: "nvarchar(max)", nullable: true), + City = table.Column(type: "nvarchar(max)", nullable: true), + State = table.Column(type: "nvarchar(max)", nullable: true), + PostalCode = table.Column(type: "nvarchar(max)", nullable: true), + PhoneNumber = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Companies", x => x.Id); + }); + + migrationBuilder.InsertData( + table: "Companies", + columns: new[] { "Id", "City", "Name", "PhoneNumber", "PostalCode", "State", "StreetAddress" }, + values: new object[,] + { + { 1, "New York", "Vivid Books", null, "10000", "NY", "123 Main St" }, + { 2, "New York", "Bookworm", null, "10000", "NY", "456 Elm St" } + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Companies"); + + migrationBuilder.AlterColumn( + name: "Name", + table: "AspNetUserTokens", + type: "nvarchar(128)", + maxLength: 128, + nullable: false, + oldClrType: typeof(string), + oldType: "nvarchar(450)"); + + migrationBuilder.AlterColumn( + name: "LoginProvider", + table: "AspNetUserTokens", + type: "nvarchar(128)", + maxLength: 128, + nullable: false, + oldClrType: typeof(string), + oldType: "nvarchar(450)"); + + migrationBuilder.AlterColumn( + name: "ProviderKey", + table: "AspNetUserLogins", + type: "nvarchar(128)", + maxLength: 128, + nullable: false, + oldClrType: typeof(string), + oldType: "nvarchar(450)"); + + migrationBuilder.AlterColumn( + name: "LoginProvider", + table: "AspNetUserLogins", + type: "nvarchar(128)", + maxLength: 128, + nullable: false, + oldClrType: typeof(string), + oldType: "nvarchar(450)"); + } + } +} diff --git a/Bulky.DataAccess/Migrations/ApplicationDbContextModelSnapshot.cs b/Bulky.DataAccess/Migrations/ApplicationDbContextModelSnapshot.cs index 5bf6e58..b45dde1 100644 --- a/Bulky.DataAccess/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/Bulky.DataAccess/Migrations/ApplicationDbContextModelSnapshot.cs @@ -63,6 +63,58 @@ protected override void BuildModel(ModelBuilder modelBuilder) }); }); + modelBuilder.Entity("BookBazaar.Models.Company", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("City") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PhoneNumber") + .HasColumnType("nvarchar(max)"); + + b.Property("PostalCode") + .HasColumnType("nvarchar(max)"); + + b.Property("State") + .HasColumnType("nvarchar(max)"); + + b.Property("StreetAddress") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Companies"); + + b.HasData( + new + { + Id = 1, + City = "New York", + Name = "Vivid Books", + PostalCode = "10000", + State = "NY", + StreetAddress = "123 Main St" + }, + new + { + Id = 2, + City = "New York", + Name = "Bookworm", + PostalCode = "10000", + State = "NY", + StreetAddress = "456 Elm St" + }); + }); + modelBuilder.Entity("BookBazaar.Models.Product", b => { b.Property("Id") @@ -353,12 +405,10 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => { b.Property("LoginProvider") - .HasMaxLength(128) - .HasColumnType("nvarchar(128)"); + .HasColumnType("nvarchar(450)"); b.Property("ProviderKey") - .HasMaxLength(128) - .HasColumnType("nvarchar(128)"); + .HasColumnType("nvarchar(450)"); b.Property("ProviderDisplayName") .HasColumnType("nvarchar(max)"); @@ -395,12 +445,10 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnType("nvarchar(450)"); b.Property("LoginProvider") - .HasMaxLength(128) - .HasColumnType("nvarchar(128)"); + .HasColumnType("nvarchar(450)"); b.Property("Name") - .HasMaxLength(128) - .HasColumnType("nvarchar(128)"); + .HasColumnType("nvarchar(450)"); b.Property("Value") .HasColumnType("nvarchar(max)"); diff --git a/Bulky.DataAccess/Repository/CompanyRepository.cs b/Bulky.DataAccess/Repository/CompanyRepository.cs new file mode 100644 index 0000000..8479c5b --- /dev/null +++ b/Bulky.DataAccess/Repository/CompanyRepository.cs @@ -0,0 +1,21 @@ +using BookBazaar.DataAccess.Data; +using BookBazaar.DataAccess.Repository.IRepository; +using BookBazaar.Models; + +namespace BookBazaar.DataAccess.Repository +{ + public class CompanyRepository : Repository, ICompanyRepository + { + private readonly ApplicationDbContext _db; + + public CompanyRepository(ApplicationDbContext db) : base(db) + { + _db = db; + } + + public void Update(Company obj) + { + _db.Companies.Update(obj); + } + } +} diff --git a/Bulky.DataAccess/Repository/IRepository/ICompanyRepository.cs b/Bulky.DataAccess/Repository/IRepository/ICompanyRepository.cs new file mode 100644 index 0000000..1758cf6 --- /dev/null +++ b/Bulky.DataAccess/Repository/IRepository/ICompanyRepository.cs @@ -0,0 +1,9 @@ +using BookBazaar.Models; + +namespace BookBazaar.DataAccess.Repository.IRepository +{ + public interface ICompanyRepository : IRepository + { + void Update(Company obj); + } +} diff --git a/Bulky.DataAccess/Repository/IRepository/IUnitOfWork.cs b/Bulky.DataAccess/Repository/IRepository/IUnitOfWork.cs index 05469f7..a83b250 100644 --- a/Bulky.DataAccess/Repository/IRepository/IUnitOfWork.cs +++ b/Bulky.DataAccess/Repository/IRepository/IUnitOfWork.cs @@ -4,6 +4,7 @@ public interface IUnitOfWork { ICategoryRepository Category { get; } IProductRepository Product { get; } + ICompanyRepository Company { get; } void Save(); } } diff --git a/Bulky.DataAccess/Repository/Repository.cs b/Bulky.DataAccess/Repository/Repository.cs index 1534eaf..7b9eca0 100644 --- a/Bulky.DataAccess/Repository/Repository.cs +++ b/Bulky.DataAccess/Repository/Repository.cs @@ -1,7 +1,6 @@ using BookBazaar.DataAccess.Data; using BookBazaar.DataAccess.Repository.IRepository; using Microsoft.EntityFrameworkCore; -using Microsoft.IdentityModel.Tokens; using System.Linq.Expressions; namespace BookBazaar.DataAccess.Repository @@ -19,10 +18,10 @@ public Repository(ApplicationDbContext db) public void Add(T entity) { - dbSet.Add(entity); + dbSet.Add(entity); } - public T Get(Expression> filter,string? includeProperties = null) + public T Get(Expression> filter, string? includeProperties = null) { IQueryable query = dbSet; query = query.Where(filter); @@ -36,7 +35,7 @@ public T Get(Expression> filter,string? includeProperties = null) } return query.FirstOrDefault(); } - + //Category is a navigation property in Product, so we need to include it //Category, CoverType, Company etc. may be navigation properties in Product public IEnumerable GetAll(string? includeProperties = null) diff --git a/Bulky.DataAccess/Repository/UnitOfWork.cs b/Bulky.DataAccess/Repository/UnitOfWork.cs index 4bd99ba..e3e4717 100644 --- a/Bulky.DataAccess/Repository/UnitOfWork.cs +++ b/Bulky.DataAccess/Repository/UnitOfWork.cs @@ -7,12 +7,14 @@ public class UnitOfWork : IUnitOfWork { private readonly ApplicationDbContext _db; public ICategoryRepository Category { get; private set; } + public ICompanyRepository Company { get; private set; } public IProductRepository Product { get; private set; } public UnitOfWork(ApplicationDbContext db) { _db = db; Category = new CategoryRepository(_db); Product = new ProductRepository(_db); + Company = new CompanyRepository(_db); } public void Save() diff --git a/Bulky.Models/Company.cs b/Bulky.Models/Company.cs new file mode 100644 index 0000000..9ca2a96 --- /dev/null +++ b/Bulky.Models/Company.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace BookBazaar.Models +{ + public class Company + { + [Key] + public int Id { get; set; } + [Required] + public string Name { get; set; } + public string? StreetAddress { get; set; } + public string? City { get; set; } + public string? State { get; set; } + public string? PostalCode { get; set; } + public string? PhoneNumber { get; set; } + + } +} diff --git a/BulkyWeb/Areas/Admin/Controllers/CompanyController.cs b/BulkyWeb/Areas/Admin/Controllers/CompanyController.cs new file mode 100644 index 0000000..3be5688 --- /dev/null +++ b/BulkyWeb/Areas/Admin/Controllers/CompanyController.cs @@ -0,0 +1,90 @@ +using BookBazaar.DataAccess.Repository.IRepository; +using BookBazaar.Models; +using BookBazaar.Utility; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; + +namespace BookBazaar.Areas.Admin.Controllers +{ + [Area("Admin")] + [Authorize(Roles = SD.Role_Admin)] + public class CompanyController : Controller + { + public readonly IUnitOfWork _unitOfWork; + + public CompanyController(IUnitOfWork unitOfWork) + { + _unitOfWork = unitOfWork; + } + public IActionResult Index() + { + List companyList = _unitOfWork.Company.GetAll().ToList(); + return View(companyList); + } + + public IActionResult Upsert(int? id) + { + if (id == null || id == 0) + { + //create + return View(new Company()); + } + else + { + //update + Company companyObj = _unitOfWork.Company.Get(c => c.Id == id); + return View(companyObj); + } + } + + [HttpPost] + public IActionResult Upsert(Company companyObj) + { + if (ModelState.IsValid) + { + if (companyObj.Id == 0) + { + _unitOfWork.Company.Add(companyObj); + TempData["Success"] = "Company created successfully"; + } + else + { + _unitOfWork.Company.Update(companyObj); + TempData["Success"] = "Company updated successfully"; + } + + _unitOfWork.Save(); + return RedirectToAction("Index"); + } + else + { + return View(companyObj); + } + } + + + #region API CALLS + [HttpGet] + public IActionResult GetAll() + { + List companyList = _unitOfWork.Company.GetAll().ToList(); + return Json(new { data = companyList }); + } + + [HttpDelete] + public IActionResult Delete(int? id) + { + var companyToBeDeleted = _unitOfWork.Company.Get(p => p.Id == id); + if (companyToBeDeleted == null) + { + return Json(new { success = false, message = "Error while deleting" }); + } + + _unitOfWork.Company.Remove(companyToBeDeleted); + _unitOfWork.Save(); + return Json(new { success = true, message = "Book deleted successfully" }); + } + #endregion + } +} diff --git a/BulkyWeb/Areas/Admin/Views/Company/Index.cshtml b/BulkyWeb/Areas/Admin/Views/Company/Index.cshtml new file mode 100644 index 0000000..cb1c7e8 --- /dev/null +++ b/BulkyWeb/Areas/Admin/Views/Company/Index.cshtml @@ -0,0 +1,36 @@ +
+
+
+
+

Company List

+
+
+
+
+ + + + + + + + + + + + +
NameAddressCityStatePhone Number
+
+
+ +@section Scripts { + +} \ No newline at end of file diff --git a/BulkyWeb/Areas/Admin/Views/Company/Upsert.cshtml b/BulkyWeb/Areas/Admin/Views/Company/Upsert.cshtml new file mode 100644 index 0000000..4c79191 --- /dev/null +++ b/BulkyWeb/Areas/Admin/Views/Company/Upsert.cshtml @@ -0,0 +1,72 @@ +@model Company + +
+
+
+
+

@(Model.Id != 0 ? "Update" : "Create") Company

+
+
+
+
+
+ +
+
+
+ @*
*@ +
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+ + + +
+
+
+ @if (Model.Id != 0) + { + + } + else + { + + } +
+ +
+
+
+
+
+
+
+ +@section Scripts { + +} diff --git a/BulkyWeb/Views/Shared/_Layout.cshtml b/BulkyWeb/Views/Shared/_Layout.cshtml index 3b65301..290b1f9 100644 --- a/BulkyWeb/Views/Shared/_Layout.cshtml +++ b/BulkyWeb/Views/Shared/_Layout.cshtml @@ -43,6 +43,10 @@ +
  • + } diff --git a/BulkyWeb/wwwroot/js/company.js b/BulkyWeb/wwwroot/js/company.js new file mode 100644 index 0000000..c7a2cf5 --- /dev/null +++ b/BulkyWeb/wwwroot/js/company.js @@ -0,0 +1,54 @@ +var dataTable; +$(document).ready(function () { + loadDataTable(); +}); + +function loadDataTable() { + dataTable = $('#tblData').DataTable({ + "ajax": { url: '/admin/company/getall' }, + "columns": [ + { data: 'name', width: '15%' }, + { data: 'streetAddress', width: '15%' }, + { data: 'city', width: '15%' }, + { data: 'state', width: '15%' }, + { data: 'phoneNumber', width: '15%' }, + { + data: 'id', + "render": function (data) { + return `` + }, + width: '25%' + } + ] + }); +} + +function Delete(url) { + Swal.fire({ + title: "Are you sure?", + text: "You won't be able to revert this!", + icon: "warning", + showCancelButton: true, + confirmButtonColor: "#3085d6", + cancelButtonColor: "#d33", + confirmButtonText: "Yes, delete it!" + }).then((result) => { + if (result.isConfirmed) { + $.ajax({ + type: "DELETE", + url: url, + success: function (data) { + if (data.success) { + toastr.success(data.message); + dataTable.ajax.reload(); + } else { + toastr.error(data.message); + } + } + }); + } + }); +} \ No newline at end of file