diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs index a0ce249..47dd565 100644 --- a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/FurnitureAssemblyDatabase.cs @@ -22,5 +22,8 @@ namespace FurnitureAssemblyDatabaseImplement public virtual DbSet Furnitures { set; get; } public virtual DbSet FurnitureComponents { set; get; } public virtual DbSet Orders { set; get; } + + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopFurniture { set; get; } } } diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20230312131725_ThirdMigration.Designer.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20230312131725_ThirdMigration.Designer.cs new file mode 100644 index 0000000..db69949 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20230312131725_ThirdMigration.Designer.cs @@ -0,0 +1,251 @@ +// +using System; +using FurnitureAssemblyDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace FurnitureAssemblyDatabaseImplement.Migrations +{ + [DbContext(typeof(FurnitureAssemblyDatabase))] + [Migration("20230312131725_ThirdMigration")] + partial class ThirdMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Furniture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("FurnitureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Furnitures"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.FurnitureComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("FurnitureId") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("FurnitureId"); + + b.ToTable("FurnitureComponents"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("FurnitureId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCount") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.ShopFurniture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("FurnitureId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopFurniture"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.FurnitureComponent", b => + { + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Component", "Component") + .WithMany("FurnitureComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Furniture", "Furniture") + .WithMany("Components") + .HasForeignKey("FurnitureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Furniture"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Order", b => + { + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Furniture", "Furniture") + .WithMany("Orders") + .HasForeignKey("FurnitureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Furniture"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.ShopFurniture", b => + { + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Furniture", "Furniture") + .WithMany() + .HasForeignKey("FurnitureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Shop", "Shop") + .WithMany("ShopFurnitures") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Furniture"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Component", b => + { + b.Navigation("FurnitureComponents"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Furniture", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Shop", b => + { + b.Navigation("ShopFurnitures"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20230312131725_ThirdMigration.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20230312131725_ThirdMigration.cs new file mode 100644 index 0000000..1b81384 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/20230312131725_ThirdMigration.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace FurnitureAssemblyDatabaseImplement.Migrations +{ + /// + public partial class ThirdMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopName = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + DateOpening = table.Column(type: "datetime2", nullable: false), + MaxCount = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopFurniture", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopId = table.Column(type: "int", nullable: false), + FurnitureId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopFurniture", x => x.Id); + table.ForeignKey( + name: "FK_ShopFurniture_Furnitures_FurnitureId", + column: x => x.FurnitureId, + principalTable: "Furnitures", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopFurniture_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopFurniture_FurnitureId", + table: "ShopFurniture", + column: "FurnitureId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopFurniture_ShopId", + table: "ShopFurniture", + column: "ShopId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopFurniture"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/FurnitureAssemblyDatabaseModelSnapshot.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/FurnitureAssemblyDatabaseModelSnapshot.cs index e24f216..b892449 100644 --- a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/FurnitureAssemblyDatabaseModelSnapshot.cs +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Migrations/FurnitureAssemblyDatabaseModelSnapshot.cs @@ -124,6 +124,59 @@ namespace FurnitureAssemblyDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCount") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.ShopFurniture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("FurnitureId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("FurnitureId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopFurniture"); + }); + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.FurnitureComponent", b => { b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Component", "Component") @@ -154,6 +207,25 @@ namespace FurnitureAssemblyDatabaseImplement.Migrations b.Navigation("Furniture"); }); + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.ShopFurniture", b => + { + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Furniture", "Furniture") + .WithMany() + .HasForeignKey("FurnitureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Shop", "Shop") + .WithMany("ShopFurnitures") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Furniture"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Component", b => { b.Navigation("FurnitureComponents"); @@ -165,6 +237,11 @@ namespace FurnitureAssemblyDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Shop", b => + { + b.Navigation("ShopFurnitures"); + }); #pragma warning restore 612, 618 } } diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Shop.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..c3fe6ac --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Shop.cs @@ -0,0 +1,117 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Models; +using Microsoft.EntityFrameworkCore; +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 FurnitureAssemblyDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + [Required] + public string ShopName { get; private set; } = string.Empty; + [Required] + public string Address { get; private set; } = string.Empty; + [Required] + public DateTime DateOpening { get; private set; } + + private Dictionary? _furnitures = null; + [NotMapped] + public Dictionary Furnitures + { + get + { + if (_furnitures == null) + { + _furnitures = ShopFurnitures + .ToDictionary(recSF => recSF.FurnitureId, recSF => + (recSF.Furniture as IFurnitureModel, recSF.Count)); + + } + return _furnitures; + } + private set { } + } + + public int MaxCount { get; private set; } + + [ForeignKey("ShopId")] + public virtual List ShopFurnitures { get; set; } = new(); + + public static Shop? Create(FurnitureAssemblyDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + MaxCount = model.MaxCount, + ShopFurnitures = model.Furnitures.Select(x => new ShopFurniture + { + Furniture = context.Furnitures.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + MaxCount = model.MaxCount; + DateOpening = model.DateOpening; + Furnitures = model.Furnitures; + } + + public void UpdateFurnitures(FurnitureAssemblyDatabase context, ShopBindingModel model) + { + var shopFurnitures = context.ShopFurniture.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopFurnitures != null && shopFurnitures.Count >= 0) + { // удалили те, которых нет в модели + context.ShopFurniture.RemoveRange(shopFurnitures.Where(rec => !model.Furnitures.ContainsKey(rec.FurnitureId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in shopFurnitures) + { + updateComponent.Count = model.Furnitures[updateComponent.FurnitureId].Item2; + model.Furnitures.Remove(updateComponent.FurnitureId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var pc in model.Furnitures) + { + context.ShopFurniture.Add(new ShopFurniture + { + Shop = shop, + Furniture = context.Furnitures.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _furnitures = null; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + MaxCount = MaxCount, + Furnitures = Furnitures + }; + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/ShopFurniture.cs b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/ShopFurniture.cs new file mode 100644 index 0000000..2319847 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/ShopFurniture.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyDatabaseImplement.Models +{ + public class ShopFurniture + { + public int Id { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int FurnitureId { get; set; } + [Required] + public int Count { get; set; } + public virtual Shop Shop { get; set; } = new(); + public virtual Furniture Furniture { get; set; } = new(); + } +}