diff --git a/Pizzeria/Pizzeria.sln b/Pizzeria/Pizzeria.sln index e436b48..a525f7b 100644 --- a/Pizzeria/Pizzeria.sln +++ b/Pizzeria/Pizzeria.sln @@ -15,7 +15,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaListImplement", "Pi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaFileImplement", "PizzeriaFileImplement\PizzeriaFileImplement.csproj", "{4B58D113-7C25-4795-8242-814C8288D30C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaDatabaseImplement", "PizzeriaDatabaseImplement\PizzeriaDatabaseImplement.csproj", "{DAE2ED2F-B064-40FA-B594-A360E041B485}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaDatabaseImplement", "PizzeriaDatabaseImplement\PizzeriaDatabaseImplement.csproj", "{DAE2ED2F-B064-40FA-B594-A360E041B485}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -43,10 +43,14 @@ Global {190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}.Debug|Any CPU.Build.0 = Debug|Any CPU {190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}.Release|Any CPU.ActiveCfg = Release|Any CPU {190E2EDD-BFA6-4213-9F8A-1B1D4FBBB8E4}.Release|Any CPU.Build.0 = Release|Any CPU - {357BD3B9-6100-469F-814F-7DABB2DDA33B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {357BD3B9-6100-469F-814F-7DABB2DDA33B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {357BD3B9-6100-469F-814F-7DABB2DDA33B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {357BD3B9-6100-469F-814F-7DABB2DDA33B}.Release|Any CPU.Build.0 = Release|Any CPU + {4B58D113-7C25-4795-8242-814C8288D30C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4B58D113-7C25-4795-8242-814C8288D30C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4B58D113-7C25-4795-8242-814C8288D30C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4B58D113-7C25-4795-8242-814C8288D30C}.Release|Any CPU.Build.0 = Release|Any CPU + {DAE2ED2F-B064-40FA-B594-A360E041B485}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DAE2ED2F-B064-40FA-B594-A360E041B485}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DAE2ED2F-B064-40FA-B594-A360E041B485}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DAE2ED2F-B064-40FA-B594-A360E041B485}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/ShopLogic.cs b/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/ShopLogic.cs index 5e2628e..69784db 100644 --- a/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/Pizzeria/PizzeriaBusinessLogic/BusinessLogics/ShopLogic.cs @@ -125,6 +125,17 @@ namespace PizzeriaBusinessLogic.BusinessLogics } shop.ShopPizzas.Add(model.PizzaId, (pizza, model.Count)); } + + _shopStorage.Update(new ShopBindingModel() + { + Id = shop.Id, + ShopName = shop.ShopName, + Adress = shop.Adress, + OpeningDate = shop.OpeningDate, + ShopPizzas = shop.ShopPizzas, + PizzaMaxCount = shop.PizzaMaxCount, + }); + return true; } diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/ShopStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..b2aaacc --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,185 @@ +using Microsoft.EntityFrameworkCore; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDatabaseImplement.Models; + +namespace PizzeriaDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new PizzeriaDatabase(); + return context.Shops.Include(x => x.Pizzas).ThenInclude(x => x.Pizza).ToList(). + Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new PizzeriaDatabase(); + return context.Shops.Include(x => x.Pizzas).ThenInclude(x => x.Pizza).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 PizzeriaDatabase(); + return context.Shops.Include(x => x.Pizzas).ThenInclude(x => x.Pizza) + .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 PizzeriaDatabase(); + 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 PizzeriaDatabase(); + 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.UpdatePizzas(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new PizzeriaDatabase(); + var shop = context.Shops.Include(x => x.Pizzas).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 PizzeriaDatabase(); + var transaction = context.Database.BeginTransaction(); + var Shops = context.Shops.Include(x => x.Pizzas).ThenInclude(x => x.Pizza).ToList(). + Where(x => x.PizzaMaxCount > x.ShopPizzas.Select(x => x.Value.Item2).Sum()).ToList(); + if (model == null) + { + return false; + } + try + { + foreach (Shop shop in Shops) + { + int difference = shop.PizzaMaxCount - shop.ShopPizzas.Select(x => x.Value.Item2).Sum(); + int refill = Math.Min(difference, model.Count); + model.Count -= refill; + if (shop.ShopPizzas.ContainsKey(model.PizzaId)) + { + var datePair = shop.ShopPizzas[model.PizzaId]; + datePair.Item2 += refill; + shop.ShopPizzas[model.PizzaId] = datePair; + } + else + { + var pizza = context.Pizzas.First(x => x.Id == model.PizzaId); + shop.ShopPizzas.Add(model.PizzaId, (pizza, refill)); + } + shop.PizzasDictionatyUpdate(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 PizzeriaDatabase(); + var transaction = context.Database.BeginTransaction(); + try + { + var shops = context.Shops.Include(x => x.Pizzas).ThenInclude(x => x.Pizza).ToList(). + Where(x => x.ShopPizzas.ContainsKey(model.PizzaId.Value)).OrderByDescending(x => x.ShopPizzas[model.PizzaId.Value].Item2).ToList(); + + foreach (var shop in shops) + { + int residue = model.Count.Value - shop.ShopPizzas[model.PizzaId.Value].Item2; + if (residue > 0) + { + shop.ShopPizzas.Remove(model.PizzaId.Value); + shop.PizzasDictionatyUpdate(context); + context.SaveChanges(); + model.Count = residue; + + } + else + { + if (residue == 0) + shop.ShopPizzas.Remove(model.PizzaId.Value); + else + { + var dataPair = shop.ShopPizzas[model.PizzaId.Value]; + dataPair.Item2 = -residue; + shop.ShopPizzas[model.PizzaId.Value] = dataPair; + } + + shop.PizzasDictionatyUpdate(context); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20240305050232_Hard_init.Designer.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20240305050232_Hard_init.Designer.cs new file mode 100644 index 0000000..582e5e1 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20240305050232_Hard_init.Designer.cs @@ -0,0 +1,248 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using PizzeriaDatabaseImplement; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + [DbContext(typeof(PizzeriaDatabase))] + [Migration("20240305050232_Hard_init")] + partial class Hard_init + { + /// + 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("PizzeriaDatabaseImplement.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("PizzeriaDatabaseImplement.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("PizzaId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PizzaName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pizzas"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.PizzaComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PizzaId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PizzaId"); + + b.ToTable("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Adress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("PizzaMaxCount") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.ShopPizzas", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PizzaId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PizzaId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPizzas"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Order", b => + { + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") + .WithMany("Orders") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.PizzaComponent", b => + { + b.HasOne("PizzeriaDatabaseImplement.Models.Component", "Component") + .WithMany("PizzaComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") + .WithMany("Components") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.ShopPizzas", b => + { + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") + .WithMany("ShopPizzas") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PizzeriaDatabaseImplement.Models.Shop", "Shop") + .WithMany("Pizzas") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pizza"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b => + { + b.Navigation("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Pizza", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Shop", b => + { + b.Navigation("Pizzas"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20240305050232_Hard_init.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20240305050232_Hard_init.cs new file mode 100644 index 0000000..a85816b --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20240305050232_Hard_init.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + /// + public partial class Hard_init : 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), + Adress = table.Column(type: "nvarchar(max)", nullable: false), + OpeningDate = table.Column(type: "datetime2", nullable: false), + PizzaMaxCount = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopPizzas", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PizzaId = 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_ShopPizzas", x => x.Id); + table.ForeignKey( + name: "FK_ShopPizzas_Pizzas_PizzaId", + column: x => x.PizzaId, + principalTable: "Pizzas", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopPizzas_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopPizzas_PizzaId", + table: "ShopPizzas", + column: "PizzaId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPizzas_ShopId", + table: "ShopPizzas", + column: "ShopId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopPizzas"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs index b5035a2..b8008e1 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace PizzeriaDatabaseImplement.Migrations b.ToTable("PizzaComponents"); }); + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Adress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("PizzaMaxCount") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.ShopPizzas", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PizzaId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PizzaId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPizzas"); + }); + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Order", b => { b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") @@ -151,6 +204,25 @@ namespace PizzeriaDatabaseImplement.Migrations b.Navigation("Pizza"); }); + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.ShopPizzas", b => + { + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") + .WithMany("ShopPizzas") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PizzeriaDatabaseImplement.Models.Shop", "Shop") + .WithMany("Pizzas") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pizza"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b => { b.Navigation("PizzaComponents"); @@ -162,6 +234,11 @@ namespace PizzeriaDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Shop", b => + { + b.Navigation("Pizzas"); + }); #pragma warning restore 612, 618 } } diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs index 55bb815..61bf1e9 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs @@ -33,6 +33,9 @@ namespace PizzeriaDatabaseImplement.Models [ForeignKey("PizzaId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("PizzaId")] + public virtual List ShopPizzas { get; set; } = new(); + public static Pizza Create(PizzeriaDatabase context, PizzaBindingModel model) { diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Shop.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..0b3f928 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Shop.cs @@ -0,0 +1,114 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; + +namespace PizzeriaDatabaseImplement.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 PizzaMaxCount { get; set; } + + private Dictionary? _shopPizzas = null; + + public Dictionary ShopPizzas + { + get + { + if (_shopPizzas == null) + { + if (_shopPizzas == null) + { + _shopPizzas = Pizzas + .ToDictionary(recSP => recSP.PizzaId, recSP => (recSP.Pizza as IPizzaModel, recSP.Count)); + } + return _shopPizzas; + } + return _shopPizzas; + } + } + + [ForeignKey("ShopId")] + public List Pizzas { get; set; } = new(); + + public static Shop Create(PizzeriaDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Adress = model.Adress, + OpeningDate = model.OpeningDate, + Pizzas = model.ShopPizzas.Select(x => new ShopPizzas + { + Pizza = context.Pizzas.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + PizzaMaxCount = model.PizzaMaxCount + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + PizzaMaxCount = model.PizzaMaxCount; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Adress = Adress, + OpeningDate = OpeningDate, + ShopPizzas = ShopPizzas, + PizzaMaxCount = PizzaMaxCount + }; + + public void UpdatePizzas(PizzeriaDatabase context, ShopBindingModel model) + { + var ShopPizzas = context.ShopPizzas.Where(rec => rec.ShopId == model.Id).ToList(); + if (ShopPizzas != null && ShopPizzas.Count > 0) + { + context.ShopPizzas.RemoveRange(ShopPizzas.Where(rec => !model.ShopPizzas.ContainsKey(rec.PizzaId))); + context.SaveChanges(); + ShopPizzas = context.ShopPizzas.Where(rec => rec.ShopId == model.Id).ToList(); + foreach (var updatePizza in ShopPizzas) + { + updatePizza.Count = model.ShopPizzas[updatePizza.PizzaId].Item2; + model.ShopPizzas.Remove(updatePizza.PizzaId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var ar in model.ShopPizzas) + { + context.ShopPizzas.Add(new ShopPizzas + { + Shop = shop, + Pizza = context.Pizzas.First(x => x.Id == ar.Key), + Count = ar.Value.Item2 + }); + context.SaveChanges(); + } + _shopPizzas = null; + } + + public void PizzasDictionatyUpdate(PizzeriaDatabase context) + { + UpdatePizzas(context, new ShopBindingModel + { + Id = Id, + ShopPizzas = ShopPizzas, + }); + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/ShopPizzas.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/ShopPizzas.cs new file mode 100644 index 0000000..2d5a371 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/ShopPizzas.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace PizzeriaDatabaseImplement.Models +{ + public class ShopPizzas + { + public int Id { get; set; } + + [Required] + public int PizzaId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Pizza Pizza { get; set; } = new(); + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs index 2a77187..ee554df 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs @@ -14,9 +14,11 @@ namespace PizzeriaDatabaseImplement } base.OnConfiguring(optionsBuilder); } - public virtual DbSet Components { set; get; } - public virtual DbSet Pizzas { set; get; } - public virtual DbSet PizzaComponents { set; get; } - public virtual DbSet Orders { set; get; } + public virtual DbSet Components { get; set; } + public virtual DbSet Pizzas { get; set; } + public virtual DbSet PizzaComponents { get; set; } + public virtual DbSet Orders { get; set; } + public virtual DbSet Shops { get; set; } + public virtual DbSet ShopPizzas { get; set; } } }