From 2510a971306ff75f89da380e94093b99f9d85877 Mon Sep 17 00:00:00 2001 From: ekallin Date: Sat, 9 Mar 2024 01:00:32 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D1=8B=20SushiComp?= =?UTF-8?q?onent=20&=20Sushi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SushiBarDatabaseImplement/Models/Component.cs | 2 +- SushiBarDatabaseImplement/Models/Order.cs | 12 --- SushiBarDatabaseImplement/Models/Sushi.cs | 92 +++++++++++++++++-- .../Models/SushiComponent.cs | 19 ++++ .../SushiBarDatabaseImplement.csproj | 1 + 5 files changed, 107 insertions(+), 19 deletions(-) delete mode 100644 SushiBarDatabaseImplement/Models/Order.cs create mode 100644 SushiBarDatabaseImplement/Models/SushiComponent.cs diff --git a/SushiBarDatabaseImplement/Models/Component.cs b/SushiBarDatabaseImplement/Models/Component.cs index 925d3a2..e394f9a 100644 --- a/SushiBarDatabaseImplement/Models/Component.cs +++ b/SushiBarDatabaseImplement/Models/Component.cs @@ -14,7 +14,7 @@ namespace SushiBarDatabaseImplement.Models [Required] public double Cost { get; set; } [ForeignKey("ComponentId")] - public virtual List ProductComponents { get; set; } = new(); + public virtual List SushiComponents { get; set; } = new(); public static Component? Create(ComponentBindingModel model) { if (model == null) diff --git a/SushiBarDatabaseImplement/Models/Order.cs b/SushiBarDatabaseImplement/Models/Order.cs deleted file mode 100644 index da6ff04..0000000 --- a/SushiBarDatabaseImplement/Models/Order.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace SushiBarDatabaseImplement.Models -{ - internal class Order - { - } -} diff --git a/SushiBarDatabaseImplement/Models/Sushi.cs b/SushiBarDatabaseImplement/Models/Sushi.cs index a47f437..52a7371 100644 --- a/SushiBarDatabaseImplement/Models/Sushi.cs +++ b/SushiBarDatabaseImplement/Models/Sushi.cs @@ -1,12 +1,92 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using SushiBarDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using SushiBarContracts.BindingModel; +using SushiBarContracts.ViewModels; + namespace SushiBarDatabaseImplement.Models { - internal class Sushi + public class Sushi : ISushiModel { + public int Id { get; set; } + [Required] + public string SushiName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _sushiComponents = null; + [NotMapped] + public Dictionary SushiComponents + { + get + { + if (_sushiComponents == null) + { + _sushiComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _sushiComponents; + } + } + [ForeignKey("SushiId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("SushiId")] + public virtual List Orders { get; set; } = new(); + public static Sushi Create(SushiBarDatabase context, SushiBindingModel model) + { + return new Sushi() + { + Id = model.Id, + SushiName = model.SushiName, + Price = model.Price, + Components = model.SushiComponents.Select(x => new SushiComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(SushiBindingModel model) + { + SushiName = model.SushiName; + Price = model.Price; + } + public SushiViewModel GetViewModel => new() + { + Id = Id, + SushiName = SushiName, + Price = Price, + SushiComponents = SushiComponents + }; + public void UpdateComponents(SushiBarDatabase context, SushiBindingModel model) + { + var SushiComponents = context.SushiComponents.Where(rec => rec.SushiId == model.Id).ToList(); + if (SushiComponents != null && SushiComponents.Count > 0) + { // удалили те, которых нет в модели + context.SushiComponents.RemoveRange(SushiComponents.Where(rec + => !model.SushiComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in SushiComponents) + { + updateComponent.Count = model.SushiComponents[updateComponent.ComponentId].Item2; + model.SushiComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var Sushi = context.Sushis.First(x => x.Id == Id); + foreach (var pc in model.SushiComponents) + { + context.SushiComponents.Add(new SushiComponent + { + Sushi = Sushi, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _sushiComponents = null; + } } } diff --git a/SushiBarDatabaseImplement/Models/SushiComponent.cs b/SushiBarDatabaseImplement/Models/SushiComponent.cs new file mode 100644 index 0000000..60c93fa --- /dev/null +++ b/SushiBarDatabaseImplement/Models/SushiComponent.cs @@ -0,0 +1,19 @@ +using System.ComponentModel.DataAnnotations; + + +namespace SushiBarDatabaseImplement.Models +{ + public class SushiComponent + { + public int Id { get; set; } + [Required] + public int SushiId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Sushi Sushi { get; set; } = new(); + + } +} diff --git a/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj b/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj index c1918fe..4a4eae2 100644 --- a/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj +++ b/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj @@ -18,6 +18,7 @@ +