From be604f06f367641df660645cd3c6c1e07f549a0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=B0=D1=82=20=D0=97=D0=B0=D1=80=D0=B3?= =?UTF-8?q?=D0=B0=D1=80=D0=BE=D0=B2?= Date: Sat, 13 May 2023 21:40:59 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=B0?= =?UTF-8?q?=20=D0=B2=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89?= =?UTF-8?q?=D0=B0=20+=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B2=20=D0=B7=D0=B0=D0=BA=D0=B0=D0=B7=D0=B0=D1=85=20?= =?UTF-8?q?+=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Pizzeria/Pizzeria.sln | 10 +- Pizzeria/PizzeriaBusinessLogic/ClientLogic.cs | 123 ++++++++++ .../BindingModels/ClientBindingModel.cs | 17 ++ .../BindingModels/OrderBindingModel.cs | 1 + .../BusinessLogicsContracts/IClientLogic.cs | 21 ++ .../SearchModels/ClientSearchModel.cs | 19 ++ .../SearchModels/OrderSearchModel.cs | 1 + .../StorageContracts/IClientStorage.cs | 22 ++ .../ViewModels/ClientViewModel.cs | 22 ++ .../ViewModels/OrderViewModel.cs | 4 + .../PizzeriaDataModels/Models/IClientModel.cs | 17 ++ .../PizzeriaDataModels/Models/IOrderModel.cs | 1 + .../Implements/ClientStorage.cs | 90 ++++++++ .../Implements/OrderStorage.cs | 16 +- ...20230513164515_ClientMigration.Designer.cs | 214 ++++++++++++++++++ .../20230513164515_ClientMigration.cs | 69 ++++++ .../PizzeriaDatabaseModelSnapshot.cs | 43 ++++ .../Models/Client.cs | 64 ++++++ .../PizzeriaDatabaseImplement/Models/Order.cs | 8 + .../PizzeriaDatabase.cs | 1 + .../DataFileSingleton.cs | 4 + .../Implements/ClientStorage.cs | 93 ++++++++ .../Implements/OrderStorage.cs | 23 +- .../PizzeriaFileImplement/Models/Client.cs | 75 ++++++ .../PizzeriaFileImplement/Models/Order.cs | 5 + .../DataListSingleton.cs | 2 + .../Implements/ClientStorage.cs | 115 ++++++++++ .../Implements/OrderStorage.cs | 27 ++- .../PizzeriaListImplement/Models/Client.cs | 53 +++++ .../PizzeriaListImplement/Models/Order.cs | 3 + .../Controllers/ClientController.cs | 65 ++++++ .../Controllers/MainController.cs | 85 +++++++ .../PizzeriaRestApi/PizzeriaRestApi.csproj | 19 ++ Pizzeria/PizzeriaRestApi/Program.cs | 51 +++++ .../Properties/launchSettings.json | 31 +++ .../appsettings.Development.json | 8 + Pizzeria/PizzeriaRestApi/appsettings.json | 9 + Pizzeria/PizzeriaRestApi/log4net.config | 16 ++ Pizzeria/PizzeriaView/FormClients.Designer.cs | 92 ++++++++ Pizzeria/PizzeriaView/FormClients.cs | 75 ++++++ Pizzeria/PizzeriaView/FormClients.resx | 60 +++++ Pizzeria/PizzeriaView/FormMain.Designer.cs | 16 +- Pizzeria/PizzeriaView/FormMain.cs | 10 + Pizzeria/PizzeriaView/Program.cs | 7 +- 44 files changed, 1680 insertions(+), 27 deletions(-) create mode 100644 Pizzeria/PizzeriaBusinessLogic/ClientLogic.cs create mode 100644 Pizzeria/PizzeriaContracts/BindingModels/ClientBindingModel.cs create mode 100644 Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IClientLogic.cs create mode 100644 Pizzeria/PizzeriaContracts/SearchModels/ClientSearchModel.cs create mode 100644 Pizzeria/PizzeriaContracts/StorageContracts/IClientStorage.cs create mode 100644 Pizzeria/PizzeriaContracts/ViewModels/ClientViewModel.cs create mode 100644 Pizzeria/PizzeriaDataModels/Models/IClientModel.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Implements/ClientStorage.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Migrations/20230513164515_ClientMigration.Designer.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Migrations/20230513164515_ClientMigration.cs create mode 100644 Pizzeria/PizzeriaDatabaseImplement/Models/Client.cs create mode 100644 Pizzeria/PizzeriaFileImplement/Implements/ClientStorage.cs create mode 100644 Pizzeria/PizzeriaFileImplement/Models/Client.cs create mode 100644 Pizzeria/PizzeriaListImplement/Implements/ClientStorage.cs create mode 100644 Pizzeria/PizzeriaListImplement/Models/Client.cs create mode 100644 Pizzeria/PizzeriaRestApi/Controllers/ClientController.cs create mode 100644 Pizzeria/PizzeriaRestApi/Controllers/MainController.cs create mode 100644 Pizzeria/PizzeriaRestApi/PizzeriaRestApi.csproj create mode 100644 Pizzeria/PizzeriaRestApi/Program.cs create mode 100644 Pizzeria/PizzeriaRestApi/Properties/launchSettings.json create mode 100644 Pizzeria/PizzeriaRestApi/appsettings.Development.json create mode 100644 Pizzeria/PizzeriaRestApi/appsettings.json create mode 100644 Pizzeria/PizzeriaRestApi/log4net.config create mode 100644 Pizzeria/PizzeriaView/FormClients.Designer.cs create mode 100644 Pizzeria/PizzeriaView/FormClients.cs create mode 100644 Pizzeria/PizzeriaView/FormClients.resx diff --git a/Pizzeria/Pizzeria.sln b/Pizzeria/Pizzeria.sln index 761a0d5..83a1d65 100644 --- a/Pizzeria/Pizzeria.sln +++ b/Pizzeria/Pizzeria.sln @@ -13,9 +13,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaListImplement", "Pi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaView", "PizzeriaView\PizzeriaView.csproj", "{25FCC09C-88AE-4515-9235-FB9C17972034}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaFileImplement", "PizzeriaFileImplement\PizzeriaFileImplement.csproj", "{BD6EEE24-695A-42BF-BE1C-B4058218A8D9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaFileImplement", "PizzeriaFileImplement\PizzeriaFileImplement.csproj", "{BD6EEE24-695A-42BF-BE1C-B4058218A8D9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaDatabaseImplement", "PizzeriaDatabaseImplement\PizzeriaDatabaseImplement.csproj", "{4CCDEA60-47C3-478E-AE8C-146A8583BAF7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaDatabaseImplement", "PizzeriaDatabaseImplement\PizzeriaDatabaseImplement.csproj", "{4CCDEA60-47C3-478E-AE8C-146A8583BAF7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaRestApi", "PizzeriaRestApi\PizzeriaRestApi.csproj", "{58517590-C346-42C0-B669-EA2C90D54A8B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -51,6 +53,10 @@ Global {4CCDEA60-47C3-478E-AE8C-146A8583BAF7}.Debug|Any CPU.Build.0 = Debug|Any CPU {4CCDEA60-47C3-478E-AE8C-146A8583BAF7}.Release|Any CPU.ActiveCfg = Release|Any CPU {4CCDEA60-47C3-478E-AE8C-146A8583BAF7}.Release|Any CPU.Build.0 = Release|Any CPU + {58517590-C346-42C0-B669-EA2C90D54A8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {58517590-C346-42C0-B669-EA2C90D54A8B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {58517590-C346-42C0-B669-EA2C90D54A8B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {58517590-C346-42C0-B669-EA2C90D54A8B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Pizzeria/PizzeriaBusinessLogic/ClientLogic.cs b/Pizzeria/PizzeriaBusinessLogic/ClientLogic.cs new file mode 100644 index 0000000..9309891 --- /dev/null +++ b/Pizzeria/PizzeriaBusinessLogic/ClientLogic.cs @@ -0,0 +1,123 @@ +using Microsoft.Extensions.Logging; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.BusinessLogicsContracts; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StorageContracts; +using PizzeriaContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaBusinessLogic +{ + public class ClientLogic : IClientLogic + { + private readonly ILogger _logger; + private readonly IClientStorage _clientStorage; + + public ClientLogic(ILogger logger, IClientStorage clientStorage) + { + _logger = logger; + _clientStorage = clientStorage; + } + + public List? ReadList(ClientSearchModel? model) + { + _logger.LogInformation("ReadList. Id: {Id}. ClientFIO: {ClientFIO}. Email: {Email}.", + model?.Id, model?.ClientFIO, model?.Email); + var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + + public ClientViewModel? ReadElement(ClientSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Id: {Id}. ClientFIO: {ClientFIO}. Email: {Email}.", + model?.Id, model?.ClientFIO, model?.Email); + var element = _clientStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id: {Id}", element.Id); + return element; + } + + public bool Create(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ClientBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_clientStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + 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.ClientFIO)) + { + throw new ArgumentNullException("Нет ФИО клиента", nameof(model.ClientFIO)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет логина клиента", nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля клиента", nameof(model.Password)); + } + _logger.LogInformation("Client. Id: {Id}. ClientFIO: {ClientFIO}. Email: {Email}.", + model?.Id, model?.ClientFIO, model?.Email); + var element = _clientStorage.GetElement(new ClientSearchModel { Email = model.Email }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Клиент с таким логином уже есть"); + } + } + } +} diff --git a/Pizzeria/PizzeriaContracts/BindingModels/ClientBindingModel.cs b/Pizzeria/PizzeriaContracts/BindingModels/ClientBindingModel.cs new file mode 100644 index 0000000..5710e70 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/BindingModels/ClientBindingModel.cs @@ -0,0 +1,17 @@ +using PizzeriaDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaContracts.BindingModels +{ + public class ClientBindingModel : IClientModel + { + public int Id { get; set; } + public string ClientFIO { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + } +} diff --git a/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs b/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs index ecf2e34..8664e57 100644 --- a/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs +++ b/Pizzeria/PizzeriaContracts/BindingModels/OrderBindingModel.cs @@ -10,6 +10,7 @@ namespace PizzeriaContracts.BindingModels public class OrderBindingModel : IOrderModel { public int Id { get; set; } + public int ClientId { get; set; } public int PizzaId { get; set; } public int Count { get; set; } public double Sum { get; set; } diff --git a/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IClientLogic.cs b/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IClientLogic.cs new file mode 100644 index 0000000..0868a32 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IClientLogic.cs @@ -0,0 +1,21 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaContracts.BusinessLogicsContracts +{ + public interface IClientLogic + { + List? ReadList(ClientSearchModel? model); + ClientViewModel? ReadElement(ClientSearchModel model); + bool Create(ClientBindingModel model); + bool Update(ClientBindingModel model); + bool Delete(ClientBindingModel model); + } + +} diff --git a/Pizzeria/PizzeriaContracts/SearchModels/ClientSearchModel.cs b/Pizzeria/PizzeriaContracts/SearchModels/ClientSearchModel.cs new file mode 100644 index 0000000..4c20691 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/SearchModels/ClientSearchModel.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaContracts.SearchModels +{ + public class ClientSearchModel + { + public int? Id { get; set; } + + public string? ClientFIO { get; set; } + + public string? Email { get; set; } + + public string? Password { get; set; } + } +} diff --git a/Pizzeria/PizzeriaContracts/SearchModels/OrderSearchModel.cs b/Pizzeria/PizzeriaContracts/SearchModels/OrderSearchModel.cs index b2e6ddd..0ec719c 100644 --- a/Pizzeria/PizzeriaContracts/SearchModels/OrderSearchModel.cs +++ b/Pizzeria/PizzeriaContracts/SearchModels/OrderSearchModel.cs @@ -9,6 +9,7 @@ namespace PizzeriaContracts.SearchModels public class OrderSearchModel { public int? Id { get; set; } + public int? ClientId { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } } diff --git a/Pizzeria/PizzeriaContracts/StorageContracts/IClientStorage.cs b/Pizzeria/PizzeriaContracts/StorageContracts/IClientStorage.cs new file mode 100644 index 0000000..661504a --- /dev/null +++ b/Pizzeria/PizzeriaContracts/StorageContracts/IClientStorage.cs @@ -0,0 +1,22 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaContracts.StorageContracts +{ + public interface IClientStorage + { + List GetFullList(); + List GetFilteredList(ClientSearchModel model); + ClientViewModel? GetElement(ClientSearchModel model); + ClientViewModel? Insert(ClientBindingModel model); + ClientViewModel? Update(ClientBindingModel model); + ClientViewModel? Delete(ClientBindingModel model); + } + +} diff --git a/Pizzeria/PizzeriaContracts/ViewModels/ClientViewModel.cs b/Pizzeria/PizzeriaContracts/ViewModels/ClientViewModel.cs new file mode 100644 index 0000000..41443ca --- /dev/null +++ b/Pizzeria/PizzeriaContracts/ViewModels/ClientViewModel.cs @@ -0,0 +1,22 @@ +using PizzeriaDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaContracts.ViewModels +{ + public class ClientViewModel : IClientModel + { + public int Id { get; set; } + [DisplayName("ФИО клиента")] + public string ClientFIO { get; set; } = string.Empty; + [DisplayName("Логин (эл. почта)")] + public string Email { get; set; } = string.Empty; + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + } + +} diff --git a/Pizzeria/PizzeriaContracts/ViewModels/OrderViewModel.cs b/Pizzeria/PizzeriaContracts/ViewModels/OrderViewModel.cs index 19f695c..de806a5 100644 --- a/Pizzeria/PizzeriaContracts/ViewModels/OrderViewModel.cs +++ b/Pizzeria/PizzeriaContracts/ViewModels/OrderViewModel.cs @@ -12,6 +12,10 @@ namespace PizzeriaContracts.ViewModels { [DisplayName("Номер")] public int Id { get; set; } + public int ClientId { get; set; } + + [DisplayName("Клиент")] + public string ClientFIO { get; set; } = string.Empty; public int PizzaId { get; set; } [DisplayName("Изделие")] public string PizzaName { get; set; } = string.Empty; diff --git a/Pizzeria/PizzeriaDataModels/Models/IClientModel.cs b/Pizzeria/PizzeriaDataModels/Models/IClientModel.cs new file mode 100644 index 0000000..273c2fd --- /dev/null +++ b/Pizzeria/PizzeriaDataModels/Models/IClientModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaDataModels.Models +{ + public interface IClientModel : IId + { + string ClientFIO { get; } + + string Email { get; } + + string Password { get; } + } +} diff --git a/Pizzeria/PizzeriaDataModels/Models/IOrderModel.cs b/Pizzeria/PizzeriaDataModels/Models/IOrderModel.cs index 6c88ac2..da2ca3c 100644 --- a/Pizzeria/PizzeriaDataModels/Models/IOrderModel.cs +++ b/Pizzeria/PizzeriaDataModels/Models/IOrderModel.cs @@ -8,6 +8,7 @@ namespace PizzeriaDataModels { public interface IOrderModel : IId { + int ClientId { get; } int PizzaId { get; } int Count { get; } double Sum { get; } diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/ClientStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..6bd3b18 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,90 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StorageContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StorageContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDatabaseImplement.Models; + +namespace PizzeriaDatabaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + public List GetFullList() + { + using var context = new PizzeriaDatabase(); + return context.Clients + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email)) + { + return new(); + } + using var context = new PizzeriaDatabase(); + return context.Clients + .Where(x => (!string.IsNullOrEmpty(model.ClientFIO) && x.ClientFIO.Contains(model.ClientFIO)) || + (!string.IsNullOrEmpty(model.Email) && x.ClientFIO.Contains(model.Email))) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password)) + { + return null; + } + using var context = new PizzeriaDatabase(); + return context.Clients + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || + (!string.IsNullOrEmpty(model.ClientFIO) && x.ClientFIO == model.ClientFIO) || + (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password) && x.Email == model.Email && x.Password == model.Password)) + ?.GetViewModel; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + using var context = new PizzeriaDatabase(); + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new PizzeriaDatabase(); + var client = context.Clients.FirstOrDefault(x => x.Id == model.Id); + if (client == null) + { + return null; + } + client.Update(model); + context.SaveChanges(); + return client.GetViewModel; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new PizzeriaDatabase(); + var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs index 06e44e9..9829b82 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Implements/OrderStorage.cs @@ -12,7 +12,10 @@ namespace PizzeriaDatabaseImplement.Implements public List GetFullList() { using var context = new PizzeriaDatabase(); - return context.Orders.Include(x => x.Pizza).Select(x => x.GetViewModel).ToList(); + return context.Orders.Include(x => x.Pizza) + .Include(x => x.Client) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFilteredList(OrderSearchModel model) @@ -22,7 +25,11 @@ namespace PizzeriaDatabaseImplement.Implements return new(); } using var context = new PizzeriaDatabase(); - return context.Orders.Include(x => x.Pizza).Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => x.GetViewModel).ToList(); + return context.Orders.Include(x => x.Pizza) + .Include(x => x.Client) + .Where(x => (model.DateFrom.HasValue && model.DateTo.HasValue && x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) || + (model.ClientId.HasValue && x.ClientId == model.ClientId)) + .Select(x => x.GetViewModel).ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) @@ -32,7 +39,10 @@ namespace PizzeriaDatabaseImplement.Implements return new(); } using var context = new PizzeriaDatabase(); - return context.Orders.Include(x => x.Pizza).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + return context.Orders.Include(x => x.Pizza) + .Include(x => x.Client) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (model.ClientId.HasValue && x.Id == model.ClientId))? + .GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230513164515_ClientMigration.Designer.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230513164515_ClientMigration.Designer.cs new file mode 100644 index 0000000..2382b8f --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230513164515_ClientMigration.Designer.cs @@ -0,0 +1,214 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using PizzeriaDatabaseImplement; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + [DbContext(typeof(PizzeriaDatabase))] + [Migration("20230513164515_ClientMigration")] + partial class ClientMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("PizzaId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("PizzaId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PizzaName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pizzas"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.PizzaComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PizzaId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PizzaId"); + + b.ToTable("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Order", b => + { + b.HasOne("PizzeriaDatabaseImplement.Models.Client", "Client") + .WithMany("ClientOrders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") + .WithMany("Orders") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.PizzaComponent", b => + { + b.HasOne("PizzeriaDatabaseImplement.Models.Component", "Component") + .WithMany("PizzaComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") + .WithMany("Components") + .HasForeignKey("PizzaId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pizza"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Client", b => + { + b.Navigation("ClientOrders"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b => + { + b.Navigation("PizzaComponents"); + }); + + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Pizza", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230513164515_ClientMigration.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230513164515_ClientMigration.cs new file mode 100644 index 0000000..eb4edd2 --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/20230513164515_ClientMigration.cs @@ -0,0 +1,69 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PizzeriaDatabaseImplement.Migrations +{ + /// + public partial class ClientMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.Sql("DELETE FROM [dbo].[Orders]"); + migrationBuilder.AddColumn( + name: "ClientId", + table: "Orders", + type: "int", + nullable: false, + defaultValue: 0); + + migrationBuilder.CreateTable( + name: "Clients", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ClientFIO = table.Column(type: "nvarchar(max)", nullable: false), + Email = table.Column(type: "nvarchar(max)", nullable: false), + Password = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Clients", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ClientId", + table: "Orders", + column: "ClientId"); + + migrationBuilder.AddForeignKey( + name: "FK_Orders_Clients_ClientId", + table: "Orders", + column: "ClientId", + principalTable: "Clients", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Orders_Clients_ClientId", + table: "Orders"); + + migrationBuilder.DropTable( + name: "Clients"); + + migrationBuilder.DropIndex( + name: "IX_Orders_ClientId", + table: "Orders"); + + migrationBuilder.DropColumn( + name: "ClientId", + table: "Orders"); + } + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs b/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs index bba534a..85a6e87 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Migrations/PizzeriaDatabaseModelSnapshot.cs @@ -22,6 +22,31 @@ namespace PizzeriaDatabaseImplement.Migrations SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b => { b.Property("Id") @@ -50,6 +75,9 @@ namespace PizzeriaDatabaseImplement.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + b.Property("ClientId") + .HasColumnType("int"); + b.Property("Count") .HasColumnType("int"); @@ -70,6 +98,8 @@ namespace PizzeriaDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("ClientId"); + b.HasIndex("PizzaId"); b.ToTable("Orders"); @@ -123,12 +153,20 @@ namespace PizzeriaDatabaseImplement.Migrations modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Order", b => { + b.HasOne("PizzeriaDatabaseImplement.Models.Client", "Client") + .WithMany("ClientOrders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza") .WithMany("Orders") .HasForeignKey("PizzaId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.Navigation("Client"); + b.Navigation("Pizza"); }); @@ -151,6 +189,11 @@ namespace PizzeriaDatabaseImplement.Migrations b.Navigation("Pizza"); }); + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Client", b => + { + b.Navigation("ClientOrders"); + }); + modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b => { b.Navigation("PizzaComponents"); diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Client.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Client.cs new file mode 100644 index 0000000..7a2dd2b --- /dev/null +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Client.cs @@ -0,0 +1,64 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaDatabaseImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; set; } + + [Required] + public string ClientFIO { get; set; } = string.Empty; + + [Required] + public string Email { get; set; } = string.Empty; + + [Required] + public string Password { get; set; } = string.Empty; + + [ForeignKey("ClientId")] + public virtual List ClientOrders { get; set; } = new(); + + public static Client? Create(ClientBindingModel model) + { + if (model == null) + { + return null; + } + return new Client() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }; + } + + public void Update(ClientBindingModel model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password + }; + } +} diff --git a/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs b/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs index cc8f570..28327f3 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs @@ -13,6 +13,10 @@ namespace PizzeriaDatabaseImplement.Models public class Order { public int Id { get; private set; } + [Required] + public int ClientId { get; private set; } + + public virtual Client Client { get; set; } = new(); [Required] public int PizzaId { get; private set; } @@ -38,6 +42,8 @@ namespace PizzeriaDatabaseImplement.Models return new Order() { Id = model.Id, + ClientId = model.ClientId, + Client = context.Clients.First(x => model.ClientId == x.Id), PizzaId = model.PizzaId, Pizza = context.Pizzas.First(x => x.Id == model.PizzaId), Count = model.Count, @@ -61,6 +67,8 @@ namespace PizzeriaDatabaseImplement.Models public OrderViewModel GetViewModel => new() { Id = Id, + ClientId = ClientId, + ClientFIO = Client.ClientFIO, PizzaId = PizzaId, PizzaName = Pizza.PizzaName, Count = Count, diff --git a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs index ca4e930..e9e02df 100644 --- a/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs +++ b/Pizzeria/PizzeriaDatabaseImplement/PizzeriaDatabase.cs @@ -14,6 +14,7 @@ namespace PizzeriaDatabaseImplement } base.OnConfiguring(optionsBuilder); } + public virtual DbSet Clients { set; get; } public virtual DbSet Components { set; get; } public virtual DbSet Pizzas { set; get; } public virtual DbSet PizzaComponents { set; get; } diff --git a/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs b/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs index c2717ac..1a8591c 100644 --- a/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs +++ b/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs @@ -9,9 +9,11 @@ namespace PizzeriaFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string PizzaFileName = "Pizza.xml"; + private readonly string ClientFileName = "Client.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Pizzas { get; private set; } + public List Clients { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -23,11 +25,13 @@ namespace PizzeriaFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SavePizzas() => SaveData(Pizzas, PizzaFileName, "Pizzas", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Pizzas = LoadData(PizzaFileName, "Pizza", x => Pizza.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/Pizzeria/PizzeriaFileImplement/Implements/ClientStorage.cs b/Pizzeria/PizzeriaFileImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..3f2c23f --- /dev/null +++ b/Pizzeria/PizzeriaFileImplement/Implements/ClientStorage.cs @@ -0,0 +1,93 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StorageContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaFileImplement.Implements +{ + public class ClientStorage : IClientStorage + { + private readonly DataFileSingleton source; + + public ClientStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Clients + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email)) + { + return new(); + } + return source.Clients + .Where(x => (!string.IsNullOrEmpty(model.ClientFIO) && x.ClientFIO.Contains(model.ClientFIO)) || + (!string.IsNullOrEmpty(model.Email) && x.ClientFIO.Contains(model.Email))) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password)) + { + return null; + } + return source.Clients + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || + (!string.IsNullOrEmpty(model.ClientFIO) && x.ClientFIO == model.ClientFIO) || + (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password) && x.Email == model.Email && x.Password == model.Password)) + ?.GetViewModel; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + model.Id = source.Components.Count > 0 ? source.Components.Max(x => x.Id) + 1 : 1; + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + source.Clients.Add(newClient); + source.SaveClients(); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + var client = source.Clients.FirstOrDefault(x => x.Id == model.Id); + if (client == null) + { + return null; + } + client.Update(model); + source.SaveClients(); + return client.GetViewModel; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + var element = source.Clients.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Clients.Remove(element); + source.SaveClients(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs index fe96028..453253d 100644 --- a/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs +++ b/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs @@ -13,22 +13,22 @@ namespace PizzeriaFileImplement.Implements { source = DataFileSingleton.GetInstance(); } - public List GetFullList() => source.Orders.Select(x => AttachPizzaName(x.GetViewModel)).ToList(); + public List GetFullList() => source.Orders.Select(x => AttachOthersNames(x.GetViewModel)).ToList(); public List GetFilteredList(OrderSearchModel model) { - if (!model.DateFrom.HasValue || !model.DateTo.HasValue) + if ((!model.DateFrom.HasValue || !model.DateTo.HasValue) && !model.ClientId.HasValue) { return new(); } - return source.Orders.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => AttachPizzaName(x.GetViewModel)).ToList(); + return source.Orders.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => AttachOthersNames(x.GetViewModel)).ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue && !model.ClientId.HasValue) { return new(); } - return AttachPizzaName(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); + return AttachOthersNames(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); } public OrderViewModel? Insert(OrderBindingModel model) { @@ -40,7 +40,7 @@ namespace PizzeriaFileImplement.Implements } source.Orders.Add(newOrder); source.SaveOrders(); - return AttachPizzaName(newOrder.GetViewModel); + return AttachOthersNames(newOrder.GetViewModel); } public OrderViewModel? Update(OrderBindingModel model) { @@ -51,7 +51,7 @@ namespace PizzeriaFileImplement.Implements } order.Update(model); source.SaveOrders(); - return AttachPizzaName(order.GetViewModel); + return AttachOthersNames(order.GetViewModel); } public OrderViewModel? Delete(OrderBindingModel model) { @@ -60,17 +60,22 @@ namespace PizzeriaFileImplement.Implements { source.Orders.Remove(order); source.SaveOrders(); - return AttachPizzaName(order.GetViewModel); + return AttachOthersNames(order.GetViewModel); } return null; } - private OrderViewModel? AttachPizzaName(OrderViewModel? model) + private OrderViewModel AttachOthersNames(OrderViewModel model) { if (model == null) { return null; } + var clientFIO = source.Clients.FirstOrDefault(x => model.ClientId == x.Id)?.ClientFIO; var pizza = source.Pizzas.FirstOrDefault(x => x.Id == model.PizzaId); + if (clientFIO != null) + { + model.ClientFIO = clientFIO; + } if (pizza != null) { model.PizzaName = pizza.PizzaName; diff --git a/Pizzeria/PizzeriaFileImplement/Models/Client.cs b/Pizzeria/PizzeriaFileImplement/Models/Client.cs new file mode 100644 index 0000000..9c80bd8 --- /dev/null +++ b/Pizzeria/PizzeriaFileImplement/Models/Client.cs @@ -0,0 +1,75 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace PizzeriaFileImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + public string ClientFIO { get; private set; } = string.Empty; + public string Email { get; private set; } = string.Empty; + public string Password { get; private set; } = string.Empty; + + public static Client? Create(ClientBindingModel model) + { + if (model == null) + { + return null; + } + return new Client() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }; + } + + public static Client? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Client() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ClientFIO = element.Element("ClientFIO")!.Value, + Email = element.Element("Email")!.Value, + Password = element.Element("Password")!.Value + }; + } + + public void Update(ClientBindingModel model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password + }; + + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("ClientFIO", ClientFIO), + new XElement("Email", Email), + new XElement("Password", Password)); + } +} diff --git a/Pizzeria/PizzeriaFileImplement/Models/Order.cs b/Pizzeria/PizzeriaFileImplement/Models/Order.cs index 3528349..fc406dc 100644 --- a/Pizzeria/PizzeriaFileImplement/Models/Order.cs +++ b/Pizzeria/PizzeriaFileImplement/Models/Order.cs @@ -8,6 +8,7 @@ namespace PizzeriaFileImplement.Models public class Order : IOrderModel { public int Id { get; private set; } + public int ClientId { get; private set; } public int PizzaId { get; private set; } public int Count { get; private set; } public double Sum { get; private set; } @@ -23,6 +24,7 @@ namespace PizzeriaFileImplement.Models return new Order() { Id = model.Id, + ClientId = model.ClientId, PizzaId = model.PizzaId, Count = model.Count, Sum = model.Sum, @@ -41,6 +43,7 @@ namespace PizzeriaFileImplement.Models return new Order() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ClientId= Convert.ToInt32(element.Attribute("ClientId")!.Value), PizzaId = Convert.ToInt32(element.Element("PizzaId")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), @@ -62,6 +65,7 @@ namespace PizzeriaFileImplement.Models public OrderViewModel GetViewModel => new() { Id = Id, + ClientId = ClientId, PizzaId = PizzaId, Count = Count, Sum = Sum, @@ -72,6 +76,7 @@ namespace PizzeriaFileImplement.Models public XElement GetXElement => new("Order", new XAttribute("Id", Id), + new XElement("ClientId",ClientId.ToString()), new XElement("PizzaId", PizzaId.ToString()), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), diff --git a/Pizzeria/PizzeriaListImplement/DataListSingleton.cs b/Pizzeria/PizzeriaListImplement/DataListSingleton.cs index c9909f5..cb6a75e 100644 --- a/Pizzeria/PizzeriaListImplement/DataListSingleton.cs +++ b/Pizzeria/PizzeriaListImplement/DataListSingleton.cs @@ -13,11 +13,13 @@ namespace PizzeriaListImplement public List Components { get; set; } public List Orders { get; set; } public List Pizzas { get; set; } + public List Clients { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Pizzas = new List(); + Clients = new List(); } public static DataListSingleton GetInstance() { diff --git a/Pizzeria/PizzeriaListImplement/Implements/ClientStorage.cs b/Pizzeria/PizzeriaListImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..1943ed6 --- /dev/null +++ b/Pizzeria/PizzeriaListImplement/Implements/ClientStorage.cs @@ -0,0 +1,115 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StorageContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaListImplement.Implements +{ + public class ClientStorage : IClientStorage + { + private readonly DataListSingleton _source; + + public ClientStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + var result = new List(); + foreach (var client in _source.Clients) + { + result.Add(client.GetViewModel); + } + return result; + } + + public List GetFilteredList(ClientSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email)) + { + return result; + } + foreach (var client in _source.Clients) + { + if ((!string.IsNullOrEmpty(model.ClientFIO) && client.ClientFIO.Contains(model.ClientFIO)) || + (!string.IsNullOrEmpty(model.Email) && client.ClientFIO.Contains(model.Email))) + { + result.Add(client.GetViewModel); + } + } + return result; + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password)) + { + return null; + } + foreach (var client in _source.Clients) + { + if ((model.Id.HasValue && client.Id == model.Id) || + (!string.IsNullOrEmpty(model.ClientFIO) && client.ClientFIO == model.ClientFIO) || + (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password) && client.Email == model.Email && client.Password == model.Password)) + { + return client.GetViewModel; + } + } + return null; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + model.Id = 1; + foreach (var client in _source.Clients) + { + if (model.Id <= client.Id) + { + model.Id = client.Id + 1; + } + } + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + _source.Clients.Add(newClient); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + foreach (var client in _source.Clients) + { + if (client.Id == model.Id) + { + client.Update(model); + return client.GetViewModel; + } + } + return null; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + for (int i = 0; i < _source.Clients.Count; ++i) + { + if (_source.Clients[i].Id == model.Id) + { + var element = _source.Clients[i]; + _source.Clients.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/Pizzeria/PizzeriaListImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaListImplement/Implements/OrderStorage.cs index c37fa3e..eb8b441 100644 --- a/Pizzeria/PizzeriaListImplement/Implements/OrderStorage.cs +++ b/Pizzeria/PizzeriaListImplement/Implements/OrderStorage.cs @@ -23,22 +23,23 @@ namespace PizzeriaListImplement.Implements var result = new List(); foreach (var order in _source.Orders) { - result.Add(AttachPizzaName(order.GetViewModel)); + result.Add(AttachOthersNames(order.GetViewModel)); } return result; } public List GetFilteredList(OrderSearchModel model) { var result = new List(); - if (model == null || !model.DateFrom.HasValue || !model.DateFrom.HasValue) + if ((!model.DateFrom.HasValue || !model.DateTo.HasValue) && !model.ClientId.HasValue) { return result; } foreach (var order in _source.Orders) { - if (order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo) + if ((model.DateFrom.HasValue && model.DateTo.HasValue && order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo) || + (model.ClientId.HasValue && order.ClientId == model.ClientId)) { - result.Add(AttachPizzaName(order.GetViewModel)); + result.Add(AttachOthersNames(order.GetViewModel)); } } return result; @@ -54,7 +55,7 @@ namespace PizzeriaListImplement.Implements { if (model.Id.HasValue && order.Id == model.Id) { - return AttachPizzaName(order.GetViewModel); + return AttachOthersNames(order.GetViewModel); } } return null; @@ -76,7 +77,7 @@ namespace PizzeriaListImplement.Implements return null; } _source.Orders.Add(newOrder); - return AttachPizzaName(newOrder.GetViewModel); + return AttachOthersNames(newOrder.GetViewModel); } public OrderViewModel? Update(OrderBindingModel model) { @@ -85,7 +86,7 @@ namespace PizzeriaListImplement.Implements if (order.Id == model.Id) { order.Update(model); - return AttachPizzaName(order.GetViewModel); + return AttachOthersNames(order.GetViewModel); } } return null; @@ -98,13 +99,21 @@ namespace PizzeriaListImplement.Implements { var element = _source.Orders[i]; _source.Orders.RemoveAt(i); - return AttachPizzaName(element.GetViewModel); + return AttachOthersNames(element.GetViewModel); } } return null; } - private OrderViewModel AttachPizzaName(OrderViewModel model) + private OrderViewModel AttachOthersNames(OrderViewModel model) { + foreach (var order in _source.Clients) + { + if (model.ClientId == order.Id) + { + model.ClientFIO = order.ClientFIO; + break; + } + } foreach (var pizza in _source.Pizzas) { if (pizza.Id == model.PizzaId) diff --git a/Pizzeria/PizzeriaListImplement/Models/Client.cs b/Pizzeria/PizzeriaListImplement/Models/Client.cs new file mode 100644 index 0000000..06bebf2 --- /dev/null +++ b/Pizzeria/PizzeriaListImplement/Models/Client.cs @@ -0,0 +1,53 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaListImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + public string ClientFIO { get; private set; } = string.Empty; + public string Email { get; private set; } = string.Empty; + public string Password { get; private set; } = string.Empty; + + public static Client? Create(ClientBindingModel? model) + { + if (model == null) + { + return null; + } + return new Client() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }; + } + + public void Update(ClientBindingModel? model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password + }; + } +} diff --git a/Pizzeria/PizzeriaListImplement/Models/Order.cs b/Pizzeria/PizzeriaListImplement/Models/Order.cs index ae0951e..553c08a 100644 --- a/Pizzeria/PizzeriaListImplement/Models/Order.cs +++ b/Pizzeria/PizzeriaListImplement/Models/Order.cs @@ -12,6 +12,7 @@ namespace PizzeriaListImplement.Models public class Order : IOrderModel { public int Id { get; private set; } + public int ClientId { get; private set; } public int PizzaId { get; private set; } public int Count { get; private set; } public double Sum { get; private set; } @@ -27,6 +28,7 @@ namespace PizzeriaListImplement.Models return new Order() { Id = model.Id, + ClientId = model.ClientId, PizzaId = model.PizzaId, Count = model.Count, Sum = model.Sum, @@ -47,6 +49,7 @@ namespace PizzeriaListImplement.Models public OrderViewModel GetViewModel => new() { Id = Id, + ClientId = ClientId, PizzaId = PizzaId, Count = Count, Sum = Sum, diff --git a/Pizzeria/PizzeriaRestApi/Controllers/ClientController.cs b/Pizzeria/PizzeriaRestApi/Controllers/ClientController.cs new file mode 100644 index 0000000..063e6cf --- /dev/null +++ b/Pizzeria/PizzeriaRestApi/Controllers/ClientController.cs @@ -0,0 +1,65 @@ +using Microsoft.AspNetCore.Mvc; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.BusinessLogicsContracts; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.ViewModels; + +namespace PizzeriaRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ClientController : Controller + { + private readonly ILogger _logger; + private readonly IClientLogic _logic; + public ClientController(IClientLogic logic, ILogger + logger) + { + _logger = logger; + _logic = logic; + } + [HttpGet] + public ClientViewModel? Login(string login, string password) + { + try + { + return _logic.ReadElement(new ClientSearchModel + { + Email = login, + Password = password + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка входа в систему"); + throw; + } + } + [HttpPost] + public void Register(ClientBindingModel model) + { + try + { + _logic.Create(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка регистрации"); + throw; + } + } + [HttpPost] + public void UpdateData(ClientBindingModel model) + { + try + { + _logic.Update(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка обновления данных"); + throw; + } + } + } +} diff --git a/Pizzeria/PizzeriaRestApi/Controllers/MainController.cs b/Pizzeria/PizzeriaRestApi/Controllers/MainController.cs new file mode 100644 index 0000000..b0708c5 --- /dev/null +++ b/Pizzeria/PizzeriaRestApi/Controllers/MainController.cs @@ -0,0 +1,85 @@ +using DocumentFormat.OpenXml.Office2010.Excel; +using Microsoft.AspNetCore.Mvc; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.BusinessLogicsContracts; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.ViewModels; + +namespace PizzeriaRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class MainController : Controller + { + private readonly ILogger _logger; + private readonly IOrderLogic _order; + private readonly IPizzaLogic _pizza; + public MainController(ILogger logger, IOrderLogic order,IPizzaLogic pizza) + { + _logger = logger; + _order = order; + _pizza = pizza; + } + [HttpGet] + public List? GetPizzaList() + { + try + { + return _pizza.ReadList(null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка пицц"); + throw; + } + } + [HttpGet] + public PizzaViewModel? GetPizza(int productId) + { + try + { + return _pizza.ReadElement(new PizzaSearchModel + { + Id = + productId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения пиццы по id={Id}", + productId); + throw; + } + } + [HttpGet] + public List? GetOrders(int clientId) + { + try + { + return _order.ReadList(new OrderSearchModel + { + ClientId = clientId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка заказов клиента id ={ Id}", clientId); + throw; + } + } + [HttpPost] + public void CreateOrder(OrderBindingModel model) + { + try + { + _order.CreateOrder(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания заказа"); + throw; + } + } + } + +} diff --git a/Pizzeria/PizzeriaRestApi/PizzeriaRestApi.csproj b/Pizzeria/PizzeriaRestApi/PizzeriaRestApi.csproj new file mode 100644 index 0000000..c32ed70 --- /dev/null +++ b/Pizzeria/PizzeriaRestApi/PizzeriaRestApi.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + diff --git a/Pizzeria/PizzeriaRestApi/Program.cs b/Pizzeria/PizzeriaRestApi/Program.cs new file mode 100644 index 0000000..595dc25 --- /dev/null +++ b/Pizzeria/PizzeriaRestApi/Program.cs @@ -0,0 +1,51 @@ +using Microsoft.OpenApi.Models; +using PizzeriaBusinessLogic; +using PizzeriaContracts.BusinessLogicsContracts; +using PizzeriaContracts.StorageContracts; +using PizzeriaDatabaseImplement.Implements; + +var builder = WebApplication.CreateBuilder(args); + +builder.Logging.SetMinimumLevel(LogLevel.Trace); +builder.Logging.AddLog4Net("log4net.config"); + +// Add services to the container. + +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(c => +{ + c.SwaggerDoc("v1", new OpenApiInfo + { + Title = "AbstractShopRestApi", + Version + = "v1" + }); +}); + + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json","AbstractShopRestApi v1")); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/Pizzeria/PizzeriaRestApi/Properties/launchSettings.json b/Pizzeria/PizzeriaRestApi/Properties/launchSettings.json new file mode 100644 index 0000000..b1d5f41 --- /dev/null +++ b/Pizzeria/PizzeriaRestApi/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:53758", + "sslPort": 44380 + } + }, + "profiles": { + "PizzeriaRestApi": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7245;http://localhost:5245", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Pizzeria/PizzeriaRestApi/appsettings.Development.json b/Pizzeria/PizzeriaRestApi/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Pizzeria/PizzeriaRestApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Pizzeria/PizzeriaRestApi/appsettings.json b/Pizzeria/PizzeriaRestApi/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/Pizzeria/PizzeriaRestApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Pizzeria/PizzeriaRestApi/log4net.config b/Pizzeria/PizzeriaRestApi/log4net.config new file mode 100644 index 0000000..43b77f9 --- /dev/null +++ b/Pizzeria/PizzeriaRestApi/log4net.config @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Pizzeria/PizzeriaView/FormClients.Designer.cs b/Pizzeria/PizzeriaView/FormClients.Designer.cs new file mode 100644 index 0000000..f690228 --- /dev/null +++ b/Pizzeria/PizzeriaView/FormClients.Designer.cs @@ -0,0 +1,92 @@ +namespace Pizzeria +{ + 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() + { + dataGridViewClients = new DataGridView(); + buttonDel = new Button(); + buttonRef = new Button(); + ((System.ComponentModel.ISupportInitialize)dataGridViewClients).BeginInit(); + SuspendLayout(); + // + // dataGridViewClients + // + dataGridViewClients.BackgroundColor = SystemColors.ControlLightLight; + dataGridViewClients.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridViewClients.Location = new Point(1, 1); + dataGridViewClients.Name = "dataGridViewClients"; + dataGridViewClients.RowHeadersVisible = false; + dataGridViewClients.RowHeadersWidth = 51; + dataGridViewClients.RowTemplate.Height = 29; + dataGridViewClients.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridViewClients.Size = new Size(650, 402); + dataGridViewClients.TabIndex = 0; + // + // buttonDel + // + buttonDel.Location = new Point(668, 12); + buttonDel.Name = "buttonDel"; + buttonDel.Size = new Size(102, 29); + buttonDel.TabIndex = 1; + buttonDel.Text = "Удалить"; + buttonDel.UseVisualStyleBackColor = true; + buttonDel.Click += ButtonDel_Click; + // + // buttonRef + // + buttonRef.Location = new Point(668, 47); + buttonRef.Name = "buttonRef"; + buttonRef.Size = new Size(102, 29); + buttonRef.TabIndex = 2; + buttonRef.Text = "Обновить"; + buttonRef.UseVisualStyleBackColor = true; + buttonRef.Click += ButtonRef_Click; + // + // FormClients + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(782, 403); + Controls.Add(buttonRef); + Controls.Add(buttonDel); + Controls.Add(dataGridViewClients); + Name = "FormClients"; + StartPosition = FormStartPosition.CenterParent; + Text = "Клиенты"; + Load += FormClients_Load; + ((System.ComponentModel.ISupportInitialize)dataGridViewClients).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridViewClients; + private Button buttonDel; + private Button buttonRef; + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaView/FormClients.cs b/Pizzeria/PizzeriaView/FormClients.cs new file mode 100644 index 0000000..20bbb82 --- /dev/null +++ b/Pizzeria/PizzeriaView/FormClients.cs @@ -0,0 +1,75 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; + +namespace Pizzeria +{ + public partial class FormClients : Form + { + private readonly ILogger _logger; + private readonly IClientLogic _logic; + + public FormClients(ILogger logger, IClientLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormClients_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridViewClients.DataSource = list; + dataGridViewClients.Columns["Id"].Visible = false; + dataGridViewClients.Columns["ClientFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка клиентов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки клиентов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonDel_Click(object sender, EventArgs e) + { + if (dataGridViewClients.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == + DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridViewClients.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление клиента"); + try + { + if (!_logic.Delete(new ClientBindingModel { Id = id })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления клиента"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/Pizzeria/PizzeriaView/FormClients.resx b/Pizzeria/PizzeriaView/FormClients.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Pizzeria/PizzeriaView/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/Pizzeria/PizzeriaView/FormMain.Designer.cs b/Pizzeria/PizzeriaView/FormMain.Designer.cs index 932d4a8..e817c36 100644 --- a/Pizzeria/PizzeriaView/FormMain.Designer.cs +++ b/Pizzeria/PizzeriaView/FormMain.Designer.cs @@ -42,6 +42,7 @@ this.buttonOrderReady = new System.Windows.Forms.Button(); this.buttonIssuedOrder = new System.Windows.Forms.Button(); this.buttonRef = new System.Windows.Forms.Button(); + this.clientsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -63,7 +64,8 @@ // this.bookToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.ingridientsToolStripMenuItem, - this.pizzasToolStripMenuItem}); + this.pizzasToolStripMenuItem, + this.clientsToolStripMenuItem}); this.bookToolStripMenuItem.Name = "bookToolStripMenuItem"; this.bookToolStripMenuItem.Size = new System.Drawing.Size(87, 20); this.bookToolStripMenuItem.Text = "Справочник"; @@ -71,14 +73,14 @@ // ingridientsToolStripMenuItem // this.ingridientsToolStripMenuItem.Name = "ingridientsToolStripMenuItem"; - this.ingridientsToolStripMenuItem.Size = new System.Drawing.Size(148, 22); + this.ingridientsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.ingridientsToolStripMenuItem.Text = "Ингредиенты"; this.ingridientsToolStripMenuItem.Click += new System.EventHandler(this.IngridentsToolStripMenuItem_Click); // // pizzasToolStripMenuItem // this.pizzasToolStripMenuItem.Name = "pizzasToolStripMenuItem"; - this.pizzasToolStripMenuItem.Size = new System.Drawing.Size(148, 22); + this.pizzasToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.pizzasToolStripMenuItem.Text = "Пиццы"; this.pizzasToolStripMenuItem.Click += new System.EventHandler(this.PizzasToolStripMenuItem_Click); // @@ -182,6 +184,13 @@ this.buttonRef.UseVisualStyleBackColor = true; this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); // + // clientsToolStripMenuItem + // + this.clientsToolStripMenuItem.Name = "clientsToolStripMenuItem"; + this.clientsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.clientsToolStripMenuItem.Text = "Клиенты"; + this.clientsToolStripMenuItem.Click += new System.EventHandler(this.ClientsToolStripMenuItem_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -223,5 +232,6 @@ private ToolStripMenuItem ингредиентыToolStripMenuItem; private ToolStripMenuItem ингредиентыПоПиццамToolStripMenuItem; private ToolStripMenuItem списокЗаказовToolStripMenuItem; + private ToolStripMenuItem clientsToolStripMenuItem; } } \ No newline at end of file diff --git a/Pizzeria/PizzeriaView/FormMain.cs b/Pizzeria/PizzeriaView/FormMain.cs index 358cb95..60a2e69 100644 --- a/Pizzeria/PizzeriaView/FormMain.cs +++ b/Pizzeria/PizzeriaView/FormMain.cs @@ -10,6 +10,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using Pizzeria; namespace PizzeriaView { @@ -38,6 +39,7 @@ namespace PizzeriaView { dataGridView.DataSource = list; dataGridView.Columns["PizzaId"].Visible = false; + dataGridView.Columns["ClientId"].Visible = false; dataGridView.Columns["PizzaName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } @@ -49,6 +51,14 @@ namespace PizzeriaView MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } + private void ClientsToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormClients)); + if (service is FormClients form) + { + form.ShowDialog(); + } + } private void IngridentsToolStripMenuItem_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormComponents)); diff --git a/Pizzeria/PizzeriaView/Program.cs b/Pizzeria/PizzeriaView/Program.cs index 07c4005..ca4a6c8 100644 --- a/Pizzeria/PizzeriaView/Program.cs +++ b/Pizzeria/PizzeriaView/Program.cs @@ -9,6 +9,7 @@ using PizzeriaContracts.BusinessLogicsContracts; using PizzeriaContracts.StorageContracts; using PizzeriaDatabaseImplement.Implements; using System; +using Pizzeria; namespace PizzeriaView { @@ -37,12 +38,15 @@ namespace PizzeriaView option.SetMinimumLevel(LogLevel.Information); option.AddNLog("nlog.config"); }); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + + services.AddTransient(); services.AddTransient(); - services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -51,6 +55,7 @@ namespace PizzeriaView services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient();