From 41e2b23042ef1982831ee1be157632c99fc5a392 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Mon, 15 May 2023 09:39:07 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A2=D1=80=D0=B5=D1=82=D1=8C=D1=8F=20=D1=83?= =?UTF-8?q?=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D1=91=D0=BD=D0=BD=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0.=20=D0=A0?= =?UTF-8?q?=D0=B5=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=BE=20=D1=81?= =?UTF-8?q?=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B2?= =?UTF-8?q?=20=D0=91=D0=94,=20=D0=BF=D0=BE=D0=BF=D0=BB=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B8=20=D1=81=D0=BF=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=82=D1=80=D0=B5=D0=B1=D1=83=D0=B5=D1=82?= =?UTF-8?q?=20=D1=84=D0=B8=D0=BA=D1=81=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../ConfectioneryDatabase.cs | 4 +- .../Implements/ShopStorage.cs | 185 +++++++++++++ ...230515043657_HardInitialCreate.Designer.cs | 246 ++++++++++++++++++ .../20230515043657_HardInitialCreate.cs | 78 ++++++ .../ConfectioneryDatabaseModelSnapshot.cs | 77 ++++++ .../Models/Shop.cs | 112 ++++++++ .../Models/ShopPastry.cs | 22 ++ 8 files changed, 724 insertions(+), 1 deletion(-) create mode 100644 Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs create mode 100644 Confectionery/ConfectioneryDataBaseImplement/Migrations/20230515043657_HardInitialCreate.Designer.cs create mode 100644 Confectionery/ConfectioneryDataBaseImplement/Migrations/20230515043657_HardInitialCreate.cs create mode 100644 Confectionery/ConfectioneryDataBaseImplement/Models/Shop.cs create mode 100644 Confectionery/ConfectioneryDataBaseImplement/Models/ShopPastry.cs diff --git a/.gitignore b/.gitignore index ca1c7a3..ecb2ca5 100644 --- a/.gitignore +++ b/.gitignore @@ -398,3 +398,4 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +/Confectionery/ImplementationExtensions \ No newline at end of file diff --git a/Confectionery/ConfectioneryDataBaseImplement/ConfectioneryDatabase.cs b/Confectionery/ConfectioneryDataBaseImplement/ConfectioneryDatabase.cs index e4ec2a7..8e3d69e 100644 --- a/Confectionery/ConfectioneryDataBaseImplement/ConfectioneryDatabase.cs +++ b/Confectionery/ConfectioneryDataBaseImplement/ConfectioneryDatabase.cs @@ -15,7 +15,7 @@ namespace ConfectioneryDataBaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=ConfectioneryDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=ConfectioneryDatabaseHardLab;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -23,5 +23,7 @@ namespace ConfectioneryDataBaseImplement public virtual DbSet Pastrys { set; get; } public virtual DbSet PastryIngredients { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopPastries { set; get; } } } diff --git a/Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs b/Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..f0e52b0 --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,185 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataBaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Shops + .Include(x => x.Pastries) + .ThenInclude(x => x.Pastry) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Shops + .Include(x => x.Pastries) + .ThenInclude(x => x.Pastry) + .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 ConfectioneryDatabase(); + return context.Shops + .Include(x => x.Pastries) + .ThenInclude(x => x.Pastry) + .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 ConfectioneryDatabase(); + var newShop = Shop.Create(model, context); + if (newShop == null) + { + return null; + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new ConfectioneryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Shops.FirstOrDefault(rec => rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdatePastries(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Shops + .Include(x => x.Pastries) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public bool Supply(int pastryId, int count) + { + using ConfectioneryDatabase context = new ConfectioneryDatabase(); + var transaction = context.Database.BeginTransaction(); + foreach (Shop shop in context.Shops) + { + int freeShopSpace = shop.PastryCapacity - shop.ShopPastries.Select(y => y.Value.Item2).Sum(); + if (freeShopSpace > 0) + { + if (freeShopSpace >= count) + { + if (shop.ShopPastries.ContainsKey(pastryId)) + { + var shopPastry = shop.ShopPastries[pastryId]; + shopPastry.Item2 += count; + shop.ShopPastries[pastryId] = shopPastry; + } + else + { + Pastry pastry = context.Pastrys.First(x => x.Id == pastryId); + shop.ShopPastries.Add(pastryId, (pastry, count)); + } + shop.RemapPastries(context); + transaction.Commit(); + return true; + } + else + { + int dif = count - freeShopSpace; + count -= freeShopSpace; + if (shop.ShopPastries.TryGetValue(pastryId, out var pastryCount)) + { + var shopPastry = shop.ShopPastries[pastryId]; + shopPastry.Item2 = pastryCount.Item2 + count; + shop.ShopPastries[pastryId] = shopPastry; + } + else + { + Pastry pastry = context.Pastrys.First(x => x.Id == pastryId); + shop.ShopPastries.Add(pastryId, (pastry, count)); + } + shop.RemapPastries(context); + } + } + } + transaction.Rollback(); + return false; + } + + public bool Sell(int pastryId, int count) + { + using ConfectioneryDatabase context = new ConfectioneryDatabase(); + var transaction = context.Database.BeginTransaction(); + foreach (Shop shop in context.Shops) + { + int shopPastryCount = shop.ShopPastries.Select(x => x.Value).Where(x => x.Item1.Id == pastryId).Sum(x => x.Item2); + if (count - shopPastryCount >= 0) + { + count -= shopPastryCount; + shop.ShopPastries.Remove(pastryId); + } + else + { + var shopPastry = shop.ShopPastries[pastryId]; + shopPastry.Item2 -= count; + count = 0; + shop.ShopPastries[pastryId] = shopPastry; + } + shop.RemapPastries(context); + if (count == 0) + { + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230515043657_HardInitialCreate.Designer.cs b/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230515043657_HardInitialCreate.Designer.cs new file mode 100644 index 0000000..39afb7d --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230515043657_HardInitialCreate.Designer.cs @@ -0,0 +1,246 @@ +// +using System; +using ConfectioneryDataBaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDataBaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + [Migration("20230515043657_HardInitialCreate")] + partial class HardInitialCreate + { + /// + 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("ConfectioneryDataBaseImplement.Models.Ingredient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("IngredientName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Ingredients"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.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("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastrys"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.PastryIngredient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IngredientId") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("IngredientId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryIngredients"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("PastryCapacity") + .HasColumnType("int"); + + b.Property("ShopAdress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.ShopPastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPastries"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDataBaseImplement.Models.Pastry", null) + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.PastryIngredient", b => + { + b.HasOne("ConfectioneryDataBaseImplement.Models.Ingredient", "Ingredient") + .WithMany("PastryIgredients") + .HasForeignKey("IngredientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDataBaseImplement.Models.Pastry", "Pastry") + .WithMany("Ingredients") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Ingredient"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.ShopPastry", b => + { + b.HasOne("ConfectioneryDataBaseImplement.Models.Pastry", "Pastry") + .WithMany() + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDataBaseImplement.Models.Shop", "Shop") + .WithMany("Pastries") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Ingredient", b => + { + b.Navigation("PastryIgredients"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Pastry", b => + { + b.Navigation("Ingredients"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Shop", b => + { + b.Navigation("Pastries"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230515043657_HardInitialCreate.cs b/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230515043657_HardInitialCreate.cs new file mode 100644 index 0000000..6104ca7 --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230515043657_HardInitialCreate.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ConfectioneryDataBaseImplement.Migrations +{ + /// + public partial class HardInitialCreate : 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), + ShopAdress = table.Column(type: "nvarchar(max)", nullable: false), + OpeningDate = table.Column(type: "datetime2", nullable: false), + PastryCapacity = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopPastries", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopId = table.Column(type: "int", nullable: false), + PastryId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopPastries", x => x.Id); + table.ForeignKey( + name: "FK_ShopPastries_Pastrys_PastryId", + column: x => x.PastryId, + principalTable: "Pastrys", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopPastries_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopPastries_PastryId", + table: "ShopPastries", + column: "PastryId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPastries_ShopId", + table: "ShopPastries", + column: "ShopId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopPastries"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/Confectionery/ConfectioneryDataBaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs index 87da33b..0d31cfc 100644 --- a/Confectionery/ConfectioneryDataBaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs +++ b/Confectionery/ConfectioneryDataBaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace ConfectioneryDataBaseImplement.Migrations b.ToTable("PastryIngredients"); }); + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("PastryCapacity") + .HasColumnType("int"); + + b.Property("ShopAdress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.ShopPastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPastries"); + }); + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Order", b => { b.HasOne("ConfectioneryDataBaseImplement.Models.Pastry", null) @@ -149,6 +202,25 @@ namespace ConfectioneryDataBaseImplement.Migrations b.Navigation("Pastry"); }); + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.ShopPastry", b => + { + b.HasOne("ConfectioneryDataBaseImplement.Models.Pastry", "Pastry") + .WithMany() + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDataBaseImplement.Models.Shop", "Shop") + .WithMany("Pastries") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Ingredient", b => { b.Navigation("PastryIgredients"); @@ -160,6 +232,11 @@ namespace ConfectioneryDataBaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Shop", b => + { + b.Navigation("Pastries"); + }); #pragma warning restore 612, 618 } } diff --git a/Confectionery/ConfectioneryDataBaseImplement/Models/Shop.cs b/Confectionery/ConfectioneryDataBaseImplement/Models/Shop.cs new file mode 100644 index 0000000..e90d635 --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Models/Shop.cs @@ -0,0 +1,112 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +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 ConfectioneryDataBaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + [Required] + public string ShopName { get; private set; } = string.Empty; + [Required] + public string ShopAdress { get; private set; } = string.Empty; + [Required] + public DateTime OpeningDate { get; private set; } + [Required] + public int PastryCapacity { get; private set; } + private Dictionary? _shopPastries = null; + [NotMapped] + public Dictionary ShopPastries + { + get + { + if (_shopPastries == null) + { + _shopPastries = Pastries + .ToDictionary(recSP => recSP.PastryId, recPI => (recPI.Pastry as IPastryModel, recPI.Count)); + } + return _shopPastries; + } + } + [ForeignKey("ShopId")] + public virtual List Pastries { get; set; } = new(); + + public static Shop Create(ShopBindingModel model, ConfectioneryDatabase context) + { + return new Shop() + { + Id = model.Id, + ShopAdress = model.ShopAdress, + ShopName = model.ShopName, + OpeningDate = model.OpeningDate, + PastryCapacity = model.PastryCapacity, + Pastries = model.ShopPastries.Select(x => new ShopPastry { + Pastry = context.Pastrys.First(y => y.Id == x.Key), Count = x.Value.Item2 }).ToList() + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + ShopAdress = model.ShopAdress; + OpeningDate = model.OpeningDate; + PastryCapacity = model.PastryCapacity; + } + + public void UpdatePastries(ConfectioneryDatabase context, ShopBindingModel model) + { + var shopPastries = context.ShopPastries.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopPastries != null && shopPastries.Count > 0) + { + // удалили те, которых нет в модели + context.ShopPastries.RemoveRange(shopPastries.Where(rec => !model.ShopPastries.ContainsKey(rec.PastryId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateIngredient in shopPastries) + { + updateIngredient.Count = model.ShopPastries[updateIngredient.PastryId].Item2; + model.ShopPastries.Remove(updateIngredient.PastryId); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var sp in model.ShopPastries) + { + context.ShopPastries.Add(new ShopPastry + { + Shop = shop, + Pastry = context.Pastrys.First(x => x.Id == sp.Key), + Count = sp.Value.Item2 + }); + context.SaveChanges(); + } + _shopPastries = null; + } + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + ShopAdress = ShopAdress, + ShopPastries = ShopPastries, + OpeningDate = OpeningDate, + PastryCapacity = PastryCapacity, + }; + + public void RemapPastries(ConfectioneryDatabase context) + { + UpdatePastries(context, new ShopBindingModel { Id = Id, ShopPastries = ShopPastries }); + } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Models/ShopPastry.cs b/Confectionery/ConfectioneryDataBaseImplement/Models/ShopPastry.cs new file mode 100644 index 0000000..57e2830 --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Models/ShopPastry.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 ConfectioneryDataBaseImplement.Models +{ + public class ShopPastry + { + public int Id { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int PastryId { get; set; } + [Required] + public int Count { get; set; } + public virtual Shop Shop { get; set; } = new(); + public virtual Pastry Pastry { get; set; } = new(); + } +}