From d6232e904fb22df2dfb67ed61a1b929a267ffd3a Mon Sep 17 00:00:00 2001 From: russell Date: Sat, 27 Apr 2024 22:53:33 +0400 Subject: [PATCH] lab3_hard --- CarRepairShop/CarRepairShop.sln | 20 +- .../CarRepairShopDatabase.cs | 3 + .../Implements/ShopStorage.cs | 146 +++++++++++ .../20240418133633_ShopAddition.Designer.cs | 247 ++++++++++++++++++ .../Migrations/20240418133633_ShopAddition.cs | 75 ++++++ .../CarRepairShopDatabaseModelSnapshot.cs | 77 ++++++ .../Models/Repair.cs | 22 +- .../Models/Shop.cs | 110 ++++++++ .../Models/ShopRepair.cs | 22 ++ 9 files changed, 701 insertions(+), 21 deletions(-) create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Implements/ShopStorage.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240418133633_ShopAddition.Designer.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240418133633_ShopAddition.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Models/Shop.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Models/ShopRepair.cs diff --git a/CarRepairShop/CarRepairShop.sln b/CarRepairShop/CarRepairShop.sln index dd1c58a..3cb2c30 100644 --- a/CarRepairShop/CarRepairShop.sln +++ b/CarRepairShop/CarRepairShop.sln @@ -13,9 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopDataModels", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopListImplement", "CarRepairShopListImplement\CarRepairShopListImplement.csproj", "{5EF355A9-2708-47C4-B7B1-6D1CB89CDC02}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopFileImplement", "CarRepairShopFileImplement\CarRepairShopFileImplement.csproj", "{FD920623-E8C5-45DE-9D7F-A6C643102F9D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopFileImplement", "CarRepairShopFileImplement\CarRepairShopFileImplement.csproj", "{4966A8B7-5985-48DF-834A-3E41D2E0D01D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarRepairShopDatabaseImplement", "CarRepairShopDatabaseImplement\CarRepairShopDatabaseImplement.csproj", "{1AC87AFF-B786-44E5-AAAC-A87461E936FC}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopDatabaseImplement", "CarRepairShopDatabaseImplement\CarRepairShopDatabaseImplement.csproj", "{C40664B4-5073-45D2-9E3A-562EDE32CC51}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -43,14 +43,14 @@ Global {5EF355A9-2708-47C4-B7B1-6D1CB89CDC02}.Debug|Any CPU.Build.0 = Debug|Any CPU {5EF355A9-2708-47C4-B7B1-6D1CB89CDC02}.Release|Any CPU.ActiveCfg = Release|Any CPU {5EF355A9-2708-47C4-B7B1-6D1CB89CDC02}.Release|Any CPU.Build.0 = Release|Any CPU - {FD920623-E8C5-45DE-9D7F-A6C643102F9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FD920623-E8C5-45DE-9D7F-A6C643102F9D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FD920623-E8C5-45DE-9D7F-A6C643102F9D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FD920623-E8C5-45DE-9D7F-A6C643102F9D}.Release|Any CPU.Build.0 = Release|Any CPU - {1AC87AFF-B786-44E5-AAAC-A87461E936FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1AC87AFF-B786-44E5-AAAC-A87461E936FC}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1AC87AFF-B786-44E5-AAAC-A87461E936FC}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1AC87AFF-B786-44E5-AAAC-A87461E936FC}.Release|Any CPU.Build.0 = Release|Any CPU + {4966A8B7-5985-48DF-834A-3E41D2E0D01D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4966A8B7-5985-48DF-834A-3E41D2E0D01D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4966A8B7-5985-48DF-834A-3E41D2E0D01D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4966A8B7-5985-48DF-834A-3E41D2E0D01D}.Release|Any CPU.Build.0 = Release|Any CPU + {C40664B4-5073-45D2-9E3A-562EDE32CC51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C40664B4-5073-45D2-9E3A-562EDE32CC51}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C40664B4-5073-45D2-9E3A-562EDE32CC51}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C40664B4-5073-45D2-9E3A-562EDE32CC51}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabase.cs b/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabase.cs index 72c7416..ce9245d 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabase.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabase.cs @@ -21,5 +21,8 @@ namespace CarRepairShopDatabaseImplement public virtual DbSet RepairComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + + public virtual DbSet ShopRepairs { set; get; } } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Implements/ShopStorage.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..99c1d11 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,146 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.SearchModels; +using CarRepairShopContracts.StoragesContracts; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDatabaseImplement.Models; +using CarRepairShopDataModels.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarRepairShopDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new CarRepairShopDatabase(); + return context.Shops + .Include(x => x.Repairs) + .ThenInclude(x => x.Repair) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new CarRepairShopDatabase(); + return context.Shops + .Include(x => x.Repairs) + .ThenInclude(x => x.Repair) + .Where(x => x.ShopName.Contains(model.ShopName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + using var context = new CarRepairShopDatabase(); + return context.Shops + .Include(x => x.Repairs) + .ThenInclude(x => x.Repair) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new CarRepairShopDatabase(); + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new CarRepairShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + shop.UpdateRepairs(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new CarRepairShopDatabase(); + var element = context.Shops + .Include(x => x.Repairs) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public bool MakeSale(IRepairModel model, int count) + { + using var context = new CarRepairShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + foreach (var shop in context.Shops.Include(x => x.Repairs).ThenInclude(x => x.Repair) + .Where(x => x.Repairs.Any(x => x.RepairId == model.Id)) + .ToList()) + { + var repair = shop.ShopRepairs[model.Id]; + int min = Math.Min(repair.Item2, count); + if (min == repair.Item2) + { + shop.ShopRepairs.Remove(model.Id); + } + else + { + shop.ShopRepairs[model.Id] = (repair.Item1, repair.Item2 - min); + } + shop.UpdateRepairs(context, new() { Id = shop.Id, ShopRepairs = shop.ShopRepairs }); + count -= min; + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240418133633_ShopAddition.Designer.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240418133633_ShopAddition.Designer.cs new file mode 100644 index 0000000..764e8fa --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240418133633_ShopAddition.Designer.cs @@ -0,0 +1,247 @@ +// +using System; +using CarRepairShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace CarRepairShopDatabaseImplement.Migrations +{ + [DbContext(typeof(CarRepairShopDatabase))] + [Migration("20240418133633_ShopAddition")] + partial class ShopAddition + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.27") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("RepairId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("RepairId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Repair", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("RepairName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Repairs"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.RepairComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("RepairId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("RepairId"); + + b.ToTable("RepairComponents"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("RepairMaxAmount") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.ShopRepair", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("RepairId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("RepairId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopRepairs"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Order", b => + { + b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair") + .WithMany("Orders") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.RepairComponent", b => + { + b.HasOne("CarRepairShopDatabaseImplement.Models.Component", "Component") + .WithMany("RepairComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair") + .WithMany("Components") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.ShopRepair", b => + { + b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair") + .WithMany() + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CarRepairShopDatabaseImplement.Models.Shop", "Shop") + .WithMany("Repairs") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Repair"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Component", b => + { + b.Navigation("RepairComponents"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Repair", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Shop", b => + { + b.Navigation("Repairs"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240418133633_ShopAddition.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240418133633_ShopAddition.cs new file mode 100644 index 0000000..3216ce9 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240418133633_ShopAddition.cs @@ -0,0 +1,75 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace CarRepairShopDatabaseImplement.Migrations +{ + public partial class ShopAddition : 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), + RepairMaxAmount = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopRepairs", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopId = table.Column(type: "int", nullable: false), + RepairId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopRepairs", x => x.Id); + table.ForeignKey( + name: "FK_ShopRepairs_Repairs_RepairId", + column: x => x.RepairId, + principalTable: "Repairs", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopRepairs_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopRepairs_RepairId", + table: "ShopRepairs", + column: "RepairId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopRepairs_ShopId", + table: "ShopRepairs", + column: "ShopId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopRepairs"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/CarRepairShopDatabaseModelSnapshot.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/CarRepairShopDatabaseModelSnapshot.cs index 3d42a4a..9867922 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/CarRepairShopDatabaseModelSnapshot.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Migrations/CarRepairShopDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace CarRepairShopDatabaseImplement.Migrations b.ToTable("RepairComponents"); }); + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("RepairMaxAmount") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.ShopRepair", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("RepairId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("RepairId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopRepairs"); + }); + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Order", b => { b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair") @@ -151,6 +204,25 @@ namespace CarRepairShopDatabaseImplement.Migrations b.Navigation("Repair"); }); + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.ShopRepair", b => + { + b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair") + .WithMany() + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CarRepairShopDatabaseImplement.Models.Shop", "Shop") + .WithMany("Repairs") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Repair"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Component", b => { b.Navigation("RepairComponents"); @@ -162,6 +234,11 @@ namespace CarRepairShopDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Shop", b => + { + b.Navigation("Repairs"); + }); #pragma warning restore 612, 618 } } diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Models/Repair.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Repair.cs index 50495c0..01a8c4a 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/Models/Repair.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Repair.cs @@ -16,19 +16,19 @@ namespace CarRepairShopDatabaseImplement.Models [Required] public double Price { get; set; } - private Dictionary? _repairComponents = null; + private Dictionary? _productComponents = null; [NotMapped] public Dictionary RepairComponents { get { - if (_repairComponents == null) + if (_productComponents == null) { - _repairComponents = Components + _productComponents = Components .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); } - return _repairComponents; + return _productComponents; } } @@ -69,31 +69,31 @@ namespace CarRepairShopDatabaseImplement.Models public void UpdateComponents(CarRepairShopDatabase context, RepairBindingModel model) { - var repairComponents = context.RepairComponents.Where(rec => rec.RepairId == model.Id).ToList(); - if (repairComponents != null && repairComponents.Count > 0) + var productComponents = context.RepairComponents.Where(rec => rec.RepairId == model.Id).ToList(); + if (productComponents != null && productComponents.Count > 0) { // удалили те, которых нет в модели - context.RepairComponents.RemoveRange(repairComponents.Where(rec => !model.RepairComponents.ContainsKey(rec.ComponentId))); + context.RepairComponents.RemoveRange(productComponents.Where(rec => !model.RepairComponents.ContainsKey(rec.ComponentId))); context.SaveChanges(); // обновили количество у существующих записей - foreach (var updateComponent in repairComponents) + foreach (var updateComponent in productComponents) { updateComponent.Count = model.RepairComponents[updateComponent.ComponentId].Item2; model.RepairComponents.Remove(updateComponent.ComponentId); } context.SaveChanges(); } - var repair = context.Repairs.First(x => x.Id == Id); + var product = context.Repairs.First(x => x.Id == Id); foreach (var pc in model.RepairComponents) { context.RepairComponents.Add(new RepairComponent { - Repair = repair, + Repair = product, Component = context.Components.First(x => x.Id == pc.Key), Count = pc.Value.Item2 }); context.SaveChanges(); } - _repairComponents = null; + _productComponents = null; } } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Models/Shop.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..ba21637 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/Shop.cs @@ -0,0 +1,110 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace CarRepairShopDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + + [Required] + public string ShopName { get; set; } = string.Empty; + + [Required] + public string Address { get; set; } = string.Empty; + + [Required] + public DateTime DateOpening { get; set; } + + [Required] + public int RepairMaxAmount { get; set; } + + private Dictionary? _shopRepairs = null; + + [NotMapped] + public Dictionary ShopRepairs + { + get + { + if (_shopRepairs == null) + { + _shopRepairs = Repairs + .ToDictionary(x => x.RepairId, x => (x.Repair as IRepairModel, x.Count)); + } + return _shopRepairs; + } + } + + [ForeignKey("ShopId")] + public virtual List Repairs { get; set; } = new(); + + public static Shop Create(CarRepairShopDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + RepairMaxAmount = model.RepairMaxAmount, + Repairs = model.ShopRepairs.Select(x => new ShopRepair + { + Repair = context.Repairs.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + RepairMaxAmount = model.RepairMaxAmount; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + RepairMaxAmount = RepairMaxAmount, + ShopRepairs = ShopRepairs + }; + + public void UpdateRepairs(CarRepairShopDatabase context, ShopBindingModel model) + { + var shopRepairs = context.ShopRepairs.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopRepairs != null && shopRepairs.Count > 0) + { + context.ShopRepairs.RemoveRange(shopRepairs.Where(rec => !model.ShopRepairs.ContainsKey(rec.RepairId))); + context.SaveChanges(); + foreach (var updateRepair in shopRepairs) + { + if (model.ShopRepairs.ContainsKey(updateRepair.RepairId)) + { + updateRepair.Count = model.ShopRepairs[updateRepair.RepairId].Item2; + model.ShopRepairs.Remove(updateRepair.RepairId); + } + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var ic in model.ShopRepairs) + { + context.ShopRepairs.Add(new ShopRepair + { + Shop = shop, + Repair = context.Repairs.First(x => x.Id == ic.Key), + Count = ic.Value.Item2 + }); + context.SaveChanges(); + } + _shopRepairs = null; + } + } +} diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Models/ShopRepair.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Models/ShopRepair.cs new file mode 100644 index 0000000..87c4ddc --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/ShopRepair.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace CarRepairShopDatabaseImplement.Models +{ + public class ShopRepair + { + public int Id { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int RepairId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Repair Repair { get; set; } = new(); + + public virtual Shop Shop { get; set; } = new(); + } +}