diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs index 51d04d5..cd260dc 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs @@ -22,5 +22,7 @@ namespace CarpentryWorkshopDatabaseImplement public virtual DbSet Woods { set; get; } public virtual DbSet WoodComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { get; set; } + public virtual DbSet ShopWoods { get; set; } } } \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..f1d2ef8 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,209 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDatabaseImplement.Models; +using CarpentryWorkshopDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + var element = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + using var context = new CarpentryWorkshopDatabase(); + return context.Shops + .Include(x => x.Woods) + .ThenInclude(x => x.Wood) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new CarpentryWorkshopDatabase(); + return context.Shops + .Include(x => x.Woods) + .ThenInclude(x => x.Wood) + .Select(x => x.GetViewModel) + .Where(x => x.ShopName.Contains(model.ShopName ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + using var context = new CarpentryWorkshopDatabase(); + return context.Shops + .Include(x => x.Woods) + .ThenInclude(x => x.Wood) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + try + { + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + if (context.Shops.Any(x => x.ShopName == newShop.ShopName)) + { + throw new Exception("Не должно быть два магазина с одним названием"); + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + catch + { + throw; + } + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + try + { + if (context.Shops.Any(x => (x.ShopName == model.ShopName && x.Id != model.Id))) + { + throw new Exception("Не должно быть два магазина с одним названием"); + } + shop.Update(model); + shop.UpdateWoods(context, model); + context.SaveChanges(); + return shop.GetViewModel; + } + catch + { + throw; + } + } + public bool RestockingShops(SupplyBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + var transaction = context.Database.BeginTransaction(); + var Shops = context.Shops.Include(x => x.Woods).ThenInclude(x => x.Wood).ToList(). + Where(x => x.WoodMaxCount > x.ShopWoods.Select(x => x.Value.Item2).Sum()).ToList(); + if (model == null) + { + return false; + } + try + { + foreach (Shop shop in Shops) + { + int difference = shop.WoodMaxCount - shop.ShopWoods.Select(x => x.Value.Item2).Sum(); + int refill = Math.Min(difference, model.Count); + model.Count -= refill; + if (shop.ShopWoods.ContainsKey(model.WoodId)) + { + var datePair = shop.ShopWoods[model.WoodId]; + datePair.Item2 += refill; + shop.ShopWoods[model.WoodId] = datePair; + } + else + { + var Wood = context.Woods.First(x => x.Id == model.WoodId); + shop.ShopWoods.Add(model.WoodId, (Wood, refill)); + } + shop.WoodsDictionatyUpdate(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 CarpentryWorkshopDatabase(); + var transaction = context.Database.BeginTransaction(); + try + { + var shops = context.Shops.Include(x => x.Woods).ThenInclude(x => x.Wood).ToList(). + Where(x => x.ShopWoods.ContainsKey(model.WoodId.Value)).OrderByDescending(x => x.ShopWoods[model.WoodId.Value].Item2).ToList(); + + foreach (var shop in shops) + { + int residue = model.Count.Value - shop.ShopWoods[model.WoodId.Value].Item2; + if (residue > 0) + { + shop.ShopWoods.Remove(model.WoodId.Value); + shop.WoodsDictionatyUpdate(context); + context.SaveChanges(); + model.Count = residue; + + } + else + { + if (residue == 0) + shop.ShopWoods.Remove(model.WoodId.Value); + else + { + var dataPair = shop.ShopWoods[model.WoodId.Value]; + dataPair.Item2 = -residue; + shop.ShopWoods[model.WoodId.Value] = dataPair; + } + + shop.WoodsDictionatyUpdate(context); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.Designer.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240511223517_InitialCreate.Designer.cs similarity index 68% rename from CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.Designer.cs rename to CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240511223517_InitialCreate.Designer.cs index 055a999..f579abb 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240511223517_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace CarpentryWorkshopDatabaseImplement.Migrations { [DbContext(typeof(CarpentryWorkshopDatabase))] - [Migration("20240319095810_InitialCreate")] + [Migration("20240511223517_InitialCreate")] partial class InitialCreate { /// @@ -82,6 +82,59 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("DateOpen") + .HasColumnType("timestamp with time zone"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.Property("WoodMaxCount") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.ShopWood", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.Property("WoodId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ShopId"); + + b.HasIndex("WoodId"); + + b.ToTable("ShopWoods"); + }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b => { b.Property("Id") @@ -137,6 +190,25 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations .IsRequired(); }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.ShopWood", b => + { + b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Shop", "Shop") + .WithMany("Woods") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Wood", "Wood") + .WithMany("ShopWoods") + .HasForeignKey("WoodId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Shop"); + + b.Navigation("Wood"); + }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.WoodComponent", b => { b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Component", "Component") @@ -161,11 +233,18 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations b.Navigation("WoodComponents"); }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Shop", b => + { + b.Navigation("Woods"); + }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b => { b.Navigation("Components"); b.Navigation("Orders"); + + b.Navigation("ShopWoods"); }); #pragma warning restore 612, 618 } diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240511223517_InitialCreate.cs similarity index 67% rename from CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.cs rename to CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240511223517_InitialCreate.cs index 6d91226..2e504ac 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240319095810_InitialCreate.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/20240511223517_InitialCreate.cs @@ -26,6 +26,22 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations table.PrimaryKey("PK_Components", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ShopName = table.Column(type: "text", nullable: false), + Address = table.Column(type: "text", nullable: false), + DateOpen = table.Column(type: "timestamp with time zone", nullable: false), + WoodMaxCount = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + migrationBuilder.CreateTable( name: "Woods", columns: table => new @@ -65,6 +81,33 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "ShopWoods", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + WoodId = table.Column(type: "integer", nullable: false), + ShopId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopWoods", x => x.Id); + table.ForeignKey( + name: "FK_ShopWoods_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopWoods_Woods_WoodId", + column: x => x.WoodId, + principalTable: "Woods", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateTable( name: "WoodComponents", columns: table => new @@ -97,6 +140,16 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations table: "Orders", column: "WoodId"); + migrationBuilder.CreateIndex( + name: "IX_ShopWoods_ShopId", + table: "ShopWoods", + column: "ShopId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopWoods_WoodId", + table: "ShopWoods", + column: "WoodId"); + migrationBuilder.CreateIndex( name: "IX_WoodComponents_ComponentId", table: "WoodComponents", @@ -114,9 +167,15 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations migrationBuilder.DropTable( name: "Orders"); + migrationBuilder.DropTable( + name: "ShopWoods"); + migrationBuilder.DropTable( name: "WoodComponents"); + migrationBuilder.DropTable( + name: "Shops"); + migrationBuilder.DropTable( name: "Components"); diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs index 7375e3b..b756112 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Migrations/CarpentryWorkshopDatabaseModelSnapshot.cs @@ -79,6 +79,59 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("DateOpen") + .HasColumnType("timestamp with time zone"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.Property("WoodMaxCount") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.ShopWood", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.Property("WoodId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ShopId"); + + b.HasIndex("WoodId"); + + b.ToTable("ShopWoods"); + }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b => { b.Property("Id") @@ -134,6 +187,25 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations .IsRequired(); }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.ShopWood", b => + { + b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Shop", "Shop") + .WithMany("Woods") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Wood", "Wood") + .WithMany("ShopWoods") + .HasForeignKey("WoodId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Shop"); + + b.Navigation("Wood"); + }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.WoodComponent", b => { b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Component", "Component") @@ -158,11 +230,18 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations b.Navigation("WoodComponents"); }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Shop", b => + { + b.Navigation("Woods"); + }); + modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b => { b.Navigation("Components"); b.Navigation("Orders"); + + b.Navigation("ShopWoods"); }); #pragma warning restore 612, 618 } diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Shop.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..c8785c5 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Shop.cs @@ -0,0 +1,131 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopDatabaseImplement.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 DateOpen { get; set; } + + [Required] + public int WoodMaxCount { get; set; } + + [ForeignKey("ShopId")] + public List Woods { get; set; } = new(); + + private Dictionary? _shopWoods = null; + + [NotMapped] + public Dictionary ShopWoods + { + get + { + if (_shopWoods == null) + { + _shopWoods = Woods + .ToDictionary(x => x.WoodId, x => + (x.Wood as IWoodModel, x.Count)); + } + return _shopWoods; + } + } + + public static Shop? Create(CarpentryWorkshopDatabase context, ShopBindingModel model) + { + if (model == null) + return null; + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpen = model.DateOpen, + WoodMaxCount = model.WoodMaxCount, + Woods = model.ShopWoods.Select(x => new ShopWood + { + Wood = context.Woods.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; + DateOpen = model.DateOpen; + WoodMaxCount = model.WoodMaxCount; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpen = DateOpen, + ShopWoods = ShopWoods, + WoodMaxCount = WoodMaxCount + }; + + public void UpdateWoods(CarpentryWorkshopDatabase context, + ShopBindingModel model) + { + var shopWoods = context.ShopWoods.Where(rec => + rec.ShopId == model.Id).ToList(); + if (shopWoods != null && shopWoods.Count > 0) + { + context.ShopWoods.RemoveRange(shopWoods.Where(rec => !model.ShopWoods.ContainsKey(rec.WoodId))); + + context.SaveChanges(); + foreach (var updateWood in shopWoods) + { + updateWood.Count = + model.ShopWoods[updateWood.WoodId].Item2; + model.ShopWoods.Remove(updateWood.WoodId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var pc in model.ShopWoods) + { + context.ShopWoods.Add(new ShopWood + { + Shop = shop, + Wood = context.Woods.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _shopWoods = null; + } + public void WoodsDictionatyUpdate(CarpentryWorkshopDatabase context) + { + UpdateWoods(context, new ShopBindingModel + { + Id = Id, + ShopWoods = ShopWoods, + }); + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/ShopWood.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/ShopWood.cs new file mode 100644 index 0000000..4347fc5 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/ShopWood.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopDatabaseImplement.Models +{ + public class ShopWood + { + public int Id { get; set; } + + [Required] + public int WoodId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Wood Wood { get; set; } = new(); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs index 6d01520..5ebd4d8 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs @@ -31,6 +31,8 @@ namespace CarpentryWorkshopDatabaseImplement.Models public virtual List Components { get; set; } = new(); [ForeignKey("WoodId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("WoodId")] + public virtual List ShopWoods { get; set; } = new(); public static Wood Create(CarpentryWorkshopDatabase context, WoodBindingModel model) { return new Wood()