From 16e907326cc3d4ec799b265af785201b3dc153d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Mon, 8 May 2023 13:51:25 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F=D1=82=D1=8C=20=D1=84=D0=BE=D1=80?= =?UTF-8?q?=D0=BC=D1=8B=20=D0=BC=D0=BD=D0=BE=D0=B3=D0=B8=D0=B5-=D0=BA?= =?UTF-8?q?=D0=BE-=D0=BC=D0=BD=D0=BE=D0=B3=D0=B8=D0=BC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Restaurant/Restaurant.sln | 14 +- .../BusinessLogics/ClientLogic.cs | 112 ++++++ .../BusinessLogics/OrderLogic.cs | 1 + .../BindingModels/ComponentBindingModel.cs | 2 +- .../ViewModels/ComponentViewModel.cs | 2 +- .../Models/IComponentModel.cs | 2 +- .../Implements/ClientStorage.cs | 107 ++++++ .../Implements/ComponentStorage.cs | 113 +++++++ .../Implements/OrderStorage.cs | 125 +++++++ .../Implements/ProductStorage.cs | 121 +++++++ .../Implements/ProviderStorage.cs | 97 ++++++ .../20230506075636_migr1.Designer.cs | 319 ++++++++++++++++++ .../Migrations/20230506075636_migr1.cs | 241 +++++++++++++ .../RestaurantDatabaseModelSnapshot.cs | 316 +++++++++++++++++ .../Models/Client.cs | 60 ++++ .../Models/Component.cs | 116 +++++++ .../Models/ComponentProvider.cs | 28 ++ .../Models/Order.cs | 110 ++++++ .../Models/OrderProduct.cs | 25 ++ .../Models/Product.cs | 113 +++++++ .../Models/ProductComponent.cs | 25 ++ .../Models/Provider.cs | 71 ++++ .../RestaurantDatabase.cs | 36 ++ .../RestaurantDatabaseImplement.csproj | 27 ++ Restaurant/RestaurantView/Form1.cs | 10 - .../RestaurantView/FormClient.Designer.cs | 141 ++++++++ Restaurant/RestaurantView/FormClient.cs | 89 +++++ Restaurant/RestaurantView/FormClient.resx | 60 ++++ .../RestaurantView/FormClients.Designer.cs | 114 +++++++ Restaurant/RestaurantView/FormClients.cs | 106 ++++++ Restaurant/RestaurantView/FormClients.resx | 60 ++++ .../RestaurantView/FormComponent.Designer.cs | 246 ++++++++++++++ Restaurant/RestaurantView/FormComponent.cs | 114 +++++++ Restaurant/RestaurantView/FormComponent.resx | 78 +++++ ...r.cs => FormComponentProvider.Designer.cs} | 12 +- .../RestaurantView/FormComponentProvider.cs | 92 +++++ ...{Form1.resx => FormComponentProvider.resx} | 0 .../RestaurantView/FormComponents.Designer.cs | 114 +++++++ Restaurant/RestaurantView/FormComponents.cs | 108 ++++++ Restaurant/RestaurantView/FormComponents.resx | 60 ++++ .../RestaurantView/FormMain.Designer.cs | 121 +++++++ Restaurant/RestaurantView/FormMain.cs | 73 ++++ Restaurant/RestaurantView/FormMain.resx | 63 ++++ .../RestaurantView/FormProvider.Designer.cs | 119 +++++++ Restaurant/RestaurantView/FormProvider.cs | 88 +++++ Restaurant/RestaurantView/FormProvider.resx | 60 ++++ .../RestaurantView/FormProviders.Designer.cs | 114 +++++++ Restaurant/RestaurantView/FormProviders.cs | 106 ++++++ Restaurant/RestaurantView/FormProviders.resx | 60 ++++ Restaurant/RestaurantView/Program.cs | 37 +- .../RestaurantView/RestaurantView.csproj | 14 + 51 files changed, 4418 insertions(+), 24 deletions(-) create mode 100644 Restaurant/RestaurantBusinessLogic/BusinessLogics/ClientLogic.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Implements/ClientStorage.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Implements/OrderStorage.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Implements/ProductStorage.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Implements/ProviderStorage.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Migrations/20230506075636_migr1.Designer.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Migrations/20230506075636_migr1.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Migrations/RestaurantDatabaseModelSnapshot.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Models/Client.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Models/Component.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Models/ComponentProvider.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Models/Order.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Models/OrderProduct.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Models/Product.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Models/ProductComponent.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/Models/Provider.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/RestaurantDatabase.cs create mode 100644 Restaurant/RestaurantDatabaseImplement/RestaurantDatabaseImplement.csproj delete mode 100644 Restaurant/RestaurantView/Form1.cs create mode 100644 Restaurant/RestaurantView/FormClient.Designer.cs create mode 100644 Restaurant/RestaurantView/FormClient.cs create mode 100644 Restaurant/RestaurantView/FormClient.resx create mode 100644 Restaurant/RestaurantView/FormClients.Designer.cs create mode 100644 Restaurant/RestaurantView/FormClients.cs create mode 100644 Restaurant/RestaurantView/FormClients.resx create mode 100644 Restaurant/RestaurantView/FormComponent.Designer.cs create mode 100644 Restaurant/RestaurantView/FormComponent.cs create mode 100644 Restaurant/RestaurantView/FormComponent.resx rename Restaurant/RestaurantView/{Form1.Designer.cs => FormComponentProvider.Designer.cs} (75%) create mode 100644 Restaurant/RestaurantView/FormComponentProvider.cs rename Restaurant/RestaurantView/{Form1.resx => FormComponentProvider.resx} (100%) create mode 100644 Restaurant/RestaurantView/FormComponents.Designer.cs create mode 100644 Restaurant/RestaurantView/FormComponents.cs create mode 100644 Restaurant/RestaurantView/FormComponents.resx create mode 100644 Restaurant/RestaurantView/FormMain.Designer.cs create mode 100644 Restaurant/RestaurantView/FormMain.cs create mode 100644 Restaurant/RestaurantView/FormMain.resx create mode 100644 Restaurant/RestaurantView/FormProvider.Designer.cs create mode 100644 Restaurant/RestaurantView/FormProvider.cs create mode 100644 Restaurant/RestaurantView/FormProvider.resx create mode 100644 Restaurant/RestaurantView/FormProviders.Designer.cs create mode 100644 Restaurant/RestaurantView/FormProviders.cs create mode 100644 Restaurant/RestaurantView/FormProviders.resx diff --git a/Restaurant/Restaurant.sln b/Restaurant/Restaurant.sln index e4e41a9..945dc0e 100644 --- a/Restaurant/Restaurant.sln +++ b/Restaurant/Restaurant.sln @@ -3,13 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.3.32819.101 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestaurantView", "RestaurantView\RestaurantView.csproj", "{6784A2BC-8BA1-48A3-992B-DC0EA755469B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RestaurantView", "RestaurantView\RestaurantView.csproj", "{6784A2BC-8BA1-48A3-992B-DC0EA755469B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestaurantDataModels", "RestaurantDataModels\RestaurantDataModels.csproj", "{A7888AAA-C1CD-4580-A204-E6720AF49931}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RestaurantDataModels", "RestaurantDataModels\RestaurantDataModels.csproj", "{A7888AAA-C1CD-4580-A204-E6720AF49931}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestaurantContracts", "RestaurantContracts\RestaurantContracts.csproj", "{288FB1A3-E6D0-42B2-BA20-463E03F922B4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RestaurantContracts", "RestaurantContracts\RestaurantContracts.csproj", "{288FB1A3-E6D0-42B2-BA20-463E03F922B4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestaurantBusinessLogic", "RestaurantBusinessLogic\RestaurantBusinessLogic.csproj", "{F3A63C5F-2BAE-4E69-895C-62FC1A7C556D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RestaurantBusinessLogic", "RestaurantBusinessLogic\RestaurantBusinessLogic.csproj", "{F3A63C5F-2BAE-4E69-895C-62FC1A7C556D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestaurantDatabaseImplement", "RestaurantDatabaseImplement\RestaurantDatabaseImplement.csproj", "{DFC11A85-B4CA-4315-8CF4-F8B0F53FB7F2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,6 +35,10 @@ Global {F3A63C5F-2BAE-4E69-895C-62FC1A7C556D}.Debug|Any CPU.Build.0 = Debug|Any CPU {F3A63C5F-2BAE-4E69-895C-62FC1A7C556D}.Release|Any CPU.ActiveCfg = Release|Any CPU {F3A63C5F-2BAE-4E69-895C-62FC1A7C556D}.Release|Any CPU.Build.0 = Release|Any CPU + {DFC11A85-B4CA-4315-8CF4-F8B0F53FB7F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DFC11A85-B4CA-4315-8CF4-F8B0F53FB7F2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DFC11A85-B4CA-4315-8CF4-F8B0F53FB7F2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DFC11A85-B4CA-4315-8CF4-F8B0F53FB7F2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Restaurant/RestaurantBusinessLogic/BusinessLogics/ClientLogic.cs b/Restaurant/RestaurantBusinessLogic/BusinessLogics/ClientLogic.cs new file mode 100644 index 0000000..3590b71 --- /dev/null +++ b/Restaurant/RestaurantBusinessLogic/BusinessLogics/ClientLogic.cs @@ -0,0 +1,112 @@ +using RestaurantContracts.BindingModels; +using RestaurantContracts.BusinessLogicsContracts; +using RestaurantContracts.SearchModels; +using RestaurantContracts.StoragesContracts; +using RestaurantContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantBusinessLogic.BusinessLogics +{ + public class ClientLogic : IClientLogic + { + + private readonly IClientStorage _clientStorage; + + public ClientLogic(IClientStorage clientStorage) + { + _clientStorage = clientStorage; + } + + public bool Create(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Insert(model) == null) + { + return false; + } + return true; + } + + public bool Delete(ClientBindingModel model) + { + CheckModel(model, false); + if (_clientStorage.Delete(model) == null) + { + return false; + } + return true; + } + + public ClientViewModel? ReadElement(ClientSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var element = _clientStorage.GetElement(model); + if (element == null) + { + return null; + } + return element; + } + + public List? ReadList(ClientSearchModel? model) + { + var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model); + if (list == null) + { + return null; + } + return list; + } + + public bool Update(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Update(model) == null) + { + return false; + } + return true; + } + + private void CheckModel(ClientBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.FirstName)) + { + throw new ArgumentNullException("Нет имени клиента", nameof(model.FirstName)); + } + if (string.IsNullOrEmpty(model.LastName)) + { + throw new ArgumentNullException("Нет фамилии клиента", nameof(model.LastName)); + } + if (string.IsNullOrEmpty(model.Number)) + { + throw new ArgumentNullException("Нет номера клиента", nameof(model.Number)); + } + var element = _clientStorage.GetElement(new ClientSearchModel + { + FirstName = model.FirstName, + LastName = model.LastName, + Number = model.Number, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Клиент с такими параметрами уже есть"); + } + } + } +} diff --git a/Restaurant/RestaurantBusinessLogic/BusinessLogics/OrderLogic.cs b/Restaurant/RestaurantBusinessLogic/BusinessLogics/OrderLogic.cs index 15a0292..1596756 100644 --- a/Restaurant/RestaurantBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/Restaurant/RestaurantBusinessLogic/BusinessLogics/OrderLogic.cs @@ -94,6 +94,7 @@ namespace RestaurantBusinessLogic.BusinessLogics } var element = _orderStorage.GetElement(new OrderSearchModel { + ClientId = model.ClientId, Date = model.Date, }); if (element != null && element.Id != model.Id) diff --git a/Restaurant/RestaurantContracts/BindingModels/ComponentBindingModel.cs b/Restaurant/RestaurantContracts/BindingModels/ComponentBindingModel.cs index 5972da5..15f17e8 100644 --- a/Restaurant/RestaurantContracts/BindingModels/ComponentBindingModel.cs +++ b/Restaurant/RestaurantContracts/BindingModels/ComponentBindingModel.cs @@ -15,7 +15,7 @@ namespace RestaurantContracts.BindingModels public int Count { get; set; } - public Dictionary ComponentProviders { get; set; } = new(); + public Dictionary ComponentProviders { get; set; } = new(); public int Id { get; set; } } diff --git a/Restaurant/RestaurantContracts/ViewModels/ComponentViewModel.cs b/Restaurant/RestaurantContracts/ViewModels/ComponentViewModel.cs index 28ffc0f..f7b11b8 100644 --- a/Restaurant/RestaurantContracts/ViewModels/ComponentViewModel.cs +++ b/Restaurant/RestaurantContracts/ViewModels/ComponentViewModel.cs @@ -21,6 +21,6 @@ namespace RestaurantContracts.ViewModels [DisplayName("Количество")] public int Count { get; set; } - public Dictionary ComponentProviders { get; set; } = new(); + public Dictionary ComponentProviders { get; set; } = new(); } } diff --git a/Restaurant/RestaurantDataModels/Models/IComponentModel.cs b/Restaurant/RestaurantDataModels/Models/IComponentModel.cs index 645383f..196dc30 100644 --- a/Restaurant/RestaurantDataModels/Models/IComponentModel.cs +++ b/Restaurant/RestaurantDataModels/Models/IComponentModel.cs @@ -14,6 +14,6 @@ namespace RestaurantDataModels.Models int Count { get; } - Dictionary ComponentProviders { get; } + Dictionary ComponentProviders { get; } } } diff --git a/Restaurant/RestaurantDatabaseImplement/Implements/ClientStorage.cs b/Restaurant/RestaurantDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..6cf9653 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,107 @@ +using Microsoft.EntityFrameworkCore; +using RestaurantContracts.BindingModels; +using RestaurantContracts.SearchModels; +using RestaurantContracts.StoragesContracts; +using RestaurantContracts.ViewModels; +using RestaurantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantDatabaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new RestaurantDatabase(); + var element = context.Clients + .FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + context.Clients.Remove(element); + return element.GetViewModel; + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new RestaurantDatabase(); + return context.Clients + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(ClientSearchModel model) + { + using var context = new RestaurantDatabase(); + if (model.Id.HasValue) + { + return context.Clients + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.Number != null) + { + return context.Clients + .Where(x => x.Number == model.Number) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.FirstName != null && model.LastName != null) + { + return context.Clients + .Where(x => (x.FirstName == model.FirstName && x.LastName == model.LastName)) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + return new(); + } + } + + public List GetFullList() + { + using var context = new RestaurantDatabase(); + return context.Clients + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + using var context = new RestaurantDatabase(); + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new RestaurantDatabase(); + var element = context.Clients + .FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Implements/ComponentStorage.cs b/Restaurant/RestaurantDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..f2f5f42 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,113 @@ +using Microsoft.EntityFrameworkCore; +using RestaurantContracts.BindingModels; +using RestaurantContracts.SearchModels; +using RestaurantContracts.StoragesContracts; +using RestaurantContracts.ViewModels; +using RestaurantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new RestaurantDatabase(); + return context.Components + .Include(x => x.Providers) + .ThenInclude(x => x.Provider) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + using var context = new RestaurantDatabase(); + if (model.Id.HasValue) + { + return context.Components + .Include(x => x.Providers) + .ThenInclude(x => x.ProviderId) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.Name != null) + { + return context.Components + .Include(x => x.Providers) + .ThenInclude(x => x.ProviderId) + .Where(x => x.Name == model.Name) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + return new(); + } + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && + !model.Id.HasValue) + { + return null; + } + using var context = new RestaurantDatabase(); + return context.Components + .Include(x => x.Providers) + .ThenInclude(x => x.Provider) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && + x.Name == model.Name) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + using var context = new RestaurantDatabase(); + var newComponent = Component.Create(context, model); + if (newComponent == null) + { + return null; + } + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new RestaurantDatabase(); + var element = context.Components + .Include(x => x.Providers) + .ThenInclude(x => x.ProviderId) + .FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return GetElement(new ComponentSearchModel { Id = element.Id }); + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new RestaurantDatabase(); + var element = context.Components + .Include(x => x.Providers) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Implements/OrderStorage.cs b/Restaurant/RestaurantDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..e16ff48 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,125 @@ +using Microsoft.EntityFrameworkCore; +using RestaurantContracts.BindingModels; +using RestaurantContracts.SearchModels; +using RestaurantContracts.StoragesContracts; +using RestaurantContracts.ViewModels; +using RestaurantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new RestaurantDatabase(); + return context.Orders + .Include(x => x.Client) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + using var context = new RestaurantDatabase(); + if (model.Id.HasValue) + { + return context.Orders + .Include(x => x.Products) + .ThenInclude(x => x.ProductId) + .Include(x => x.Client) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.ClientId.HasValue) + { + return context.Orders + .Include(x => x.Products) + .ThenInclude(x => x.ProductId) + .Include(x => x.Client) + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.Date.HasValue) + { + return context.Orders + .Include(x => x.Products) + .ThenInclude(x => x.ProductId) + .Include(x => x.Client) + .Where(x => x.Date == model.Date) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + return new(); + } + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new RestaurantDatabase(); + return context.Orders + .Include(x => x.Client) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new RestaurantDatabase(); + 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 RestaurantDatabase(); + var element = context.Orders + .Include(x => x.Client) + .Include(x => x.Products) + .ThenInclude(x => x.ProductId) + .FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return GetElement(new OrderSearchModel { Id = element.Id }); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new RestaurantDatabase(); + var element = context.Orders + .Include(x => x.Client) + .Include(x => x.Products) + .ThenInclude(x => x.ProductId) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Implements/ProductStorage.cs b/Restaurant/RestaurantDatabaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..832794b --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Implements/ProductStorage.cs @@ -0,0 +1,121 @@ +using Microsoft.EntityFrameworkCore; +using RestaurantContracts.BindingModels; +using RestaurantContracts.SearchModels; +using RestaurantContracts.StoragesContracts; +using RestaurantContracts.ViewModels; +using RestaurantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantDatabaseImplement.Implements +{ + public class ProductStorage : IProductStorage + { + public List GetFullList() + { + using var context = new RestaurantDatabase(); + return context.Products + .Include(x => x.Components) + .ThenInclude(x => x.ComponentId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ProductSearchModel model) + { + using var context = new RestaurantDatabase(); + if (model.Id.HasValue) + { + return context.Products + .Include(x => x.Components) + .ThenInclude(x => x.ComponentId) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.Type != null) + { + return context.Products + .Include(x => x.Components) + .ThenInclude(x => x.ComponentId) + .Where(x => x.Type == model.Type) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + return new(); + } + } + public ProductViewModel? GetElement(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.Type) && + !model.Id.HasValue) + { + return null; + } + using var context = new RestaurantDatabase(); + return context.Products + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Type) && + x.Type == model.Type) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public ProductViewModel? Insert(ProductBindingModel model) + { + using var context = new RestaurantDatabase(); + var newProduct = Product.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Products.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + public ProductViewModel? Update(ProductBindingModel model) + { + using var context = new RestaurantDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Products.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateComponents(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ProductViewModel? Delete(ProductBindingModel model) + { + using var context = new RestaurantDatabase(); + var element = context.Products + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Products.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Implements/ProviderStorage.cs b/Restaurant/RestaurantDatabaseImplement/Implements/ProviderStorage.cs new file mode 100644 index 0000000..9b37909 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Implements/ProviderStorage.cs @@ -0,0 +1,97 @@ +using Microsoft.EntityFrameworkCore; +using RestaurantContracts.BindingModels; +using RestaurantContracts.SearchModels; +using RestaurantContracts.StoragesContracts; +using RestaurantContracts.ViewModels; +using RestaurantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantDatabaseImplement.Implements +{ + public class ProviderStorage : IProviderStorage + { + public List GetFullList() + { + using var context = new RestaurantDatabase(); + return context.Providers.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ProviderSearchModel model) + { + using var context = new RestaurantDatabase(); + if (model.Id.HasValue) + { + return context.Providers + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.Name != null) + { + return context.Providers + .Where(x => x.Name == model.Name) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + return new(); + } + } + + public ProviderViewModel? GetElement(ProviderSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new RestaurantDatabase(); + return context.Providers + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public ProviderViewModel? Insert(ProviderBindingModel model) + { + var newProvider = Provider.Create(model); + if (newProvider == null) + { + return null; + } + using var context = new RestaurantDatabase(); + context.Providers.Add(newProvider); + context.SaveChanges(); + return newProvider.GetViewModel; + } + + public ProviderViewModel? Update(ProviderBindingModel model) + { + using var context = new RestaurantDatabase(); + var Provider = context.Providers.FirstOrDefault(x => x.Id == model.Id); + if (Provider == null) + { + return null; + } + Provider.Update(model); + context.SaveChanges(); + return Provider.GetViewModel; + } + + public ProviderViewModel? Delete(ProviderBindingModel model) + { + using var context = new RestaurantDatabase(); + var element = context.Providers.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Providers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Migrations/20230506075636_migr1.Designer.cs b/Restaurant/RestaurantDatabaseImplement/Migrations/20230506075636_migr1.Designer.cs new file mode 100644 index 0000000..074f4f6 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Migrations/20230506075636_migr1.Designer.cs @@ -0,0 +1,319 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using RestaurantDatabaseImplement; + +#nullable disable + +namespace RestaurantDatabaseImplement.Migrations +{ + [DbContext(typeof(RestaurantDatabase))] + [Migration("20230506075636_migr1")] + partial class migr1 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("text"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Number") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.ComponentProvider", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("Date") + .HasColumnType("timestamp with time zone"); + + b.Property("ProviderId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ProviderId"); + + b.ToTable("ComponentProviders"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("integer"); + + b.Property("Date") + .HasColumnType("timestamp with time zone"); + + b.Property("Price") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.OrderProduct", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("OrderId") + .HasColumnType("integer"); + + b.Property("ProductId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("OrderId"); + + b.HasIndex("ProductId"); + + b.ToTable("OrderProducts"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("Price") + .HasColumnType("integer"); + + b.Property("Type") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.ProductComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ProductId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ProductId"); + + b.ToTable("ProductComponents"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Provider", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Providers"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.ComponentProvider", b => + { + b.HasOne("RestaurantDatabaseImplement.Models.Component", "Component") + .WithMany("Providers") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RestaurantDatabaseImplement.Models.Provider", "Provider") + .WithMany("ComponentProviders") + .HasForeignKey("ProviderId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Provider"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Order", b => + { + b.HasOne("RestaurantDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.OrderProduct", b => + { + b.HasOne("RestaurantDatabaseImplement.Models.Order", "Order") + .WithMany("Products") + .HasForeignKey("OrderId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RestaurantDatabaseImplement.Models.Product", "Product") + .WithMany("OrderProducts") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Order"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.ProductComponent", b => + { + b.HasOne("RestaurantDatabaseImplement.Models.Component", "Component") + .WithMany("ProductComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RestaurantDatabaseImplement.Models.Product", "Product") + .WithMany("Components") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Component", b => + { + b.Navigation("ProductComponents"); + + b.Navigation("Providers"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Order", b => + { + b.Navigation("Products"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Product", b => + { + b.Navigation("Components"); + + b.Navigation("OrderProducts"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Provider", b => + { + b.Navigation("ComponentProviders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Migrations/20230506075636_migr1.cs b/Restaurant/RestaurantDatabaseImplement/Migrations/20230506075636_migr1.cs new file mode 100644 index 0000000..1bb6257 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Migrations/20230506075636_migr1.cs @@ -0,0 +1,241 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace RestaurantDatabaseImplement.Migrations +{ + /// + public partial class migr1 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Clients", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + FirstName = table.Column(type: "text", nullable: false), + LastName = table.Column(type: "text", nullable: false), + Number = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Clients", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false), + Price = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Products", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Type = table.Column(type: "text", nullable: false), + Price = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Products", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Providers", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false), + Address = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Providers", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ClientId = table.Column(type: "integer", nullable: false), + Price = table.Column(type: "integer", nullable: false), + Date = table.Column(type: "timestamp with time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Clients_ClientId", + column: x => x.ClientId, + principalTable: "Clients", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "ProductComponents", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ProductId = table.Column(type: "integer", nullable: false), + ComponentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ProductComponents", x => x.Id); + table.ForeignKey( + name: "FK_ProductComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ProductComponents_Products_ProductId", + column: x => x.ProductId, + principalTable: "Products", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "ComponentProviders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ComponentId = table.Column(type: "integer", nullable: false), + ProviderId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false), + Date = table.Column(type: "timestamp with time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ComponentProviders", x => x.Id); + table.ForeignKey( + name: "FK_ComponentProviders_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ComponentProviders_Providers_ProviderId", + column: x => x.ProviderId, + principalTable: "Providers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "OrderProducts", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + OrderId = table.Column(type: "integer", nullable: false), + ProductId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_OrderProducts", x => x.Id); + table.ForeignKey( + name: "FK_OrderProducts_Orders_OrderId", + column: x => x.OrderId, + principalTable: "Orders", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_OrderProducts_Products_ProductId", + column: x => x.ProductId, + principalTable: "Products", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ComponentProviders_ComponentId", + table: "ComponentProviders", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_ComponentProviders_ProviderId", + table: "ComponentProviders", + column: "ProviderId"); + + migrationBuilder.CreateIndex( + name: "IX_OrderProducts_OrderId", + table: "OrderProducts", + column: "OrderId"); + + migrationBuilder.CreateIndex( + name: "IX_OrderProducts_ProductId", + table: "OrderProducts", + column: "ProductId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ClientId", + table: "Orders", + column: "ClientId"); + + migrationBuilder.CreateIndex( + name: "IX_ProductComponents_ComponentId", + table: "ProductComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_ProductComponents_ProductId", + table: "ProductComponents", + column: "ProductId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ComponentProviders"); + + migrationBuilder.DropTable( + name: "OrderProducts"); + + migrationBuilder.DropTable( + name: "ProductComponents"); + + migrationBuilder.DropTable( + name: "Providers"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Products"); + + migrationBuilder.DropTable( + name: "Clients"); + } + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Migrations/RestaurantDatabaseModelSnapshot.cs b/Restaurant/RestaurantDatabaseImplement/Migrations/RestaurantDatabaseModelSnapshot.cs new file mode 100644 index 0000000..cef9d22 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Migrations/RestaurantDatabaseModelSnapshot.cs @@ -0,0 +1,316 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using RestaurantDatabaseImplement; + +#nullable disable + +namespace RestaurantDatabaseImplement.Migrations +{ + [DbContext(typeof(RestaurantDatabase))] + partial class RestaurantDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("text"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Number") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.ComponentProvider", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("Date") + .HasColumnType("timestamp with time zone"); + + b.Property("ProviderId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ProviderId"); + + b.ToTable("ComponentProviders"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("integer"); + + b.Property("Date") + .HasColumnType("timestamp with time zone"); + + b.Property("Price") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.OrderProduct", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("OrderId") + .HasColumnType("integer"); + + b.Property("ProductId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("OrderId"); + + b.HasIndex("ProductId"); + + b.ToTable("OrderProducts"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("Price") + .HasColumnType("integer"); + + b.Property("Type") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.ProductComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ProductId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ProductId"); + + b.ToTable("ProductComponents"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Provider", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Providers"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.ComponentProvider", b => + { + b.HasOne("RestaurantDatabaseImplement.Models.Component", "Component") + .WithMany("Providers") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RestaurantDatabaseImplement.Models.Provider", "Provider") + .WithMany("ComponentProviders") + .HasForeignKey("ProviderId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Provider"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Order", b => + { + b.HasOne("RestaurantDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.OrderProduct", b => + { + b.HasOne("RestaurantDatabaseImplement.Models.Order", "Order") + .WithMany("Products") + .HasForeignKey("OrderId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RestaurantDatabaseImplement.Models.Product", "Product") + .WithMany("OrderProducts") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Order"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.ProductComponent", b => + { + b.HasOne("RestaurantDatabaseImplement.Models.Component", "Component") + .WithMany("ProductComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RestaurantDatabaseImplement.Models.Product", "Product") + .WithMany("Components") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Component", b => + { + b.Navigation("ProductComponents"); + + b.Navigation("Providers"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Order", b => + { + b.Navigation("Products"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Product", b => + { + b.Navigation("Components"); + + b.Navigation("OrderProducts"); + }); + + modelBuilder.Entity("RestaurantDatabaseImplement.Models.Provider", b => + { + b.Navigation("ComponentProviders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Models/Client.cs b/Restaurant/RestaurantDatabaseImplement/Models/Client.cs new file mode 100644 index 0000000..35642c6 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Models/Client.cs @@ -0,0 +1,60 @@ +using RestaurantContracts.BindingModels; +using RestaurantContracts.ViewModels; +using RestaurantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantDatabaseImplement.Models +{ + public class Client : IClientModel + { + [Required] + public string FirstName { get; set; } = string.Empty; + + [Required] + public string LastName { get; set; } = string.Empty; + + [Required] + public string Number { get; set; } = string.Empty; + + public int Id { get; set; } + + public static Client? Create(ClientBindingModel? model) + { + if (model == null) + { + return null; + } + return new Client() + { + Id = model.Id, + FirstName = model.FirstName, + LastName = model.LastName, + Number = model.Number, + }; + } + + public void Update(ClientBindingModel? model) + { + if (model == null) + { + return; + } + FirstName = model.FirstName; + LastName = model.LastName; + Number = model.Number; + } + + public ClientViewModel GetViewModel => new() + { + Id = Id, + FirstName = FirstName, + LastName = LastName, + Number = Number, + }; + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Models/Component.cs b/Restaurant/RestaurantDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..47efc93 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Models/Component.cs @@ -0,0 +1,116 @@ +using RestaurantContracts.BindingModels; +using RestaurantContracts.ViewModels; +using RestaurantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantDatabaseImplement.Models +{ + public class Component : IComponentModel + { + [Required] + public string Name { get; private set; } = string.Empty; + + [Required] + public int Price { get; private set; } + + [Required] + public int Count { get; private set; } + + private Dictionary? _componentProviders = null; + + [NotMapped] + public Dictionary ComponentProviders + { + get + { + if (_componentProviders == null) + { + _componentProviders = Providers.ToDictionary(recPC => recPC.ProviderId, recPC => + (recPC.Provider as IProviderModel, recPC.Count, recPC.Date)); + } + return _componentProviders; + } + } + + [ForeignKey("ComponentId")] + public virtual List Providers { get; set; } = new(); + + public int Id { get; private set; } + + [ForeignKey("ComponentId")] + public virtual List ProductComponents { get; set; } = new(); + + public static Component? Create(RestaurantDatabase context, ComponentBindingModel? model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + Name = model.Name, + Price = model.Price, + Count = model.Count, + Providers = model.ComponentProviders.Select(x => new ComponentProvider + { + Provider = context.Providers.First(y => y.Id == x.Key), + Count = x.Value.Item2, + Date = x.Value.Item3 + }).ToList() + }; + } + + public void Update(ComponentBindingModel? model) + { + Name = model.Name; + Price = model.Price; + Count = model.Count; + } + + public ComponentViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Price = Price, + Count = Count, + ComponentProviders = ComponentProviders, + }; + + public void UpdateComponents(RestaurantDatabase context, ComponentBindingModel model) + { + var ComponentProviders = context.ComponentProviders.Where(rec => rec.ComponentId == model.Id).ToList(); + if (ComponentProviders != null && ComponentProviders.Count > 0) + { // удалили те, которых нет в модели + context.ComponentProviders.RemoveRange(ComponentProviders.Where(rec => !model.ComponentProviders.ContainsKey(rec.ProviderId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in ComponentProviders) + { + updateComponent.Count = model.ComponentProviders[updateComponent.ProviderId].Item2; + model.ComponentProviders.Remove(updateComponent.ProviderId); + } + context.SaveChanges(); + } + var Component = context.Components.First(x => x.Id == Id); + foreach (var pc in model.ComponentProviders) + { + context.ComponentProviders.Add(new ComponentProvider + { + Component = Component, + Provider = context.Providers.First(x => x.Id == pc.Key), + Count = pc.Value.Item2, + Date = pc.Value.Item3 // ?????? + }); + context.SaveChanges(); + } + _componentProviders = null; + } + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Models/ComponentProvider.cs b/Restaurant/RestaurantDatabaseImplement/Models/ComponentProvider.cs new file mode 100644 index 0000000..7747dae --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Models/ComponentProvider.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantDatabaseImplement.Models +{ + public class ComponentProvider + { + public int Id { get; set; } + + [Required] + public int ComponentId { get; set; } + public virtual Component Component { get; set; } = new(); + + [Required] + public int ProviderId { get; set; } + public virtual Provider Provider { get; set; } = new(); + + [Required] + public int Count { get; set; } + + [Required] + public DateTime Date { get; set; } + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Models/Order.cs b/Restaurant/RestaurantDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..19ed1be --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Models/Order.cs @@ -0,0 +1,110 @@ +using RestaurantContracts.BindingModels; +using RestaurantContracts.ViewModels; +using RestaurantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantDatabaseImplement.Models +{ + public class Order : IOrderModel + { + [Required] + public int ClientId { get; set; } + public virtual Client Client { get; set; } = new(); + + [Required] + public int Price { get; set; } + + [Required] + public DateTime Date { get; set; } + + private Dictionary? _orderProducts = null; + + [NotMapped] + public Dictionary OrderProducts + { + get + { + if (_orderProducts == null) + { + _orderProducts = Products.ToDictionary(recPC => recPC.ProductId, recPC => + (recPC.Product as IProductModel, recPC.Count)); + } + return _orderProducts; + } + } + + [ForeignKey("OrderId")] + public virtual List Products { get; set; } = new(); + + public int Id { get; set; } + + public static Order? Create(RestaurantDatabase context, OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + ClientId = model.ClientId, + Price = model.Price, + Date = model.Date, + Products = model.OrderProducts.Select(x => new OrderProduct + { + Product = context.Products.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(OrderBindingModel? model) + { + Date = model.Date; + Price = model.Price; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + ClientId = ClientId, + Price = Price, + Date = Date, + }; + + public void UpdateComponents(RestaurantDatabase context, OrderBindingModel model) + { + var OrderProducts = context.OrderProducts.Where(rec => rec.OrderId == model.Id).ToList(); + if (OrderProducts != null && OrderProducts.Count > 0) + { // удалили те, которых нет в модели + context.OrderProducts.RemoveRange(OrderProducts.Where(rec => !model.OrderProducts.ContainsKey(rec.ProductId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in OrderProducts) + { + updateComponent.Count = model.OrderProducts[updateComponent.ProductId].Item2; + model.OrderProducts.Remove(updateComponent.ProductId); + } + context.SaveChanges(); + } + var Order = context.Orders.First(x => x.Id == Id); + foreach (var pc in model.OrderProducts) + { + context.OrderProducts.Add(new OrderProduct + { + Order = Order, + Product = context.Products.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _orderProducts = null; + } + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Models/OrderProduct.cs b/Restaurant/RestaurantDatabaseImplement/Models/OrderProduct.cs new file mode 100644 index 0000000..1857bf5 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Models/OrderProduct.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantDatabaseImplement.Models +{ + public class OrderProduct + { + public int Id { get; set; } + + [Required] + public int OrderId { get; set; } + public virtual Order Order { get; set; } = new(); + + [Required] + public int ProductId { get; set; } + public virtual Product Product { get; set; } = new(); + + [Required] + public int Count { get; set; } + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Models/Product.cs b/Restaurant/RestaurantDatabaseImplement/Models/Product.cs new file mode 100644 index 0000000..6b2e033 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Models/Product.cs @@ -0,0 +1,113 @@ +using RestaurantContracts.BindingModels; +using RestaurantContracts.ViewModels; +using RestaurantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantDatabaseImplement.Models +{ + public class Product : IProductModel + { + [Required] + public string Type { get; private set; } = string.Empty; + + [Required] + public int Price { get; private set; } + + [Required] + public int Count { get; private set; } + + private Dictionary? _productComponents = null; + + [NotMapped] + public Dictionary ProductComponents + { + get + { + if (_productComponents == null) + { + _productComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _productComponents; + } + } + + public int Id { get; private set; } + + [ForeignKey("ProductId")] + public virtual List Components { get; private set; } = new(); + + [ForeignKey("ProductId")] + public virtual List OrderProducts { get; private set; } = new(); + + public static Product? Create(RestaurantDatabase context, ProductBindingModel? model) + { + if (model == null) + { + return null; + } + return new Product() + { + Id = model.Id, + Type = model.Type, + Price = model.Price, + Components = model.ProductComponents.Select(x => new ProductComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ProductBindingModel? model) + { + Type = model.Type; + Price = model.Price; + Count = model.Count; + } + + public ProductViewModel GetViewModel => new() + { + Id = Id, + Type = Type, + Price = Price, + Count = Count, + ProductComponents = ProductComponents + }; + + public void UpdateComponents(RestaurantDatabase context, ProductBindingModel model) + { + var ProductComponents = context.ProductComponents.Where(rec => rec.ProductId == model.Id).ToList(); + if (ProductComponents != null && ProductComponents.Count > 0) + { // удалили те, которых нет в модели + context.ProductComponents.RemoveRange(ProductComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in ProductComponents) + { + updateComponent.Count = model.ProductComponents[updateComponent.ComponentId].Item2; + model.ProductComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var Product = context.Products.First(x => x.Id == Id); + foreach (var pc in model.ProductComponents) + { + context.ProductComponents.Add(new ProductComponent + { + Product = Product, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _productComponents = null; + } + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Models/ProductComponent.cs b/Restaurant/RestaurantDatabaseImplement/Models/ProductComponent.cs new file mode 100644 index 0000000..d5c8f83 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Models/ProductComponent.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantDatabaseImplement.Models +{ + public class ProductComponent + { + public int Id { get; set; } + + [Required] + public int ProductId { get; set; } + public virtual Product Product { get; set; } = new(); + + [Required] + public int ComponentId { get; set; } + public virtual Component Component { get; set; } = new(); + + [Required] + public int Count { get; set; } + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/Models/Provider.cs b/Restaurant/RestaurantDatabaseImplement/Models/Provider.cs new file mode 100644 index 0000000..bda77b6 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/Models/Provider.cs @@ -0,0 +1,71 @@ +using RestaurantContracts.BindingModels; +using RestaurantContracts.ViewModels; +using RestaurantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantDatabaseImplement.Models +{ + public class Provider : IProviderModel + { + [Required] + public string Name { get; private set; } = string.Empty; + + [Required] + public string Address { get; private set; } = string.Empty; + + public int Id { get; private set; } + + [ForeignKey("ProviderId")] + public virtual List ComponentProviders { get; set; } = new(); + public static Provider? Create(ProviderBindingModel model) + { + if (model == null) + { + return null; + } + return new Provider() + { + Id = model.Id, + Name = model.Name, + Address = model.Address + }; + } + + public static Provider Create(ProviderViewModel model) + { + if (model == null) + { + return null; + } + return new Provider() + { + Id = model.Id, + Name = model.Name, + Address = model.Address + }; + } + + public void Update(ProviderBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + Address = model.Address; + } + + public ProviderViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Address = Address + }; + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/RestaurantDatabase.cs b/Restaurant/RestaurantDatabaseImplement/RestaurantDatabase.cs new file mode 100644 index 0000000..b1f48e4 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/RestaurantDatabase.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore; +using RestaurantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RestaurantDatabaseImplement +{ + public class RestaurantDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql(@" + Host=localhost; + Port=5432; + Database=RestaurantDatabase; + Username=postgres; + Password=postgres"); + } + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet Components { set; get; } + public virtual DbSet Products { set; get; } + public virtual DbSet ProductComponents { set; get; } + public virtual DbSet Providers { set; get; } + public virtual DbSet ComponentProviders { set; get; } + public virtual DbSet Orders { set; get; } + public virtual DbSet OrderProducts { set; get; } + public virtual DbSet Clients { set; get; } + } +} diff --git a/Restaurant/RestaurantDatabaseImplement/RestaurantDatabaseImplement.csproj b/Restaurant/RestaurantDatabaseImplement/RestaurantDatabaseImplement.csproj new file mode 100644 index 0000000..b560eb7 --- /dev/null +++ b/Restaurant/RestaurantDatabaseImplement/RestaurantDatabaseImplement.csproj @@ -0,0 +1,27 @@ + + + + net6.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/Restaurant/RestaurantView/Form1.cs b/Restaurant/RestaurantView/Form1.cs deleted file mode 100644 index 4889ae2..0000000 --- a/Restaurant/RestaurantView/Form1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace RestaurantView -{ - public partial class Form1 : Form - { - public Form1() - { - InitializeComponent(); - } - } -} \ No newline at end of file diff --git a/Restaurant/RestaurantView/FormClient.Designer.cs b/Restaurant/RestaurantView/FormClient.Designer.cs new file mode 100644 index 0000000..22fe79e --- /dev/null +++ b/Restaurant/RestaurantView/FormClient.Designer.cs @@ -0,0 +1,141 @@ +namespace RestaurantView +{ + partial class FormClient + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.labelFirstName = new System.Windows.Forms.Label(); + this.labelLastName = new System.Windows.Forms.Label(); + this.labelNumber = new System.Windows.Forms.Label(); + this.textBoxFirstName = new System.Windows.Forms.TextBox(); + this.textBoxLastName = new System.Windows.Forms.TextBox(); + this.textBoxNumber = new System.Windows.Forms.TextBox(); + this.SuspendLayout(); + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(53, 160); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(105, 37); + this.buttonSave.TabIndex = 0; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(214, 160); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(121, 37); + this.buttonCancel.TabIndex = 1; + this.buttonCancel.Text = "Отменить"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // labelFirstName + // + this.labelFirstName.AutoSize = true; + this.labelFirstName.Location = new System.Drawing.Point(46, 25); + this.labelFirstName.Name = "labelFirstName"; + this.labelFirstName.Size = new System.Drawing.Size(34, 15); + this.labelFirstName.TabIndex = 2; + this.labelFirstName.Text = "Имя:"; + // + // labelLastName + // + this.labelLastName.AutoSize = true; + this.labelLastName.Location = new System.Drawing.Point(19, 70); + this.labelLastName.Name = "labelLastName"; + this.labelLastName.Size = new System.Drawing.Size(61, 15); + this.labelLastName.TabIndex = 3; + this.labelLastName.Text = "Фамилия:"; + // + // labelNumber + // + this.labelNumber.AutoSize = true; + this.labelNumber.Location = new System.Drawing.Point(32, 113); + this.labelNumber.Name = "labelNumber"; + this.labelNumber.Size = new System.Drawing.Size(48, 15); + this.labelNumber.TabIndex = 4; + this.labelNumber.Text = "Номер:"; + // + // textBoxFirstName + // + this.textBoxFirstName.Location = new System.Drawing.Point(86, 22); + this.textBoxFirstName.Name = "textBoxFirstName"; + this.textBoxFirstName.Size = new System.Drawing.Size(273, 23); + this.textBoxFirstName.TabIndex = 5; + // + // textBoxLastName + // + this.textBoxLastName.Location = new System.Drawing.Point(86, 67); + this.textBoxLastName.Name = "textBoxLastName"; + this.textBoxLastName.Size = new System.Drawing.Size(273, 23); + this.textBoxLastName.TabIndex = 6; + // + // textBoxNumber + // + this.textBoxNumber.Location = new System.Drawing.Point(86, 110); + this.textBoxNumber.Name = "textBoxNumber"; + this.textBoxNumber.Size = new System.Drawing.Size(273, 23); + this.textBoxNumber.TabIndex = 7; + // + // FormClient + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(393, 215); + this.Controls.Add(this.textBoxNumber); + this.Controls.Add(this.textBoxLastName); + this.Controls.Add(this.textBoxFirstName); + this.Controls.Add(this.labelNumber); + this.Controls.Add(this.labelLastName); + this.Controls.Add(this.labelFirstName); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Name = "FormClient"; + this.Text = "Клиент"; + this.Load += new System.EventHandler(this.FormClient_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Button buttonSave; + private Button buttonCancel; + private Label labelFirstName; + private Label labelLastName; + private Label labelNumber; + private TextBox textBoxFirstName; + private TextBox textBoxLastName; + private TextBox textBoxNumber; + } +} \ No newline at end of file diff --git a/Restaurant/RestaurantView/FormClient.cs b/Restaurant/RestaurantView/FormClient.cs new file mode 100644 index 0000000..bc69df1 --- /dev/null +++ b/Restaurant/RestaurantView/FormClient.cs @@ -0,0 +1,89 @@ +using RestaurantContracts.BindingModels; +using RestaurantContracts.BusinessLogicsContracts; +using RestaurantContracts.SearchModels; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace RestaurantView +{ + public partial class FormClient : Form + { + private readonly IClientLogic _clientLogic; + private int? _id; + public int Id { set { _id = value; } } + public FormClient(IClientLogic clientLogic) + { + InitializeComponent(); + _clientLogic = clientLogic; + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + + private void FormClient_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + var view = _clientLogic.ReadElement(new ClientSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxFirstName.Text = view.FirstName; + textBoxLastName.Text = view.LastName; + textBoxNumber.Text = view.Number; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxFirstName.Text) && string.IsNullOrEmpty(textBoxLastName.Text) && string.IsNullOrEmpty(textBoxNumber.Text)) + { + MessageBox.Show("Введены не все данные.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + try + { + var model = new ClientBindingModel + { + Id = _id ?? 0, + FirstName = textBoxFirstName.Text, + LastName = textBoxLastName.Text, + Number = textBoxNumber.Text + }; + var operationResult = _id.HasValue ? _clientLogic.Update(model) : _clientLogic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} diff --git a/Restaurant/RestaurantView/FormClient.resx b/Restaurant/RestaurantView/FormClient.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Restaurant/RestaurantView/FormClient.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Restaurant/RestaurantView/FormClients.Designer.cs b/Restaurant/RestaurantView/FormClients.Designer.cs new file mode 100644 index 0000000..9c1d59c --- /dev/null +++ b/Restaurant/RestaurantView/FormClients.Designer.cs @@ -0,0 +1,114 @@ +namespace RestaurantView +{ + partial class FormClients + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.buttonDel = new System.Windows.Forms.Button(); + this.buttonUpd = new System.Windows.Forms.Button(); + this.buttonRef = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(0, 0); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowTemplate.Height = 25; + this.dataGridView.Size = new System.Drawing.Size(728, 479); + this.dataGridView.TabIndex = 0; + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(746, 42); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(85, 38); + this.buttonAdd.TabIndex = 1; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.ButtonAdd_Click); + // + // buttonDel + // + this.buttonDel.Location = new System.Drawing.Point(746, 110); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(85, 36); + this.buttonDel.TabIndex = 2; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; + this.buttonDel.Click += new System.EventHandler(this.ButtonDelete_Click); + // + // buttonUpd + // + this.buttonUpd.Location = new System.Drawing.Point(746, 175); + this.buttonUpd.Name = "buttonUpd"; + this.buttonUpd.Size = new System.Drawing.Size(85, 33); + this.buttonUpd.TabIndex = 3; + this.buttonUpd.Text = "Изменить"; + this.buttonUpd.UseVisualStyleBackColor = true; + this.buttonUpd.Click += new System.EventHandler(this.ButtonChange_Click); + // + // buttonRef + // + this.buttonRef.Location = new System.Drawing.Point(746, 240); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(85, 30); + this.buttonRef.TabIndex = 4; + this.buttonRef.Text = "Обновить"; + this.buttonRef.UseVisualStyleBackColor = true; + this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); + // + // FormClients + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(852, 478); + this.Controls.Add(this.buttonRef); + this.Controls.Add(this.buttonUpd); + this.Controls.Add(this.buttonDel); + this.Controls.Add(this.buttonAdd); + this.Controls.Add(this.dataGridView); + this.Name = "FormClients"; + this.Text = "Клиенты"; + this.Load += new System.EventHandler(this.FormClients_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonAdd; + private Button buttonDel; + private Button buttonUpd; + private Button buttonRef; + } +} \ No newline at end of file diff --git a/Restaurant/RestaurantView/FormClients.cs b/Restaurant/RestaurantView/FormClients.cs new file mode 100644 index 0000000..ffc38b5 --- /dev/null +++ b/Restaurant/RestaurantView/FormClients.cs @@ -0,0 +1,106 @@ +using RestaurantContracts.BindingModels; +using RestaurantContracts.BusinessLogicsContracts; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace RestaurantView +{ + public partial class FormClients : Form + { + private readonly IClientLogic _clientLogic; + + public FormClients(IClientLogic clientLogic) + { + InitializeComponent(); + _clientLogic = clientLogic; + } + + private void LoadData() + { + try + { + var list = _clientLogic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + + private void ButtonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormClient)); + if (service is FormClient form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void ButtonChange_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormClient)); + if (service is FormClient form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void ButtonDelete_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + try + { + if (!_clientLogic.Delete(new ClientBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении."); + } + LoadData(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + + private void FormClients_Load(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/Restaurant/RestaurantView/FormClients.resx b/Restaurant/RestaurantView/FormClients.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Restaurant/RestaurantView/FormClients.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Restaurant/RestaurantView/FormComponent.Designer.cs b/Restaurant/RestaurantView/FormComponent.Designer.cs new file mode 100644 index 0000000..0a4fc47 --- /dev/null +++ b/Restaurant/RestaurantView/FormComponent.Designer.cs @@ -0,0 +1,246 @@ +namespace RestaurantView +{ + partial class FormComponent + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.labelName = new System.Windows.Forms.Label(); + this.labelPrice = new System.Windows.Forms.Label(); + this.labelCount = new System.Windows.Forms.Label(); + this.textBoxName = new System.Windows.Forms.TextBox(); + this.textBoxPrice = new System.Windows.Forms.TextBox(); + this.textBoxCount = new System.Windows.Forms.TextBox(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.buttonDel = new System.Windows.Forms.Button(); + this.buttonUpd = new System.Windows.Forms.Button(); + this.buttonRef = new System.Windows.Forms.Button(); + this.Id = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Provider = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Count = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(233, 519); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(92, 33); + this.buttonSave.TabIndex = 0; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(380, 519); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(87, 33); + this.buttonCancel.TabIndex = 1; + this.buttonCancel.Text = "Отменить"; + this.buttonCancel.UseVisualStyleBackColor = true; + // + // labelName + // + this.labelName.AutoSize = true; + this.labelName.Location = new System.Drawing.Point(30, 25); + this.labelName.Name = "labelName"; + this.labelName.Size = new System.Drawing.Size(62, 15); + this.labelName.TabIndex = 2; + this.labelName.Text = "Название:"; + // + // labelPrice + // + this.labelPrice.AutoSize = true; + this.labelPrice.Location = new System.Drawing.Point(54, 68); + this.labelPrice.Name = "labelPrice"; + this.labelPrice.Size = new System.Drawing.Size(38, 15); + this.labelPrice.TabIndex = 3; + this.labelPrice.Text = "Цена:"; + // + // labelCount + // + this.labelCount.AutoSize = true; + this.labelCount.Location = new System.Drawing.Point(17, 113); + this.labelCount.Name = "labelCount"; + this.labelCount.Size = new System.Drawing.Size(75, 15); + this.labelCount.TabIndex = 4; + this.labelCount.Text = "Количество:"; + // + // textBoxName + // + this.textBoxName.Location = new System.Drawing.Point(98, 22); + this.textBoxName.Name = "textBoxName"; + this.textBoxName.Size = new System.Drawing.Size(295, 23); + this.textBoxName.TabIndex = 5; + // + // textBoxPrice + // + this.textBoxPrice.Enabled = false; + this.textBoxPrice.Location = new System.Drawing.Point(98, 65); + this.textBoxPrice.Name = "textBoxPrice"; + this.textBoxPrice.Size = new System.Drawing.Size(295, 23); + this.textBoxPrice.TabIndex = 6; + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(98, 110); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(295, 23); + this.textBoxCount.TabIndex = 7; + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.buttonRef); + this.groupBox1.Controls.Add(this.buttonUpd); + this.groupBox1.Controls.Add(this.buttonDel); + this.groupBox1.Controls.Add(this.buttonAdd); + this.groupBox1.Controls.Add(this.dataGridView); + this.groupBox1.Location = new System.Drawing.Point(30, 186); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(622, 292); + this.groupBox1.TabIndex = 8; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Поставщики"; + // + // dataGridView1 + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.Id, + this.Provider, + this.Count}); + this.dataGridView.Location = new System.Drawing.Point(6, 22); + this.dataGridView.Name = "dataGridView1"; + this.dataGridView.RowTemplate.Height = 25; + this.dataGridView.Size = new System.Drawing.Size(466, 251); + this.dataGridView.TabIndex = 0; + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(512, 22); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(75, 23); + this.buttonAdd.TabIndex = 1; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click); + // + // buttonDel + // + this.buttonDel.Location = new System.Drawing.Point(512, 79); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(75, 23); + this.buttonDel.TabIndex = 2; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; + // + // buttonUpd + // + this.buttonUpd.Location = new System.Drawing.Point(512, 136); + this.buttonUpd.Name = "buttonUpd"; + this.buttonUpd.Size = new System.Drawing.Size(75, 23); + this.buttonUpd.TabIndex = 3; + this.buttonUpd.Text = "Изменить"; + this.buttonUpd.UseVisualStyleBackColor = true; + // + // buttonRef + // + this.buttonRef.Location = new System.Drawing.Point(512, 194); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(75, 23); + this.buttonRef.TabIndex = 4; + this.buttonRef.Text = "Обновить"; + this.buttonRef.UseVisualStyleBackColor = true; + // + // Id + // + this.Id.HeaderText = "Id"; + this.Id.Name = "Id"; + this.Id.Visible = false; + // + // Provider + // + this.Provider.HeaderText = "Поставщик"; + this.Provider.Name = "Provider"; + this.Provider.Width = 200; + // + // Count + // + this.Count.HeaderText = "Количество"; + this.Count.Name = "Count"; + this.Count.Width = 200; + // + // FormComponent + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(679, 590); + this.Controls.Add(this.groupBox1); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.textBoxPrice); + this.Controls.Add(this.textBoxName); + this.Controls.Add(this.labelCount); + this.Controls.Add(this.labelPrice); + this.Controls.Add(this.labelName); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Name = "FormComponent"; + this.Text = "Компонент"; + this.Load += new System.EventHandler(this.FormComponent_Load); + this.groupBox1.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Button buttonSave; + private Button buttonCancel; + private Label labelName; + private Label labelPrice; + private Label labelCount; + private TextBox textBoxName; + private TextBox textBoxPrice; + private TextBox textBoxCount; + private GroupBox groupBox1; + private Button buttonRef; + private Button buttonUpd; + private Button buttonDel; + private Button buttonAdd; + private DataGridView dataGridView; + private DataGridViewTextBoxColumn Id; + private DataGridViewTextBoxColumn Provider; + private DataGridViewTextBoxColumn Count; + } +} \ No newline at end of file diff --git a/Restaurant/RestaurantView/FormComponent.cs b/Restaurant/RestaurantView/FormComponent.cs new file mode 100644 index 0000000..0c37d53 --- /dev/null +++ b/Restaurant/RestaurantView/FormComponent.cs @@ -0,0 +1,114 @@ +using RestaurantBusinessLogic.BusinessLogics; +using RestaurantContracts.BindingModels; +using RestaurantContracts.BusinessLogicsContracts; +using RestaurantContracts.SearchModels; +using RestaurantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace RestaurantView +{ + public partial class FormComponent : Form + { + private readonly IComponentLogic _componentLogic; + private Dictionary _componentProviders; + private int? _id; + public int Id { set { _id = value; } } + public FormComponent(IComponentLogic componentLogic) + { + InitializeComponent(); + _componentLogic = componentLogic; + _componentProviders = new Dictionary(); + } + + private void FormComponent_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + var view = _componentLogic.ReadElement(new ComponentSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxName.Text = view.Name; + textBoxPrice.Text = view.Price.ToString(); + textBoxCount.Text = view.Count.ToString(); + _componentProviders = view.ComponentProviders ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text) && string.IsNullOrEmpty(textBoxPrice.Text) && string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Введены не все данные.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + try + { + var model = new ComponentBindingModel + { + Id = _id ?? 0, + Name = textBoxName.Text, + Price = int.Parse(textBoxPrice.Text), + Count = int.Parse(textBoxCount.Text) + }; + var operationResult = _id.HasValue ? _componentLogic.Update(model) : _componentLogic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonAdd_Click(object sender, EventArgs e) + { + + } + + private void LoadData() + { + try + { + if (_componentProviders != null) + { + dataGridView.Rows.Clear(); + foreach (var pc in _componentProviders) + { + dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.Name, pc.Value.Item2, pc.Value.Item3 }); + } + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } +} diff --git a/Restaurant/RestaurantView/FormComponent.resx b/Restaurant/RestaurantView/FormComponent.resx new file mode 100644 index 0000000..4c5d442 --- /dev/null +++ b/Restaurant/RestaurantView/FormComponent.resx @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + True + + + True + + + True + + + True + + + True + + + True + + \ No newline at end of file diff --git a/Restaurant/RestaurantView/Form1.Designer.cs b/Restaurant/RestaurantView/FormComponentProvider.Designer.cs similarity index 75% rename from Restaurant/RestaurantView/Form1.Designer.cs rename to Restaurant/RestaurantView/FormComponentProvider.Designer.cs index 5c9a671..09c7141 100644 --- a/Restaurant/RestaurantView/Form1.Designer.cs +++ b/Restaurant/RestaurantView/FormComponentProvider.Designer.cs @@ -1,14 +1,14 @@ namespace RestaurantView { - partial class Form1 + partial class FormComponentProvider { /// - /// Required designer variable. + /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// - /// Clean up any resources being used. + /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) @@ -23,15 +23,15 @@ #region Windows Form Designer generated code /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "Form1"; + this.Text = "FormComponentProvider"; } #endregion diff --git a/Restaurant/RestaurantView/FormComponentProvider.cs b/Restaurant/RestaurantView/FormComponentProvider.cs new file mode 100644 index 0000000..9a522ad --- /dev/null +++ b/Restaurant/RestaurantView/FormComponentProvider.cs @@ -0,0 +1,92 @@ +using RestaurantContracts.BusinessLogicsContracts; +using RestaurantContracts.ViewModels; +using RestaurantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace RestaurantView +{ + public partial class FormComponentProvider : Form + { + private readonly List? _list; + + public int Id + { + get + { + return Convert.ToInt32(comboBoxComponent.SelectedValue); + } + set + { + comboBoxComponent.SelectedValue = value; + } + } + + public IProviderModel? ProviderModel + { + get + { + if (_list == null) + { + return null; + } + foreach (var elem in _list) + { + if (elem.Id == Id) + { + return elem; + } + } + return null; + } + } + public int Count + { + get { return Convert.ToInt32(textBoxCount.Text); } + set { textBoxCount.Text = value.ToString(); } + } + public FormComponentProvider(IProviderLogic logic) + { + InitializeComponent(); + _list = logic.ReadList(null); + if (_list != null) + { + comboBoxProvider.DisplayMember = "ComponentName"; + comboBoxProvider.ValueMember = "Id"; + comboBoxProvider.DataSource = _list; + comboBoxProvider.SelectedItem = null; + } + } + + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxComponent.SelectedValue == null) + { + MessageBox.Show("Выберите компонент", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + DialogResult = DialogResult.OK; + Close(); + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/Restaurant/RestaurantView/Form1.resx b/Restaurant/RestaurantView/FormComponentProvider.resx similarity index 100% rename from Restaurant/RestaurantView/Form1.resx rename to Restaurant/RestaurantView/FormComponentProvider.resx diff --git a/Restaurant/RestaurantView/FormComponents.Designer.cs b/Restaurant/RestaurantView/FormComponents.Designer.cs new file mode 100644 index 0000000..a691547 --- /dev/null +++ b/Restaurant/RestaurantView/FormComponents.Designer.cs @@ -0,0 +1,114 @@ +namespace RestaurantView +{ + partial class FormComponents + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.buttonDel = new System.Windows.Forms.Button(); + this.buttonChange = new System.Windows.Forms.Button(); + this.buttonRef = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(0, 0); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowTemplate.Height = 25; + this.dataGridView.Size = new System.Drawing.Size(531, 432); + this.dataGridView.TabIndex = 0; + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(549, 36); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(88, 49); + this.buttonAdd.TabIndex = 1; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.ButtonAdd_Click); + // + // buttonDel + // + this.buttonDel.Location = new System.Drawing.Point(549, 114); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(88, 47); + this.buttonDel.TabIndex = 2; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; + this.buttonDel.Click += new System.EventHandler(this.ButtonDelete_Click); + // + // buttonChange + // + this.buttonChange.Location = new System.Drawing.Point(549, 190); + this.buttonChange.Name = "buttonChange"; + this.buttonChange.Size = new System.Drawing.Size(88, 42); + this.buttonChange.TabIndex = 3; + this.buttonChange.Text = "Изменить"; + this.buttonChange.UseVisualStyleBackColor = true; + this.buttonChange.Click += new System.EventHandler(this.ButtonChange_Click); + // + // buttonRef + // + this.buttonRef.Location = new System.Drawing.Point(549, 260); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(88, 40); + this.buttonRef.TabIndex = 4; + this.buttonRef.Text = "Обновить"; + this.buttonRef.UseVisualStyleBackColor = true; + this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); + // + // FormComponents + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(655, 432); + this.Controls.Add(this.buttonRef); + this.Controls.Add(this.buttonChange); + this.Controls.Add(this.buttonDel); + this.Controls.Add(this.buttonAdd); + this.Controls.Add(this.dataGridView); + this.Name = "FormComponents"; + this.Text = "Компоненты"; + this.Load += new System.EventHandler(this.FormComponents_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonAdd; + private Button buttonDel; + private Button buttonChange; + private Button buttonRef; + } +} \ No newline at end of file diff --git a/Restaurant/RestaurantView/FormComponents.cs b/Restaurant/RestaurantView/FormComponents.cs new file mode 100644 index 0000000..9fd3329 --- /dev/null +++ b/Restaurant/RestaurantView/FormComponents.cs @@ -0,0 +1,108 @@ +using RestaurantBusinessLogic.BusinessLogics; +using RestaurantContracts.BindingModels; +using RestaurantContracts.BusinessLogicsContracts; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace RestaurantView +{ + public partial class FormComponents : Form + { + private readonly IComponentLogic _componentLogic; + + public FormComponents(IComponentLogic componentLogic) + { + InitializeComponent(); + _componentLogic = componentLogic; + } + + private void FormComponents_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _componentLogic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + + private void ButtonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); + if (service is FormComponent form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void ButtonChange_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); + if (service is FormComponent form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void ButtonDelete_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + try + { + if (!_componentLogic.Delete(new ComponentBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении."); + } + LoadData(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/Restaurant/RestaurantView/FormComponents.resx b/Restaurant/RestaurantView/FormComponents.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Restaurant/RestaurantView/FormComponents.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Restaurant/RestaurantView/FormMain.Designer.cs b/Restaurant/RestaurantView/FormMain.Designer.cs new file mode 100644 index 0000000..69781fc --- /dev/null +++ b/Restaurant/RestaurantView/FormMain.Designer.cs @@ -0,0 +1,121 @@ +namespace RestaurantView +{ + partial class FormMain + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.menuStrip1 = new System.Windows.Forms.MenuStrip(); + this.ClientsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ProvidersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ComponentsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.menuStrip1.SuspendLayout(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(0, 63); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowTemplate.Height = 25; + this.dataGridView.Size = new System.Drawing.Size(1162, 559); + this.dataGridView.TabIndex = 0; + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(977, 12); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(174, 45); + this.buttonAdd.TabIndex = 1; + this.buttonAdd.Text = "Создать заказ"; + this.buttonAdd.UseVisualStyleBackColor = true; + // + // menuStrip1 + // + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.ClientsToolStripMenuItem, + this.ProvidersToolStripMenuItem, + this.ComponentsToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 0); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(1163, 24); + this.menuStrip1.TabIndex = 2; + this.menuStrip1.Text = "menuStrip1"; + // + // ClientsToolStripMenuItem + // + this.ClientsToolStripMenuItem.Name = "ClientsToolStripMenuItem"; + this.ClientsToolStripMenuItem.Size = new System.Drawing.Size(67, 20); + this.ClientsToolStripMenuItem.Text = "Клиенты"; + this.ClientsToolStripMenuItem.Click += new System.EventHandler(this.ClientsToolStripMenuItem_Click); + // + // ProvidersToolStripMenuItem + // + this.ProvidersToolStripMenuItem.Name = "ProvidersToolStripMenuItem"; + this.ProvidersToolStripMenuItem.Size = new System.Drawing.Size(89, 20); + this.ProvidersToolStripMenuItem.Text = "Поставщики"; + this.ProvidersToolStripMenuItem.Click += new System.EventHandler(this.ProvidersToolStripMenuItem_Click); + // + // ComponentsToolStripMenuItem + // + this.ComponentsToolStripMenuItem.Name = "ComponentsToolStripMenuItem"; + this.ComponentsToolStripMenuItem.Size = new System.Drawing.Size(90, 20); + this.ComponentsToolStripMenuItem.Text = "Компоненты"; + this.ComponentsToolStripMenuItem.Click += new System.EventHandler(this.ComponentsToolStripMenuItem_Click); + // + // FormMain + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(1163, 634); + this.Controls.Add(this.buttonAdd); + this.Controls.Add(this.dataGridView); + this.Controls.Add(this.menuStrip1); + this.MainMenuStrip = this.menuStrip1; + this.Name = "FormMain"; + this.Text = "Заказы"; + this.Load += new System.EventHandler(this.FormMain_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonAdd; + private MenuStrip menuStrip1; + private ToolStripMenuItem ClientsToolStripMenuItem; + private ToolStripMenuItem ProvidersToolStripMenuItem; + private ToolStripMenuItem ComponentsToolStripMenuItem; + } +} \ No newline at end of file diff --git a/Restaurant/RestaurantView/FormMain.cs b/Restaurant/RestaurantView/FormMain.cs new file mode 100644 index 0000000..16365e2 --- /dev/null +++ b/Restaurant/RestaurantView/FormMain.cs @@ -0,0 +1,73 @@ +using RestaurantContracts.BusinessLogicsContracts; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace RestaurantView +{ + public partial class FormMain : Form + { + private readonly IOrderLogic _orderLogic; + + public FormMain(IOrderLogic orderLogic) + { + InitializeComponent(); + _orderLogic = orderLogic; + } + + private void LoadData() + { + try + { + var list = _orderLogic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["ClientId"].Visible = false; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void FormMain_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void ClientsToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormClients)); + if (service is FormClients form) + { + form.ShowDialog(); + } + } + + private void ProvidersToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormProviders)); + if (service is FormProviders form) + { + form.ShowDialog(); + } + } + + private void ComponentsToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormComponents)); + if (service is FormComponents form) + { + form.ShowDialog(); + } + } + } +} diff --git a/Restaurant/RestaurantView/FormMain.resx b/Restaurant/RestaurantView/FormMain.resx new file mode 100644 index 0000000..938108a --- /dev/null +++ b/Restaurant/RestaurantView/FormMain.resx @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/Restaurant/RestaurantView/FormProvider.Designer.cs b/Restaurant/RestaurantView/FormProvider.Designer.cs new file mode 100644 index 0000000..fc2e4ff --- /dev/null +++ b/Restaurant/RestaurantView/FormProvider.Designer.cs @@ -0,0 +1,119 @@ +namespace RestaurantView +{ + partial class FormProvider + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.labelName = new System.Windows.Forms.Label(); + this.labelAddress = new System.Windows.Forms.Label(); + this.textBoxName = new System.Windows.Forms.TextBox(); + this.textBoxAddress = new System.Windows.Forms.TextBox(); + this.SuspendLayout(); + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(51, 135); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(97, 31); + this.buttonSave.TabIndex = 0; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(202, 135); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(113, 31); + this.buttonCancel.TabIndex = 1; + this.buttonCancel.Text = "Отменить"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // labelName + // + this.labelName.AutoSize = true; + this.labelName.Location = new System.Drawing.Point(22, 31); + this.labelName.Name = "labelName"; + this.labelName.Size = new System.Drawing.Size(62, 15); + this.labelName.TabIndex = 2; + this.labelName.Text = "Название:"; + // + // labelAddress + // + this.labelAddress.AutoSize = true; + this.labelAddress.Location = new System.Drawing.Point(32, 80); + this.labelAddress.Name = "labelAddress"; + this.labelAddress.Size = new System.Drawing.Size(49, 15); + this.labelAddress.TabIndex = 3; + this.labelAddress.Text = "Адресс:"; + // + // textBoxName + // + this.textBoxName.Location = new System.Drawing.Point(90, 28); + this.textBoxName.Name = "textBoxName"; + this.textBoxName.Size = new System.Drawing.Size(248, 23); + this.textBoxName.TabIndex = 4; + // + // textBoxAddress + // + this.textBoxAddress.Location = new System.Drawing.Point(90, 77); + this.textBoxAddress.Name = "textBoxAddress"; + this.textBoxAddress.Size = new System.Drawing.Size(248, 23); + this.textBoxAddress.TabIndex = 5; + // + // FormProvider + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(376, 186); + this.Controls.Add(this.textBoxAddress); + this.Controls.Add(this.textBoxName); + this.Controls.Add(this.labelAddress); + this.Controls.Add(this.labelName); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Name = "FormProvider"; + this.Text = "Поставщик"; + this.Load += new System.EventHandler(this.FormProvider_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Button buttonSave; + private Button buttonCancel; + private Label labelName; + private Label labelAddress; + private TextBox textBoxName; + private TextBox textBoxAddress; + } +} \ No newline at end of file diff --git a/Restaurant/RestaurantView/FormProvider.cs b/Restaurant/RestaurantView/FormProvider.cs new file mode 100644 index 0000000..9f2013d --- /dev/null +++ b/Restaurant/RestaurantView/FormProvider.cs @@ -0,0 +1,88 @@ +using RestaurantBusinessLogic.BusinessLogics; +using RestaurantContracts.BindingModels; +using RestaurantContracts.BusinessLogicsContracts; +using RestaurantContracts.SearchModels; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace RestaurantView +{ + public partial class FormProvider : Form + { + private readonly IProviderLogic _providerLogic; + private int? _id; + public int Id { set { _id = value; } } + public FormProvider(IProviderLogic providerLogic) + { + InitializeComponent(); + _providerLogic = providerLogic; + } + + private void FormProvider_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + var view = _providerLogic.ReadElement(new ProviderSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxName.Text = view.Name; + textBoxAddress.Text = view.Address; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text) && string.IsNullOrEmpty(textBoxAddress.Text)) + { + MessageBox.Show("Введены не все данные.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + try + { + var model = new ProviderBindingModel + { + Id = _id ?? 0, + Name = textBoxName.Text, + Address = textBoxAddress.Text, + }; + var operationResult = _id.HasValue ? _providerLogic.Update(model) : _providerLogic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/Restaurant/RestaurantView/FormProvider.resx b/Restaurant/RestaurantView/FormProvider.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Restaurant/RestaurantView/FormProvider.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Restaurant/RestaurantView/FormProviders.Designer.cs b/Restaurant/RestaurantView/FormProviders.Designer.cs new file mode 100644 index 0000000..79abb56 --- /dev/null +++ b/Restaurant/RestaurantView/FormProviders.Designer.cs @@ -0,0 +1,114 @@ +namespace RestaurantView +{ + partial class FormProviders + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.buttonDel = new System.Windows.Forms.Button(); + this.buttonUpd = new System.Windows.Forms.Button(); + this.buttonRef = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(0, 0); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowTemplate.Height = 25; + this.dataGridView.Size = new System.Drawing.Size(665, 450); + this.dataGridView.TabIndex = 0; + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(682, 42); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(106, 35); + this.buttonAdd.TabIndex = 1; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.ButtonAdd_Click); + // + // buttonDel + // + this.buttonDel.Location = new System.Drawing.Point(682, 111); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(106, 33); + this.buttonDel.TabIndex = 2; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; + this.buttonDel.Click += new System.EventHandler(this.ButtonDelete_Click); + // + // buttonUpd + // + this.buttonUpd.Location = new System.Drawing.Point(682, 181); + this.buttonUpd.Name = "buttonUpd"; + this.buttonUpd.Size = new System.Drawing.Size(106, 35); + this.buttonUpd.TabIndex = 3; + this.buttonUpd.Text = "Изменить"; + this.buttonUpd.UseVisualStyleBackColor = true; + this.buttonUpd.Click += new System.EventHandler(this.ButtonChange_Click); + // + // buttonRef + // + this.buttonRef.Location = new System.Drawing.Point(682, 252); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(106, 41); + this.buttonRef.TabIndex = 4; + this.buttonRef.Text = "Обновить"; + this.buttonRef.UseVisualStyleBackColor = true; + this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); + // + // FormProviders + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.buttonRef); + this.Controls.Add(this.buttonUpd); + this.Controls.Add(this.buttonDel); + this.Controls.Add(this.buttonAdd); + this.Controls.Add(this.dataGridView); + this.Name = "FormProviders"; + this.Text = "Поставщики"; + this.Load += new System.EventHandler(this.FormProviders_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonAdd; + private Button buttonDel; + private Button buttonUpd; + private Button buttonRef; + } +} \ No newline at end of file diff --git a/Restaurant/RestaurantView/FormProviders.cs b/Restaurant/RestaurantView/FormProviders.cs new file mode 100644 index 0000000..0ec8ac6 --- /dev/null +++ b/Restaurant/RestaurantView/FormProviders.cs @@ -0,0 +1,106 @@ +using RestaurantContracts.BindingModels; +using RestaurantContracts.BusinessLogicsContracts; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace RestaurantView +{ + public partial class FormProviders : Form + { + private readonly IProviderLogic _providerLogic; + + public FormProviders(IProviderLogic providerLogic) + { + InitializeComponent(); + _providerLogic = providerLogic; + } + + private void LoadData() + { + try + { + var list = _providerLogic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + + private void ButtonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormProvider)); + if (service is FormProvider form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void ButtonChange_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormProvider)); + if (service is FormProvider form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void ButtonDelete_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + try + { + if (!_providerLogic.Delete(new ProviderBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении."); + } + LoadData(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + + private void FormProviders_Load(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/Restaurant/RestaurantView/FormProviders.resx b/Restaurant/RestaurantView/FormProviders.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Restaurant/RestaurantView/FormProviders.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Restaurant/RestaurantView/Program.cs b/Restaurant/RestaurantView/Program.cs index b6533e4..9933725 100644 --- a/Restaurant/RestaurantView/Program.cs +++ b/Restaurant/RestaurantView/Program.cs @@ -1,7 +1,16 @@ +using Microsoft.Extensions.DependencyInjection; +using RestaurantBusinessLogic.BusinessLogics; +using RestaurantContracts.BusinessLogicsContracts; +using RestaurantContracts.StoragesContracts; +using RestaurantDatabaseImplement.Implements; +using System; + namespace RestaurantView { internal static class Program { + private static ServiceProvider? _serviceProvider; + public static ServiceProvider? ServiceProvider => _serviceProvider; /// /// The main entry point for the application. /// @@ -11,7 +20,33 @@ namespace RestaurantView // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new Form1()); + var services = new ServiceCollection(); + ConfigureServices(services); + _serviceProvider = services.BuildServiceProvider(); + Application.Run(_serviceProvider.GetRequiredService()); + } + + private static void ConfigureServices(ServiceCollection services) + { + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/Restaurant/RestaurantView/RestaurantView.csproj b/Restaurant/RestaurantView/RestaurantView.csproj index b57c89e..fe0017d 100644 --- a/Restaurant/RestaurantView/RestaurantView.csproj +++ b/Restaurant/RestaurantView/RestaurantView.csproj @@ -8,4 +8,18 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + \ No newline at end of file