diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/AbstractFoodOrdersDatabase.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/AbstractFoodOrdersDatabase.cs new file mode 100644 index 0000000..977eb92 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/AbstractFoodOrdersDatabase.cs @@ -0,0 +1,29 @@ +using AbstractFoodOrdersDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersDatabaseImplement +{ + public class AbstractFoodOrdersDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder + optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-7A1PHA0\SQLEXPRESS; + Initial Catalog=AbstractFoodOrdersDatabaseFull; + Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Dishes { set; get; } + public virtual DbSet DishComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/AbstractFoodOrdersDatabaseImplement.csproj b/FoodOrders/AbstractFoodOrdersDatabaseImplement/AbstractFoodOrdersDatabaseImplement.csproj new file mode 100644 index 0000000..17b6b3f --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/AbstractFoodOrdersDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Component.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..80508fa --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Component.cs @@ -0,0 +1,57 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.ViewModels; +using AbstractFoodOrdersDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace AbstractFoodOrdersDatabaseImplement.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 DishComponents { 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 + }; + } + +} \ No newline at end of file diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Dish.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Dish.cs new file mode 100644 index 0000000..8242596 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Dish.cs @@ -0,0 +1,101 @@ +using AbstractFoodOrdersDataModels.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 AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.ViewModels; + +namespace AbstractFoodOrdersDatabaseImplement.Models +{ + public class Dish : IDishModel + { + public int Id { get; set; } + [Required] + public string DishName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _dishComponents = null; + [NotMapped] + public Dictionary DishComponents + { + get + { + if (_dishComponents == null) + { + _dishComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _dishComponents; + } + } + [ForeignKey("ProductId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("ProductId")] + public virtual List Orders { get; set; } = new(); + public static Dish Create(AbstractFoodOrdersDatabase context, + DishBindingModel model) + { + return new Dish() + { + Id = model.Id, + DishName = model.DishName, + Price = model.Price, + Components = model.DishComponents.Select(x => new + DishComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(DishBindingModel model) + { + DishName = model.DishName; + Price = model.Price; + } + public DishViewModel GetViewModel => new() + { + Id = Id, + DishName = DishName, + Price = Price, + DishComponents = DishComponents + }; + public void UpdateComponents(AbstractFoodOrdersDatabase context, + DishBindingModel model) + { + var dishComponents = context.DishComponents.Where(rec => + rec.DishId == model.Id).ToList(); + if (dishComponents != null && dishComponents.Count > 0) + { // удалили те, которых нет в модели + context.DishComponents.RemoveRange(dishComponents.Where(rec + => !model.DishComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in dishComponents) + { + updateComponent.Count = + model.DishComponents[updateComponent.ComponentId].Item2; + model.DishComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var dish = context.Dishes.First(x => x.Id == Id); + foreach (var pc in model.DishComponents) + { + context.DishComponents.Add(new DishComponent + { + Dish = dish, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _dishComponents = null; + } + } +} diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/DishComponent.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/DishComponent.cs new file mode 100644 index 0000000..c8e17b5 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/DishComponent.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 AbstractFoodOrdersDatabaseImplement.Models +{ + public class DishComponent + { + public int Id { get; set; } + [Required] + public int DishId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Dish Dish { get; set; } = new(); + } +} diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Order.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..8aa66c3 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Order.cs @@ -0,0 +1,65 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.ViewModels; +using AbstractFoodOrdersDataModels.Enums; +using AbstractFoodOrdersDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int DishId { get; private set; } + [Required] + public int Count { get; private set; } + [Required] + public double Sum { get; private set; } + [Required] + public OrderStatus Status { get; private set; } + [Required] + public DateTime DateCreate { get; private set; } + public DateTime? DateImplement { get; private set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + DishId = model.DishId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + if (model.DateImplement.HasValue) DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + DishId = DishId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} diff --git a/FoodOrders/FoodOrders.sln b/FoodOrders/FoodOrders.sln index 52df192..37971b2 100644 --- a/FoodOrders/FoodOrders.sln +++ b/FoodOrders/FoodOrders.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFoodOrdersBusinessL EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFoodOrdersListImplement", "AbstractFoodOrdersListImplement\AbstractFoodOrdersListImplement.csproj", "{275ED134-536F-4025-BE10-2718F66FBD10}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFoodOrdersFileImplement", "AbstractShopFileImplement\AbstractFoodOrdersFileImplement.csproj", "{D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFoodOrdersFileImplement", "AbstractShopFileImplement\AbstractFoodOrdersFileImplement.csproj", "{D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFoodOrdersDatabaseImplement", "AbstractFoodOrdersDatabaseImplement\AbstractFoodOrdersDatabaseImplement.csproj", "{E6FA5D86-8EBB-46C7-93D5-CE9FFC7A3566}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}.Debug|Any CPU.Build.0 = Debug|Any CPU {D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}.Release|Any CPU.ActiveCfg = Release|Any CPU {D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}.Release|Any CPU.Build.0 = Release|Any CPU + {E6FA5D86-8EBB-46C7-93D5-CE9FFC7A3566}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E6FA5D86-8EBB-46C7-93D5-CE9FFC7A3566}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E6FA5D86-8EBB-46C7-93D5-CE9FFC7A3566}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E6FA5D86-8EBB-46C7-93D5-CE9FFC7A3566}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FoodOrders/FoodOrders/FoodOrders.csproj b/FoodOrders/FoodOrders/FoodOrders.csproj index d226432..9db7e33 100644 --- a/FoodOrders/FoodOrders/FoodOrders.csproj +++ b/FoodOrders/FoodOrders/FoodOrders.csproj @@ -9,8 +9,12 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - +