From 7772bf7f38c88752c574ab23841f57bf124b7c42 Mon Sep 17 00:00:00 2001 From: goblinrf Date: Tue, 30 Apr 2024 00:56:34 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20=D0=B1=D0=B4=20=D1=82=20=D0=BF=D1=80=D0=BE=D0=B4=D0=B5?= =?UTF-8?q?=D0=B1=D0=B0=D0=B6=D0=B8=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ShopLogic.cs | 11 +- Diner/Diner.sln | 6 + Diner/Diner/DinerView.csproj | 3 + Diner/Diner/Program.cs | 3 +- .../DinerDatabaseImplements/DinerDatabase.cs | 24 +++ .../DinerDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 79 ++++++++ .../Implements/OrderStorage.cs | 79 ++++++++ .../Implements/ShopStorage.cs | 191 ++++++++++++++++++ .../Implements/SnackStorage.cs | 94 +++++++++ .../Models/Component.cs | 58 ++++++ Diner/DinerDatabaseImplements/Models/Order.cs | 73 +++++++ Diner/DinerDatabaseImplements/Models/Shop.cs | 120 +++++++++++ .../Models/ShopSnacks.cs | 27 +++ Diner/DinerDatabaseImplements/Models/Snack.cs | 91 +++++++++ .../Models/SnackComponent.cs | 22 ++ 16 files changed, 902 insertions(+), 2 deletions(-) create mode 100644 Diner/DinerDatabaseImplements/DinerDatabase.cs create mode 100644 Diner/DinerDatabaseImplements/DinerDatabaseImplement.csproj create mode 100644 Diner/DinerDatabaseImplements/Implements/ComponentStorage.cs create mode 100644 Diner/DinerDatabaseImplements/Implements/OrderStorage.cs create mode 100644 Diner/DinerDatabaseImplements/Implements/ShopStorage.cs create mode 100644 Diner/DinerDatabaseImplements/Implements/SnackStorage.cs create mode 100644 Diner/DinerDatabaseImplements/Models/Component.cs create mode 100644 Diner/DinerDatabaseImplements/Models/Order.cs create mode 100644 Diner/DinerDatabaseImplements/Models/Shop.cs create mode 100644 Diner/DinerDatabaseImplements/Models/ShopSnacks.cs create mode 100644 Diner/DinerDatabaseImplements/Models/Snack.cs create mode 100644 Diner/DinerDatabaseImplements/Models/SnackComponent.cs diff --git a/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs b/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs index 75076cd..502d9af 100644 --- a/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/AbstractShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -126,7 +126,16 @@ namespace DinerBusinessLogic.BusinessLogics } shop.ShopSnacks.Add(model.SnackId, (snack, model.Count)); } - return true; + _shopStorage.Update(new ShopBindingModel() + { + Id = shop.Id, + ShopName = shop.ShopName, + Adress = shop.Adress, + OpeningDate = shop.OpeningDate, + ShopSnacks = shop.ShopSnacks, + SnackMaxCount = shop.SnackMaxCount, + }); + return true; } private void CheckModel(ShopBindingModel model, bool withParams = true) diff --git a/Diner/Diner.sln b/Diner/Diner.sln index 6cb40de..62f68e0 100644 --- a/Diner/Diner.sln +++ b/Diner/Diner.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerListImplement", "Abstr EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerFileImplement", "DinerFileImplement\DinerFileImplement.csproj", "{13294C1E-C8DF-4E4B-B645-A61900A797EB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerDatabaseImplement", "DinerDatabaseImplements\DinerDatabaseImplement.csproj", "{BE778091-B058-4BDD-8AEE-2773F4E00979}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {13294C1E-C8DF-4E4B-B645-A61900A797EB}.Debug|Any CPU.Build.0 = Debug|Any CPU {13294C1E-C8DF-4E4B-B645-A61900A797EB}.Release|Any CPU.ActiveCfg = Release|Any CPU {13294C1E-C8DF-4E4B-B645-A61900A797EB}.Release|Any CPU.Build.0 = Release|Any CPU + {BE778091-B058-4BDD-8AEE-2773F4E00979}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BE778091-B058-4BDD-8AEE-2773F4E00979}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BE778091-B058-4BDD-8AEE-2773F4E00979}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BE778091-B058-4BDD-8AEE-2773F4E00979}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Diner/Diner/DinerView.csproj b/Diner/Diner/DinerView.csproj index a7b8bd0..f8854ee 100644 --- a/Diner/Diner/DinerView.csproj +++ b/Diner/Diner/DinerView.csproj @@ -19,7 +19,10 @@ + + + diff --git a/Diner/Diner/Program.cs b/Diner/Diner/Program.cs index aeca90d..b98f417 100644 --- a/Diner/Diner/Program.cs +++ b/Diner/Diner/Program.cs @@ -1,10 +1,11 @@ using DinerContracts.BusinessLogicsContracts; using DinerContracts.StoragesContracts; -using DinerFileImplement.Implements; +using DinerDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using DinerBusinessLogic.BusinessLogics; + using DinerView; namespace Diner diff --git a/Diner/DinerDatabaseImplements/DinerDatabase.cs b/Diner/DinerDatabaseImplements/DinerDatabase.cs new file mode 100644 index 0000000..a4d86c1 --- /dev/null +++ b/Diner/DinerDatabaseImplements/DinerDatabase.cs @@ -0,0 +1,24 @@ +using DinerDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; + +namespace DinerDatabaseImplement +{ + public class DinerDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + optionsBuilder.UseSqlServer(@"Data Source=SHADOWIK\SHADOWIK;Initial Catalog=PizzeriaHardDatabase;Integrated Security=True;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { get; set; } + public virtual DbSet Snacks { get; set; } + public virtual DbSet SnackComponents { get; set; } + public virtual DbSet Orders { get; set; } + public virtual DbSet Shops { get; set; } + public virtual DbSet ShopSnacks { get; set; } + } +} \ No newline at end of file diff --git a/Diner/DinerDatabaseImplements/DinerDatabaseImplement.csproj b/Diner/DinerDatabaseImplements/DinerDatabaseImplement.csproj new file mode 100644 index 0000000..031588f --- /dev/null +++ b/Diner/DinerDatabaseImplements/DinerDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Diner/DinerDatabaseImplements/Implements/ComponentStorage.cs b/Diner/DinerDatabaseImplements/Implements/ComponentStorage.cs new file mode 100644 index 0000000..118b1cb --- /dev/null +++ b/Diner/DinerDatabaseImplements/Implements/ComponentStorage.cs @@ -0,0 +1,79 @@ +using DinerContracts.BindingModels; +using DinerContracts.SearchModels; +using DinerContracts.StoragesContracts; +using DinerContracts.ViewModels; +using DinerDatabaseImplement.Models; + +namespace DinerDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new DinerDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new DinerDatabase(); + return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new DinerDatabase(); + return context.Components.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new DinerDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new DinerDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new DinerDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Diner/DinerDatabaseImplements/Implements/OrderStorage.cs b/Diner/DinerDatabaseImplements/Implements/OrderStorage.cs new file mode 100644 index 0000000..85e5b16 --- /dev/null +++ b/Diner/DinerDatabaseImplements/Implements/OrderStorage.cs @@ -0,0 +1,79 @@ +using DinerContracts.BindingModels; +using DinerContracts.SearchModels; +using DinerContracts.StoragesContracts; +using DinerContracts.ViewModels; +using DinerDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace DinerDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new DinerDatabase(); + return context.Orders.Include(x => x.Snack).Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new DinerDatabase(); + return context.Orders.Include(x => x.Snack).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new DinerDatabase(); + return context.Orders.Include(x => x.Snack).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new DinerDatabase(); + if (model == null) + return null; + var newOrder = Order.Create(context, model); + if (newOrder == null) + { + return null; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new DinerDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new DinerDatabase(); + var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (order != null) + { + context.Orders.Remove(order); + context.SaveChanges(); + return order.GetViewModel; + } + return null; + } + } +} diff --git a/Diner/DinerDatabaseImplements/Implements/ShopStorage.cs b/Diner/DinerDatabaseImplements/Implements/ShopStorage.cs new file mode 100644 index 0000000..5461987 --- /dev/null +++ b/Diner/DinerDatabaseImplements/Implements/ShopStorage.cs @@ -0,0 +1,191 @@ +using Microsoft.EntityFrameworkCore; +using DinerContracts.BindingModels; +using DinerContracts.SearchModels; +using DinerContracts.StoragesContracts; +using DinerContracts.ViewModels; +using DinerDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DinerDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new DinerDatabase(); + return context.Shops.Include(x => x.Snacks).ThenInclude(x => x.Snack).ToList(). + Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new DinerDatabase(); + return context.Shops.Include(x => x.Snacks).ThenInclude(x => x.Snack).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 DinerDatabase(); + return context.Shops.Include(x => x.Snacks).ThenInclude(x => x.Snack) + .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 DinerDatabase(); + 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 DinerDatabase(); + 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.UpdateSnacks(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new DinerDatabase(); + var shop = context.Shops.Include(x => x.Snacks).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 DinerDatabase(); + var transaction = context.Database.BeginTransaction(); + var Shops = context.Shops.Include(x => x.Snacks).ThenInclude(x => x.Snack).ToList(). + Where(x => x.SnackMaxCount > x.ShopSnacks.Select(x => x.Value.Item2).Sum()).ToList(); + if (model == null) + { + return false; + } + try + { + foreach (Shop shop in Shops) + { + int difference = shop.SnackMaxCount - shop.ShopSnacks.Select(x => x.Value.Item2).Sum(); + int refill = Math.Min(difference, model.Count); + model.Count -= refill; + if (shop.ShopSnacks.ContainsKey(model.SnackId)) + { + var datePair = shop.ShopSnacks[model.SnackId]; + datePair.Item2 += refill; + shop.ShopSnacks[model.SnackId] = datePair; + } + else + { + var snack = context.Snacks.First(x => x.Id == model.SnackId); + shop.ShopSnacks.Add(model.SnackId, (snack, refill)); + } + shop.SnacksDictionatyUpdate(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 DinerDatabase(); + var transaction = context.Database.BeginTransaction(); + try + { + var shops = context.Shops.Include(x => x.Snacks).ThenInclude(x => x.Snack).ToList(). + Where(x => x.ShopSnacks.ContainsKey(model.SnackId.Value)).OrderByDescending(x => x.ShopSnacks[model.SnackId.Value].Item2).ToList(); + + foreach (var shop in shops) + { + int residue = model.Count.Value - shop.ShopSnacks[model.SnackId.Value].Item2; + if (residue > 0) + { + shop.ShopSnacks.Remove(model.SnackId.Value); + shop.SnacksDictionatyUpdate(context); + context.SaveChanges(); + model.Count = residue; + + } + else + { + if (residue == 0) + shop.ShopSnacks.Remove(model.SnackId.Value); + else + { + var dataPair = shop.ShopSnacks[model.SnackId.Value]; + dataPair.Item2 = -residue; + shop.ShopSnacks[model.SnackId.Value] = dataPair; + } + + shop.SnacksDictionatyUpdate(context); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + + + } +} diff --git a/Diner/DinerDatabaseImplements/Implements/SnackStorage.cs b/Diner/DinerDatabaseImplements/Implements/SnackStorage.cs new file mode 100644 index 0000000..e96bebb --- /dev/null +++ b/Diner/DinerDatabaseImplements/Implements/SnackStorage.cs @@ -0,0 +1,94 @@ +using DinerContracts.BindingModels; +using DinerContracts.SearchModels; +using DinerContracts.StoragesContracts; +using DinerContracts.ViewModels; +using DinerDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace DinerDatabaseImplement.Implements +{ + public class SnackStorage : ISnackStorage + { + public List GetFullList() + { + using var context = new DinerDatabase(); + return context.Snacks.Include(x => x.Components).ThenInclude(x => x.Component).ToList() + .Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(SnackSearchModel model) + { + if (string.IsNullOrEmpty(model.SnackName)) + { + return new(); + } + using var context = new DinerDatabase(); + return context.Snacks.Include(x => x.Components).ThenInclude(x => x.Component) + .Where(x => x.SnackName.Contains(model.SnackName)).ToList().Select(x => x.GetViewModel).ToList(); + } + + public SnackViewModel? GetElement(SnackSearchModel model) + { + if (string.IsNullOrEmpty(model.SnackName) && !model.Id.HasValue) + { + return null; + } + using var context = new DinerDatabase(); + return context.Snacks.Include(x => x.Components).ThenInclude(x => x.Component) + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.SnackName) && x.SnackName == model.SnackName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public SnackViewModel? Insert(SnackBindingModel model) + { + using var context = new DinerDatabase(); + var newSnack = Snack.Create(context, model); + if (newSnack == null) + { + return null; + } + context.Snacks.Add(newSnack); + context.SaveChanges(); + return newSnack.GetViewModel; + } + + public SnackViewModel? Update(SnackBindingModel model) + { + using var context = new DinerDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var Snack = context.Snacks.FirstOrDefault(rec => rec.Id == model.Id); + if (Snack == null) + { + return null; + } + Snack.Update(model); + context.SaveChanges(); + Snack.UpdateComponents(context, model); + transaction.Commit(); + return Snack.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public SnackViewModel? Delete(SnackBindingModel model) + { + using var context = new DinerDatabase(); + var element = context.Snacks.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Snacks.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Diner/DinerDatabaseImplements/Models/Component.cs b/Diner/DinerDatabaseImplements/Models/Component.cs new file mode 100644 index 0000000..cc82188 --- /dev/null +++ b/Diner/DinerDatabaseImplements/Models/Component.cs @@ -0,0 +1,58 @@ +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using DinerDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace DinerDatabaseImplement.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 SnackComponents { 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/Diner/DinerDatabaseImplements/Models/Order.cs b/Diner/DinerDatabaseImplements/Models/Order.cs new file mode 100644 index 0000000..c5da618 --- /dev/null +++ b/Diner/DinerDatabaseImplements/Models/Order.cs @@ -0,0 +1,73 @@ +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using DinerDataModels.Enum; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DinerDatabaseImplement.Models +{ + public class Order + { + public int Id { get; private set; } + + [Required] + public int SnackId { get; private set; } + + public virtual Snack Snack { get; set; } = new(); + + [Required] + public int Count { get; private set; } + + [Required] + public double Sum { get; private set; } + + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public static Order Create(DinerDatabase context, OrderBindingModel model) + { + return new Order() + { + Id = model.Id, + SnackId = model.SnackId, + Snack = context.Snacks.First(x => x.Id == model.SnackId), + 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; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + SnackId = SnackId, + SnackName = Snack.SnackName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +} diff --git a/Diner/DinerDatabaseImplements/Models/Shop.cs b/Diner/DinerDatabaseImplements/Models/Shop.cs new file mode 100644 index 0000000..866c5ae --- /dev/null +++ b/Diner/DinerDatabaseImplements/Models/Shop.cs @@ -0,0 +1,120 @@ +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using DinerDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DinerDatabaseImplement.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 SnackMaxCount { get; set; } + + private Dictionary? _shopSnacks = null; + public Dictionary ShopSnacks + { + get + { + if (_shopSnacks == null) { + if (_shopSnacks == null) + { + _shopSnacks = Snacks + .ToDictionary(recSP => recSP.SnackId, recSP => (recSP.Snack as ISnackModel, recSP.Count)); + } + return _shopSnacks; + } + return _shopSnacks; + } + } + + [ForeignKey("ShopId")] + public List Snacks { get; set; } = new(); + + public static Shop Create(DinerDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Adress = model.Adress, + OpeningDate = model.OpeningDate, + Snacks = model.ShopSnacks.Select(x => new ShopSnacks + { + Snack = context.Snacks.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + SnackMaxCount = model.SnackMaxCount + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + SnackMaxCount = model.SnackMaxCount; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Adress = Adress, + OpeningDate = OpeningDate, + ShopSnacks = ShopSnacks, + SnackMaxCount = SnackMaxCount + }; + + + + public void UpdateSnacks(DinerDatabase context, ShopBindingModel model) + { + var ShopSnacks = context.ShopSnacks.Where(rec => rec.ShopId == model.Id).ToList(); + if (ShopSnacks != null && this.ShopSnacks.Count > 0) + { + context.ShopSnacks.RemoveRange(ShopSnacks.Where(rec => !model.ShopSnacks.ContainsKey(rec.SnackId))); + context.SaveChanges(); + ShopSnacks = context.ShopSnacks.Where(rec => rec.ShopId == model.Id).ToList(); + foreach (var updateSnack in ShopSnacks) + { + updateSnack.Count = model.ShopSnacks[updateSnack.SnackId].Item2; + model.ShopSnacks.Remove(updateSnack.SnackId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var ar in model.ShopSnacks) + { + context.ShopSnacks.Add(new ShopSnacks + { + Shop = shop, + Snack = context.Snacks.First(x => x.Id == ar.Key), + Count = ar.Value.Item2 + }); + context.SaveChanges(); + } + _shopSnacks = null; + } + + public void SnacksDictionatyUpdate(DinerDatabase context) + { + UpdateSnacks(context, new ShopBindingModel + { + Id = Id, + ShopSnacks = ShopSnacks, + }); + } + + } +} diff --git a/Diner/DinerDatabaseImplements/Models/ShopSnacks.cs b/Diner/DinerDatabaseImplements/Models/ShopSnacks.cs new file mode 100644 index 0000000..a4f35e4 --- /dev/null +++ b/Diner/DinerDatabaseImplements/Models/ShopSnacks.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DinerDatabaseImplement.Models +{ + public class ShopSnacks + { + public int Id { get; set; } + + [Required] + public int SnackId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Snack Snack { get; set; } = new(); + } +} diff --git a/Diner/DinerDatabaseImplements/Models/Snack.cs b/Diner/DinerDatabaseImplements/Models/Snack.cs new file mode 100644 index 0000000..f96b169 --- /dev/null +++ b/Diner/DinerDatabaseImplements/Models/Snack.cs @@ -0,0 +1,91 @@ +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using DinerDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace DinerDatabaseImplement.Models +{ + public class Snack : ISnackModel + { + public int Id { get; set; } + [Required] + public string SnackName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + + private Dictionary? _snackComponents = null; + [NotMapped] + public Dictionary SnackComponents + { + get + { + if (_snackComponents == null) + { + _snackComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _snackComponents; + } + } + [ForeignKey("SnackId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("SnackId")] + public virtual List Orders { get; set; } = new(); + + public static Snack + Create(DinerDatabase context, SnackBindingModel model) + { + return new Snack() + { + Id = model.Id, + SnackName = model.SnackName, + Price = model.Price, + Components = model.SnackComponents.Select(x => new SnackComponent { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(SnackBindingModel model) + { + SnackName = model.SnackName; + Price = model.Price; + } + public SnackViewModel GetViewModel => new() + { + Id = Id, + SnackName = SnackName, + Price = Price, + SnackComponents = SnackComponents + }; + public void UpdateComponents(DinerDatabase context, SnackBindingModel model) + { + var snackComponents = context.SnackComponents.Where(rec => rec.SnackId == model.Id).ToList(); + + if (snackComponents != null && snackComponents.Count > 0) { + context.SnackComponents.RemoveRange(snackComponents.Where(rec => !model.SnackComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + foreach (var updateComponent in snackComponents) + { + updateComponent.Count = model.SnackComponents[updateComponent.ComponentId].Item2; + model.SnackComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + + var snack = context.Snacks.First(x => x.Id == Id); + foreach (var pc in model.SnackComponents) { + context.SnackComponents.Add(new SnackComponent + { + Snack = snack, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _snackComponents = null; + } + + } +} diff --git a/Diner/DinerDatabaseImplements/Models/SnackComponent.cs b/Diner/DinerDatabaseImplements/Models/SnackComponent.cs new file mode 100644 index 0000000..63e1a70 --- /dev/null +++ b/Diner/DinerDatabaseImplements/Models/SnackComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations; + +namespace DinerDatabaseImplement.Models +{ + public class SnackComponent + { + public int Id { get; set; } + [Required] + public int SnackId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Snack Snack { get; set; } = new(); + } +} -- 2.25.1 From 9648755b221413c1c83ccce5acb5b4c040580c1c Mon Sep 17 00:00:00 2001 From: goblinrf Date: Mon, 6 May 2024 21:12:06 +0400 Subject: [PATCH 2/2] Done --- .../SearchModels/SupplySearchModel.cs | 1 + Diner/Diner/DinerView.csproj | 4 + Diner/Diner/FormCreateSupply.Designer.cs | 20 +- Diner/Diner/FormSellSnack.Designer.cs | 2 +- Diner/Diner/FormSellSnack.cs | 2 +- .../DinerDatabaseImplements/DinerDatabase.cs | 2 +- .../20240506162016_InitialCreate.Designer.cs | 248 ++++++++++++++++++ .../20240506162016_InitialCreate.cs | 184 +++++++++++++ .../Migrations/DinerDatabaseModelSnapshot.cs | 245 +++++++++++++++++ 9 files changed, 695 insertions(+), 13 deletions(-) create mode 100644 Diner/DinerDatabaseImplements/Migrations/20240506162016_InitialCreate.Designer.cs create mode 100644 Diner/DinerDatabaseImplements/Migrations/20240506162016_InitialCreate.cs create mode 100644 Diner/DinerDatabaseImplements/Migrations/DinerDatabaseModelSnapshot.cs diff --git a/AbstractShopContracts/SearchModels/SupplySearchModel.cs b/AbstractShopContracts/SearchModels/SupplySearchModel.cs index 67d40b6..e7e6863 100644 --- a/AbstractShopContracts/SearchModels/SupplySearchModel.cs +++ b/AbstractShopContracts/SearchModels/SupplySearchModel.cs @@ -8,6 +8,7 @@ namespace DinerContracts.SearchModels { public class SupplySearchModel { + public int? SnackId { get; set; } public int? Count { get; set; } } diff --git a/Diner/Diner/DinerView.csproj b/Diner/Diner/DinerView.csproj index f8854ee..7e6084d 100644 --- a/Diner/Diner/DinerView.csproj +++ b/Diner/Diner/DinerView.csproj @@ -13,6 +13,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Diner/Diner/FormCreateSupply.Designer.cs b/Diner/Diner/FormCreateSupply.Designer.cs index fde6034..f7bf8e5 100644 --- a/Diner/Diner/FormCreateSupply.Designer.cs +++ b/Diner/Diner/FormCreateSupply.Designer.cs @@ -30,7 +30,7 @@ { comboBoxShop = new ComboBox(); labelShop = new Label(); - labelPizza = new Label(); + labelSnack = new Label(); comboBoxSnack = new ComboBox(); labelCount = new Label(); textBoxCount = new TextBox(); @@ -55,14 +55,14 @@ labelShop.TabIndex = 1; labelShop.Text = "Магазин: "; // - // labelPizza + // labelSnack // - labelPizza.AutoSize = true; - labelPizza.Location = new Point(12, 49); - labelPizza.Name = "labelPizza"; - labelPizza.Size = new Size(75, 20); - labelPizza.TabIndex = 2; - labelPizza.Text = "Изделие: "; + labelSnack.AutoSize = true; + labelSnack.Location = new Point(12, 49); + labelSnack.Name = "labelSnack"; + labelSnack.Size = new Size(75, 20); + labelSnack.TabIndex = 2; + labelSnack.Text = "Изделие: "; // // comboBoxSnack // @@ -118,7 +118,7 @@ Controls.Add(textBoxCount); Controls.Add(labelCount); Controls.Add(comboBoxSnack); - Controls.Add(labelPizza); + Controls.Add(labelSnack); Controls.Add(labelShop); Controls.Add(comboBoxShop); Name = "FormCreateSupply"; @@ -132,7 +132,7 @@ private ComboBox comboBoxShop; private Label labelShop; - private Label labelPizza; + private Label labelSnack; private ComboBox comboBoxSnack; private Label labelCount; private TextBox textBoxCount; diff --git a/Diner/Diner/FormSellSnack.Designer.cs b/Diner/Diner/FormSellSnack.Designer.cs index 8145f17..e08d87a 100644 --- a/Diner/Diner/FormSellSnack.Designer.cs +++ b/Diner/Diner/FormSellSnack.Designer.cs @@ -102,7 +102,7 @@ Controls.Add(labelSnack); Name = "FormSellSnack"; Text = "Продажа закуски"; - Load += FormSellingPizza_Load; + Load += FormSellingSnack_Load; ResumeLayout(false); PerformLayout(); } diff --git a/Diner/Diner/FormSellSnack.cs b/Diner/Diner/FormSellSnack.cs index d6f96a4..b906fa7 100644 --- a/Diner/Diner/FormSellSnack.cs +++ b/Diner/Diner/FormSellSnack.cs @@ -28,7 +28,7 @@ namespace DinerView _logicP = logicP; _logicS = logicS; } - private void FormSellingPizza_Load(object sender, EventArgs e) + private void FormSellingSnack_Load(object sender, EventArgs e) { _snackList = _logicP.ReadList(null); if (_snackList != null) diff --git a/Diner/DinerDatabaseImplements/DinerDatabase.cs b/Diner/DinerDatabaseImplements/DinerDatabase.cs index a4d86c1..3a3232b 100644 --- a/Diner/DinerDatabaseImplements/DinerDatabase.cs +++ b/Diner/DinerDatabaseImplements/DinerDatabase.cs @@ -10,7 +10,7 @@ namespace DinerDatabaseImplement { if (!optionsBuilder.IsConfigured) { - optionsBuilder.UseSqlServer(@"Data Source=SHADOWIK\SHADOWIK;Initial Catalog=PizzeriaHardDatabase;Integrated Security=True;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS01;Initial Catalog=DinerDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/Diner/DinerDatabaseImplements/Migrations/20240506162016_InitialCreate.Designer.cs b/Diner/DinerDatabaseImplements/Migrations/20240506162016_InitialCreate.Designer.cs new file mode 100644 index 0000000..b1efc68 --- /dev/null +++ b/Diner/DinerDatabaseImplements/Migrations/20240506162016_InitialCreate.Designer.cs @@ -0,0 +1,248 @@ +// +using System; +using DinerDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DinerDatabaseImplement.Migrations +{ + [DbContext(typeof(DinerDatabase))] + [Migration("20240506162016_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.14") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DinerDatabaseImplement.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("DinerDatabaseImplement.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("SnackId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("SnackId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.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("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SnackMaxCount") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.ShopSnacks", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.Property("SnackId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ShopId"); + + b.HasIndex("SnackId"); + + b.ToTable("ShopSnacks"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.Snack", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("SnackName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Snacks"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.SnackComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("SnackId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("SnackId"); + + b.ToTable("SnackComponents"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.Order", b => + { + b.HasOne("DinerDatabaseImplement.Models.Snack", "Snack") + .WithMany("Orders") + .HasForeignKey("SnackId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Snack"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.ShopSnacks", b => + { + b.HasOne("DinerDatabaseImplement.Models.Shop", "Shop") + .WithMany("Snacks") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DinerDatabaseImplement.Models.Snack", "Snack") + .WithMany() + .HasForeignKey("SnackId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Shop"); + + b.Navigation("Snack"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.SnackComponent", b => + { + b.HasOne("DinerDatabaseImplement.Models.Component", "Component") + .WithMany("SnackComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DinerDatabaseImplement.Models.Snack", "Snack") + .WithMany("Components") + .HasForeignKey("SnackId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Snack"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.Component", b => + { + b.Navigation("SnackComponents"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.Shop", b => + { + b.Navigation("Snacks"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.Snack", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Diner/DinerDatabaseImplements/Migrations/20240506162016_InitialCreate.cs b/Diner/DinerDatabaseImplements/Migrations/20240506162016_InitialCreate.cs new file mode 100644 index 0000000..0cf23df --- /dev/null +++ b/Diner/DinerDatabaseImplements/Migrations/20240506162016_InitialCreate.cs @@ -0,0 +1,184 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DinerDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + 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), + SnackMaxCount = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Snacks", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SnackName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Snacks", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SnackId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Snacks_SnackId", + column: x => x.SnackId, + principalTable: "Snacks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "ShopSnacks", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SnackId = 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_ShopSnacks", x => x.Id); + table.ForeignKey( + name: "FK_ShopSnacks_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopSnacks_Snacks_SnackId", + column: x => x.SnackId, + principalTable: "Snacks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "SnackComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SnackId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_SnackComponents", x => x.Id); + table.ForeignKey( + name: "FK_SnackComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_SnackComponents_Snacks_SnackId", + column: x => x.SnackId, + principalTable: "Snacks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_SnackId", + table: "Orders", + column: "SnackId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopSnacks_ShopId", + table: "ShopSnacks", + column: "ShopId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopSnacks_SnackId", + table: "ShopSnacks", + column: "SnackId"); + + migrationBuilder.CreateIndex( + name: "IX_SnackComponents_ComponentId", + table: "SnackComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_SnackComponents_SnackId", + table: "SnackComponents", + column: "SnackId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "ShopSnacks"); + + migrationBuilder.DropTable( + name: "SnackComponents"); + + migrationBuilder.DropTable( + name: "Shops"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Snacks"); + } + } +} diff --git a/Diner/DinerDatabaseImplements/Migrations/DinerDatabaseModelSnapshot.cs b/Diner/DinerDatabaseImplements/Migrations/DinerDatabaseModelSnapshot.cs new file mode 100644 index 0000000..a4e5337 --- /dev/null +++ b/Diner/DinerDatabaseImplements/Migrations/DinerDatabaseModelSnapshot.cs @@ -0,0 +1,245 @@ +// +using System; +using DinerDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DinerDatabaseImplement.Migrations +{ + [DbContext(typeof(DinerDatabase))] + partial class DinerDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.14") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DinerDatabaseImplement.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("DinerDatabaseImplement.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("SnackId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("SnackId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.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("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SnackMaxCount") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.ShopSnacks", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.Property("SnackId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ShopId"); + + b.HasIndex("SnackId"); + + b.ToTable("ShopSnacks"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.Snack", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("SnackName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Snacks"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.SnackComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("SnackId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("SnackId"); + + b.ToTable("SnackComponents"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.Order", b => + { + b.HasOne("DinerDatabaseImplement.Models.Snack", "Snack") + .WithMany("Orders") + .HasForeignKey("SnackId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Snack"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.ShopSnacks", b => + { + b.HasOne("DinerDatabaseImplement.Models.Shop", "Shop") + .WithMany("Snacks") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DinerDatabaseImplement.Models.Snack", "Snack") + .WithMany() + .HasForeignKey("SnackId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Shop"); + + b.Navigation("Snack"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.SnackComponent", b => + { + b.HasOne("DinerDatabaseImplement.Models.Component", "Component") + .WithMany("SnackComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DinerDatabaseImplement.Models.Snack", "Snack") + .WithMany("Components") + .HasForeignKey("SnackId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Snack"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.Component", b => + { + b.Navigation("SnackComponents"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.Shop", b => + { + b.Navigation("Snacks"); + }); + + modelBuilder.Entity("DinerDatabaseImplement.Models.Snack", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} -- 2.25.1