diff --git a/Pizzeria/Pizzeria.sln b/Pizzeria/Pizzeria.sln index 2f7e985..e536891 100644 --- a/Pizzeria/Pizzeria.sln +++ b/Pizzeria/Pizzeria.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaBusinessLogic", "Pi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaFileImplement", "PizzeriaFileImplement\PizzeriaFileImplement.csproj", "{5FA9C75E-4A06-4B3E-AD87-48B0C96BB4A1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaDatabaseImplement", "PizzeriaDatabaseImplement\PizzeriaDatabaseImplement.csproj", "{31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {5FA9C75E-4A06-4B3E-AD87-48B0C96BB4A1}.Debug|Any CPU.Build.0 = Debug|Any CPU {5FA9C75E-4A06-4B3E-AD87-48B0C96BB4A1}.Release|Any CPU.ActiveCfg = Release|Any CPU {5FA9C75E-4A06-4B3E-AD87-48B0C96BB4A1}.Release|Any CPU.Build.0 = Release|Any CPU + {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Component.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..d557e7c --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Component.cs @@ -0,0 +1,56 @@ +using PizzeriaDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; + +namespace PizzeriaDatabaseImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + [Required] + public string ComponentName { get; private set; } = string.Empty; + [Required] + public double Cost { get; set; } + [ForeignKey("ComponentId")] + public virtual List ProductComponents { get; set; } = new(); + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component Create(ComponentViewModel model) + { + return new Component + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs new file mode 100644 index 0000000..bd68926 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs @@ -0,0 +1,94 @@ +using PizzeriaDataModels.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; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; + +namespace PizzeriaDatabaseImplement.Models +{ + public class Pizza : IPizzaModel + { + public int Id { get; set; } + [Required] + public string PizzaName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _PizzaComponents = null; + [NotMapped] + public Dictionary PizzaComponents + { + get + { + if (_PizzaComponents == null) + { + _PizzaComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _PizzaComponents; + } + } + [ForeignKey("PizzaId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("PizzaId")] + public virtual List Orders { get; set; } = new(); + public static Pizza Create(PizzeriaDatabase context, PizzaBindingModel model) + { + return new Pizza() + { + Id = model.Id, + PizzaName = model.PizzaName, + Price = model.Price, + Components = model.PizzaComponents.Select(x => new PizzaComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(PizzaBindingModel model) + { + PizzaName = model.PizzaName; + Price = model.Price; + } + public PizzaViewModel GetViewModel => new() + { + Id = Id, + PizzaName = PizzaName, + Price = Price, + PizzaComponents = PizzaComponents + }; + public void UpdateComponents(PizzeriaDatabase context, PizzaBindingModel model) + { + var PizzaComponents = context.PizzaComponents.Where(rec => rec.PizzaId == model.Id).ToList(); + if (PizzaComponents != null && PizzaComponents.Count > 0) + { // удалили те, которых нет в модели + context.PizzaComponents.RemoveRange(PizzaComponents.Where(rec => !model.PizzaComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in PizzaComponents) + { + updateComponent.Count = model.PizzaComponents[updateComponent.ComponentId].Item2; + model.PizzaComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var Pizza = context.Pizzas.First(x => x.Id == Id); + foreach (var pc in model.PizzaComponents) + { + context.PizzaComponents.Add(new PizzaComponent + { + Pizza = Pizza, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _PizzaComponents = null; + } + } +} + diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/ProductComponent.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/ProductComponent.cs new file mode 100644 index 0000000..6671d7c --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/ProductComponent.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaDatabaseImplement.Models +{ + public class ProductComponent + { + public int Id { get; set; } + [Required] + public int ProductId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Pizza Pizza { get; set; } = new(); + } + +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj new file mode 100644 index 0000000..b612a23 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabaseImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + +