diff --git a/MotorPlant/MotorPlantDatabaseImplement/Engine.cs b/MotorPlant/MotorPlantDatabaseImplement/Engine.cs index 8a51aae..004be91 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Engine.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Engine.cs @@ -33,6 +33,8 @@ namespace MotorPlantDatabaseImplement.Models public virtual List Components { get; set; } = new(); [ForeignKey("EngineId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("EngineId")] + public virtual List ShopEngines { get; set; } = new(); public static Engine Create(MotorPlantDataBase context, EngineBindingModel model) { diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240308215333_InitialCreate.Designer.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240502092321_Initial-Create.Designer.cs similarity index 67% rename from MotorPlant/MotorPlantDatabaseImplement/Migrations/20240308215333_InitialCreate.Designer.cs rename to MotorPlant/MotorPlantDatabaseImplement/Migrations/20240502092321_Initial-Create.Designer.cs index 9e32da0..b68afd1 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240308215333_InitialCreate.Designer.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240502092321_Initial-Create.Designer.cs @@ -12,7 +12,7 @@ using MotorPlantDatabaseImplement; namespace MotorPlantDatabaseImplement.Migrations { [DbContext(typeof(MotorPlantDataBase))] - [Migration("20240308215333_InitialCreate")] + [Migration("20240502092321_Initial-Create")] partial class InitialCreate { /// @@ -124,6 +124,59 @@ namespace MotorPlantDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Adress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("EngineMaxCount") + .HasColumnType("int"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngines", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("EngineId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("EngineId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopEngines"); + }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b => { b.HasOne("MotorPlantDatabaseImplement.Models.Component", "Component") @@ -152,6 +205,25 @@ namespace MotorPlantDatabaseImplement.Migrations .IsRequired(); }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngines", b => + { + b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine") + .WithMany("ShopEngines") + .HasForeignKey("EngineId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MotorPlantDatabaseImplement.Models.Shop", "Shop") + .WithMany("Engines") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Engine"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Component", b => { b.Navigation("EngineComponents"); @@ -162,6 +234,13 @@ namespace MotorPlantDatabaseImplement.Migrations b.Navigation("Components"); b.Navigation("Orders"); + + b.Navigation("ShopEngines"); + }); + + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Shop", b => + { + b.Navigation("Engines"); }); #pragma warning restore 612, 618 } diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240308215333_InitialCreate.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240502092321_Initial-Create.cs similarity index 66% rename from MotorPlant/MotorPlantDatabaseImplement/Migrations/20240308215333_InitialCreate.cs rename to MotorPlant/MotorPlantDatabaseImplement/Migrations/20240502092321_Initial-Create.cs index a857529..8f45944 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240308215333_InitialCreate.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240502092321_Initial-Create.cs @@ -39,6 +39,22 @@ namespace MotorPlantDatabaseImplement.Migrations table.PrimaryKey("PK_Engines", x => x.Id); }); + 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), + Adress = table.Column(type: "nvarchar(max)", nullable: false), + OpeningDate = table.Column(type: "datetime2", nullable: false), + EngineMaxCount = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + migrationBuilder.CreateTable( name: "EngineComponents", columns: table => new @@ -90,6 +106,33 @@ namespace MotorPlantDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "ShopEngines", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + EngineId = table.Column(type: "int", nullable: false), + ShopId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopEngines", x => x.Id); + table.ForeignKey( + name: "FK_ShopEngines_Engines_EngineId", + column: x => x.EngineId, + principalTable: "Engines", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopEngines_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateIndex( name: "IX_EngineComponents_ComponentId", table: "EngineComponents", @@ -104,6 +147,16 @@ namespace MotorPlantDatabaseImplement.Migrations name: "IX_Orders_EngineId", table: "Orders", column: "EngineId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopEngines_EngineId", + table: "ShopEngines", + column: "EngineId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopEngines_ShopId", + table: "ShopEngines", + column: "ShopId"); } /// @@ -115,11 +168,17 @@ namespace MotorPlantDatabaseImplement.Migrations migrationBuilder.DropTable( name: "Orders"); + migrationBuilder.DropTable( + name: "ShopEngines"); + migrationBuilder.DropTable( name: "Components"); migrationBuilder.DropTable( name: "Engines"); + + migrationBuilder.DropTable( + name: "Shops"); } } } diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDataBaseModelSnapshot.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDataBaseModelSnapshot.cs index 7479b8f..db6ef98 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDataBaseModelSnapshot.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDataBaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace MotorPlantDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Adress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("EngineMaxCount") + .HasColumnType("int"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngines", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("EngineId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("EngineId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopEngines"); + }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b => { b.HasOne("MotorPlantDatabaseImplement.Models.Component", "Component") @@ -149,6 +202,25 @@ namespace MotorPlantDatabaseImplement.Migrations .IsRequired(); }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngines", b => + { + b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine") + .WithMany("ShopEngines") + .HasForeignKey("EngineId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MotorPlantDatabaseImplement.Models.Shop", "Shop") + .WithMany("Engines") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Engine"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Component", b => { b.Navigation("EngineComponents"); @@ -159,6 +231,13 @@ namespace MotorPlantDatabaseImplement.Migrations b.Navigation("Components"); b.Navigation("Orders"); + + b.Navigation("ShopEngines"); + }); + + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Shop", b => + { + b.Navigation("Engines"); }); #pragma warning restore 612, 618 } diff --git a/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDataBase.cs b/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDataBase.cs index 41ab4eb..74c0884 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDataBase.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/MotorPlantDataBase.cs @@ -16,7 +16,7 @@ optionsBuilder) { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-ETBGTEE;Initial Catalog=MotorPlantDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-ETBGTEE;Initial Catalog=MotorPlantHardDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -24,6 +24,7 @@ optionsBuilder) public virtual DbSet Engines { set; get; } public virtual DbSet EngineComponents { set; get; } public virtual DbSet Orders { set; get; } - + public virtual DbSet Shops { get; set; } + public virtual DbSet ShopEngines { get; set; } } } diff --git a/MotorPlant/MotorPlantDatabaseImplement/Shop.cs b/MotorPlant/MotorPlantDatabaseImplement/Shop.cs new file mode 100644 index 0000000..1e56eab --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/Shop.cs @@ -0,0 +1,107 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.ViewModels; +using MotorPlantDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + public string ShopName { get; set; } = string.Empty; + public string Adress { get; set; } = string.Empty; + public DateTime OpeningDate { get; set; } + public int EngineMaxCount { get; set; } + private Dictionary? _shopEngines = null; + public Dictionary ShopEngines + { + get + { + if (_shopEngines == null) + { + if (_shopEngines == null) + { + _shopEngines = Engines + .ToDictionary(recSP => recSP.EngineId, recSP => (recSP.Engine as IEngineModel, recSP.Count)); + } + return _shopEngines; + } + return _shopEngines; + } + } + + [ForeignKey("ShopId")] + public List Engines { get; set; } = new(); + public static Shop Create(MotorPlantDataBase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Adress = model.Adress, + OpeningDate = model.OpeningDate, + Engines = model.ShopEngines.Select(x => new ShopEngines + { + Engine = context.Engines.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + EngineMaxCount = model.EngineMaxCount + }; + } + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + EngineMaxCount = model.EngineMaxCount; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Adress = Adress, + OpeningDate = OpeningDate, + ShopEngines = ShopEngines, + EngineMaxCount = EngineMaxCount + }; + public void UpdateEngines(MotorPlantDataBase context, ShopBindingModel model) + { + var ShopEngines = context.ShopEngines.Where(rec => rec.ShopId == model.Id).ToList(); + if (ShopEngines != null && ShopEngines.Count > 0) + { + context.ShopEngines.RemoveRange(ShopEngines.Where(rec => !model.ShopEngines.ContainsKey(rec.EngineId))); + context.SaveChanges(); + ShopEngines = context.ShopEngines.Where(rec => rec.ShopId == model.Id).ToList(); + foreach (var updateEngine in ShopEngines) + { + updateEngine.Count = model.ShopEngines[updateEngine.EngineId].Item2; + model.ShopEngines.Remove(updateEngine.EngineId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var ar in model.ShopEngines) + { + context.ShopEngines.Add(new ShopEngines + { + Shop = shop, + Engine = context.Engines.First(x => x.Id == ar.Key), + Count = ar.Value.Item2 + }); + context.SaveChanges(); + } + _shopEngines = null; + } + public void EnginesDictionatyUpdate(MotorPlantDataBase context) + { + UpdateEngines(context, new ShopBindingModel + { + Id = Id, + ShopEngines = ShopEngines, + }); + } + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/ShopEngines.cs b/MotorPlant/MotorPlantDatabaseImplement/ShopEngines.cs new file mode 100644 index 0000000..69a9f38 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/ShopEngines.cs @@ -0,0 +1,24 @@ +using MotorPlantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantDatabaseImplement.Models +{ + public class ShopEngines + { + public int Id { get; set; } + [Required] + public int EngineId { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int Count { get; set; } + public virtual Shop Shop { get; set; } = new(); + public virtual Engine Engine { get; set; } = new(); + } +} diff --git a/MotorPlant/MotorPlantDatabaseImplement/ShopStorage.cs b/MotorPlant/MotorPlantDatabaseImplement/ShopStorage.cs new file mode 100644 index 0000000..61d9d34 --- /dev/null +++ b/MotorPlant/MotorPlantDatabaseImplement/ShopStorage.cs @@ -0,0 +1,182 @@ +using Microsoft.EntityFrameworkCore; +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.StoragesContracts; +using MotorPlantContracts.ViewModels; +using MotorPlantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new MotorPlantDataBase(); + return context.Shops.Include(x => x.Engines).ThenInclude(x => x.Engine).ToList(). + Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new MotorPlantDataBase(); + return context.Shops.Include(x => x.Engines).ThenInclude(x => x.Engine).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 new(); + } + using var context = new MotorPlantDataBase(); + return context.Shops.Include(x => x.Engines).ThenInclude(x => x.Engine) + .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 MotorPlantDataBase(); + 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 MotorPlantDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + shop.UpdateEngines(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new MotorPlantDataBase(); + var shop = context.Shops.Include(x => x.Engines).FirstOrDefault(x => x.Id == model.Id); + if (shop != null) + { + context.Shops.Remove(shop); + context.SaveChanges(); + return shop.GetViewModel; + } + return null; + } + public bool RestockingShops(SupplyBindingModel model) + { + using var context = new MotorPlantDataBase(); + var transaction = context.Database.BeginTransaction(); + var Shops = context.Shops.Include(x => x.Engines).ThenInclude(x => x.Engine).ToList(). + Where(x => x.EngineMaxCount > x.ShopEngines.Select(x => x.Value.Item2).Sum()).ToList(); + if (model == null) + { + return false; + } + try + { + foreach (Shop shop in Shops) + { + int difference = shop.EngineMaxCount - shop.ShopEngines.Select(x => x.Value.Item2).Sum(); + int refill = Math.Min(difference, model.Count); + model.Count -= refill; + if (shop.ShopEngines.ContainsKey(model.EngineId)) + { + var datePair = shop.ShopEngines[model.EngineId]; + datePair.Item2 += refill; + shop.ShopEngines[model.EngineId] = datePair; + } + else + { + var Engine = context.Engines.First(x => x.Id == model.EngineId); + shop.ShopEngines.Add(model.EngineId, (Engine, refill)); + } + shop.EnginesDictionatyUpdate(context); + if (model.Count == 0) + { + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + public bool Sale(SupplySearchModel model) + { + using var context = new MotorPlantDataBase(); + var transaction = context.Database.BeginTransaction(); + try + { + var shops = context.Shops.Include(x => x.Engines).ThenInclude(x => x.Engine).ToList(). + Where(x => x.ShopEngines.ContainsKey(model.EngineId.Value)).OrderByDescending(x => x.ShopEngines[model.EngineId.Value].Item2).ToList(); + + foreach (var shop in shops) + { + int residue = model.Count.Value - shop.ShopEngines[model.EngineId.Value].Item2; + if (residue > 0) + { + shop.ShopEngines.Remove(model.EngineId.Value); + shop.EnginesDictionatyUpdate(context); + context.SaveChanges(); + model.Count = residue; + + } + else + { + if (residue == 0) + shop.ShopEngines.Remove(model.EngineId.Value); + else + { + var dataPair = shop.ShopEngines[model.EngineId.Value]; + dataPair.Item2 = -residue; + shop.ShopEngines[model.EngineId.Value] = dataPair; + } + + shop.EnginesDictionatyUpdate(context); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +}