From 12b16be979bea0dc616d7cf35f6c0d105256d135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Sat, 20 Apr 2024 23:00:09 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=20=D0=BD?= =?UTF-8?q?=D0=B5=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=B0=205=20=D0=BD=D0=BE=20=D1=8F=20=D0=BF=D1=8B?= =?UTF-8?q?=D1=82=D0=B0=D0=BB=D0=B0=D1=81=D1=8C=20=D0=BA=D0=B0=D0=BA=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=B3=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/ClientBindingModel.cs | 17 +++ .../BindingModels/OrderBindingModel.cs | 1 + .../BusinessLogicsContracts/IClientLogic.cs | 20 +++ .../SearchModels/ClientSearchModel.cs | 16 +++ .../SearchModels/OrderSearchModel.cs | 1 + .../StoragesContracts/IClientStorage.cs | 21 +++ .../ViewModels/ClientViewModel.cs | 21 +++ .../ViewModels/OrderViewModel.cs | 4 + .../Models/IClientModel.cs | 15 +++ .../Models/IOrderModel.cs | 1 + .../BusinessLogics/ClientLogic.cs | 125 ++++++++++++++++++ .../ConfectioneryDatabase.cs | 1 + .../Implements/ClientStorage.cs | 88 ++++++++++++ .../Implements/OrderStorage.cs | 30 ++++- .../Models/Client.cs | 57 ++++++++ .../Models/Order.cs | 8 +- .../DataFileSingleton.cs | 10 +- .../Implements/ClientStorage.cs | 91 +++++++++++++ .../Implements/OrderStorage.cs | 50 ++++--- .../Models/Client.cs | 79 +++++++++++ .../Models/Order.cs | 6 + .../DataListSingleton.cs | 7 +- .../Implements/ClientStorage.cs | 114 ++++++++++++++++ .../Implements/OrderStorage.cs | 44 ++++-- .../Models/Client.cs | 56 ++++++++ .../Models/Component.cs | 2 +- .../Models/Order.cs | 6 +- .../Models/Pastry.cs | 2 +- .../ConfectioneryView/FormClients.Designer.cs | 99 ++++++++++++++ .../ConfectioneryView/FormClients.cs | 84 ++++++++++++ .../ConfectioneryView/FormClients.resx | 60 +++++++++ 31 files changed, 1087 insertions(+), 49 deletions(-) create mode 100644 Confectionery/ConfectionaryContracts/BindingModels/ClientBindingModel.cs create mode 100644 Confectionery/ConfectionaryContracts/BusinessLogicsContracts/IClientLogic.cs create mode 100644 Confectionery/ConfectionaryContracts/SearchModels/ClientSearchModel.cs create mode 100644 Confectionery/ConfectionaryContracts/StoragesContracts/IClientStorage.cs create mode 100644 Confectionery/ConfectionaryContracts/ViewModels/ClientViewModel.cs create mode 100644 Confectionery/ConfectionaryDataModels/Models/IClientModel.cs create mode 100644 Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Implements/ClientStorage.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/Client.cs create mode 100644 Confectionery/ConfectioneryFileImplement/Implements/ClientStorage.cs create mode 100644 Confectionery/ConfectioneryFileImplement/Models/Client.cs create mode 100644 Confectionery/ConfectioneryListImplement/Implements/ClientStorage.cs create mode 100644 Confectionery/ConfectioneryListImplement/Models/Client.cs create mode 100644 Confectionery/ConfectioneryView/FormClients.Designer.cs create mode 100644 Confectionery/ConfectioneryView/FormClients.cs create mode 100644 Confectionery/ConfectioneryView/FormClients.resx diff --git a/Confectionery/ConfectionaryContracts/BindingModels/ClientBindingModel.cs b/Confectionery/ConfectionaryContracts/BindingModels/ClientBindingModel.cs new file mode 100644 index 0000000..5f1edc9 --- /dev/null +++ b/Confectionery/ConfectionaryContracts/BindingModels/ClientBindingModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryDataModels.Models; + +namespace ConfectioneryContracts.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/Confectionery/ConfectionaryContracts/BindingModels/OrderBindingModel.cs b/Confectionery/ConfectionaryContracts/BindingModels/OrderBindingModel.cs index ad6b29d..5bca2c5 100644 --- a/Confectionery/ConfectionaryContracts/BindingModels/OrderBindingModel.cs +++ b/Confectionery/ConfectionaryContracts/BindingModels/OrderBindingModel.cs @@ -12,6 +12,7 @@ namespace ConfectioneryContracts.BindingModels { public int Id { get; set; } public int PastryId { get; set; } + public int ClientId { get; set; } public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; diff --git a/Confectionery/ConfectionaryContracts/BusinessLogicsContracts/IClientLogic.cs b/Confectionery/ConfectionaryContracts/BusinessLogicsContracts/IClientLogic.cs new file mode 100644 index 0000000..a9eed99 --- /dev/null +++ b/Confectionery/ConfectionaryContracts/BusinessLogicsContracts/IClientLogic.cs @@ -0,0 +1,20 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryContracts.SearchModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.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/Confectionery/ConfectionaryContracts/SearchModels/ClientSearchModel.cs b/Confectionery/ConfectionaryContracts/SearchModels/ClientSearchModel.cs new file mode 100644 index 0000000..b8d9f8c --- /dev/null +++ b/Confectionery/ConfectionaryContracts/SearchModels/ClientSearchModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.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/Confectionery/ConfectionaryContracts/SearchModels/OrderSearchModel.cs b/Confectionery/ConfectionaryContracts/SearchModels/OrderSearchModel.cs index 9c4821b..d636d99 100644 --- a/Confectionery/ConfectionaryContracts/SearchModels/OrderSearchModel.cs +++ b/Confectionery/ConfectionaryContracts/SearchModels/OrderSearchModel.cs @@ -9,6 +9,7 @@ namespace ConfectioneryContracts.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/Confectionery/ConfectionaryContracts/StoragesContracts/IClientStorage.cs b/Confectionery/ConfectionaryContracts/StoragesContracts/IClientStorage.cs new file mode 100644 index 0000000..7979433 --- /dev/null +++ b/Confectionery/ConfectionaryContracts/StoragesContracts/IClientStorage.cs @@ -0,0 +1,21 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryContracts.SearchModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.StoragesContracts +{ + 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/Confectionery/ConfectionaryContracts/ViewModels/ClientViewModel.cs b/Confectionery/ConfectionaryContracts/ViewModels/ClientViewModel.cs new file mode 100644 index 0000000..ad7ba69 --- /dev/null +++ b/Confectionery/ConfectionaryContracts/ViewModels/ClientViewModel.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryDataModels.Models; +using System.ComponentModel; + +namespace ConfectioneryContracts.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/Confectionery/ConfectionaryContracts/ViewModels/OrderViewModel.cs b/Confectionery/ConfectionaryContracts/ViewModels/OrderViewModel.cs index 679b9f0..4563061 100644 --- a/Confectionery/ConfectionaryContracts/ViewModels/OrderViewModel.cs +++ b/Confectionery/ConfectionaryContracts/ViewModels/OrderViewModel.cs @@ -16,6 +16,10 @@ namespace ConfectioneryContracts.ViewModels public int PastryId { get; set; } [DisplayName("Выпечка")] public string PastryName { get; set; } = string.Empty; + public int ClientId { get; set; } + + [DisplayName("ФИО клиента")] + public string ClientFIO { get; set; } = string.Empty; [DisplayName("Количество")] public int Count { get; set; } [DisplayName("Сумма")] diff --git a/Confectionery/ConfectionaryDataModels/Models/IClientModel.cs b/Confectionery/ConfectionaryDataModels/Models/IClientModel.cs new file mode 100644 index 0000000..004fd69 --- /dev/null +++ b/Confectionery/ConfectionaryDataModels/Models/IClientModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataModels.Models +{ + public interface IClientModel : IId + { + string ClientFIO { get; } + string Email { get; } + string Password { get; } + } +} diff --git a/Confectionery/ConfectionaryDataModels/Models/IOrderModel.cs b/Confectionery/ConfectionaryDataModels/Models/IOrderModel.cs index 40e889f..97508bb 100644 --- a/Confectionery/ConfectionaryDataModels/Models/IOrderModel.cs +++ b/Confectionery/ConfectionaryDataModels/Models/IOrderModel.cs @@ -10,6 +10,7 @@ namespace ConfectioneryDataModels.Models public interface IOrderModel : IId { int PastryId { get; } + int ClientId { get; } int Count { get; } double Sum { get; } OrderStatus Status { get; } diff --git a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs new file mode 100644 index 0000000..da92bdd --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs @@ -0,0 +1,125 @@ +using ConfectioneryContracts.ViewModels; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryBusinessLogic.BusinessLogics +{ + 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. Email: {Email}. Id: {Id} ", model?.Email, model?.Id); + 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. Client email: {Email}. Client id: {Id}", model.Email, model.Id); + 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}, FIO: {fio}, email: {email}, password: {password}", model.Id, model.ClientFIO, model.Email, + model.Password); + var element = _clientStorage.GetElement(new ClientSearchModel + { + Email = model.Email + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Клиент с таким логином(почтой) уже есть"); + } + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index 9682e7f..9e535ef 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -22,5 +22,6 @@ namespace ConfectioneryDatabaseImplement public virtual DbSet Pastries { set; get; } public virtual DbSet PastryComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Clients { set; get; } } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/ClientStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..8b17d43 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Clients + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Clients + .Where(x => x.Email.Contains(model.Email)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Clients.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + using var context = new ConfectioneryDatabase(); + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new ConfectioneryDatabase(); + 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 ConfectioneryDatabase(); + var element = context.Clients.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs index 0f76e5b..2fb6862 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs @@ -14,30 +14,42 @@ namespace ConfectioneryDatabaseImplement.Implements using var context = new ConfectioneryDatabase(); return context.Orders .Include(x => x.Pastry) + .Include(x => x.Client) .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(OrderSearchModel model) { - if (!model.Id.HasValue && !model.DateFrom.HasValue) - { - return new(); - } using var context = new ConfectioneryDatabase(); - if (model.DateFrom.HasValue) + if (model.Id.HasValue) { return context.Orders .Include(x => x.Pastry) + .Include(x => x.Client) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.DateFrom != null && model.DateTo != null) + { + return context.Orders + .Include(x => x.Pastry) + .Include(x => x.Client) .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) .Select(x => x.GetViewModel) .ToList(); } - return context.Orders + else if (model.ClientId.HasValue) + { + return context.Orders .Include(x => x.Pastry) - .Where(x => x.Id == model.Id) + .Include(x => x.Client) + .Where(x => x.ClientId == model.ClientId) .Select(x => x.GetViewModel) .ToList(); + } + return new(); } public OrderViewModel? GetElement(OrderSearchModel model) @@ -49,6 +61,7 @@ namespace ConfectioneryDatabaseImplement.Implements using var context = new ConfectioneryDatabase(); return context.Orders .Include(x => x.Pastry) + .Include(x => x.Client) .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; } @@ -65,6 +78,7 @@ namespace ConfectioneryDatabaseImplement.Implements context.SaveChanges(); return context.Orders .Include(x => x.Pastry) + .Include(x => x.Client) .FirstOrDefault(x => x.Id == newOrder.Id) ?.GetViewModel; } @@ -81,6 +95,7 @@ namespace ConfectioneryDatabaseImplement.Implements context.SaveChanges(); return context.Orders .Include(x => x.Pastry) + .Include(x => x.Client) .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; } @@ -93,6 +108,7 @@ namespace ConfectioneryDatabaseImplement.Implements { var deletedElement = context.Orders .Include(x => x.Pastry) + .Include(x => x.Client) .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; context.Orders.Remove(element); diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Client.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Client.cs new file mode 100644 index 0000000..6ddd8b2 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Client.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + [Required] + public string ClientFIO { get; private set; } = string.Empty; + [Required] + public string Email { get; private set; } = string.Empty; + [Required] + public string Password { get; private set; } = string.Empty; + [ForeignKey("ClientId")] + public virtual List Orders { get; set; } = new(); + public static Client? Create(ClientBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + 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/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs index 619f7b1..344fca8 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs @@ -12,6 +12,8 @@ namespace ConfectioneryDatabaseImplement.Models [Required] public int PastryId { get; set; } [Required] + public int ClientId { get; set; } + [Required] public int Count { get; set; } [Required] public double Sum { get; set; } @@ -21,6 +23,7 @@ namespace ConfectioneryDatabaseImplement.Models public DateTime DateCreate { get; set; } public DateTime? DateImplement { get; set; } public virtual Pastry Pastry { get; set; } + public virtual Client Client { get; set; } public static Order? Create(OrderBindingModel? model) { if (model == null) @@ -31,6 +34,7 @@ namespace ConfectioneryDatabaseImplement.Models { Id = model.Id, PastryId = model.PastryId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -53,12 +57,14 @@ namespace ConfectioneryDatabaseImplement.Models { Id = Id, PastryId = PastryId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, - PastryName = Pastry.PastryName + PastryName = Pastry.PastryName, + ClientFIO = Client.ClientFIO }; } } \ No newline at end of file diff --git a/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs b/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs index 85ba816..3e76d45 100644 --- a/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs +++ b/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; using ConfectioneryFileImplement.Models; using System.Xml.Linq; - +using ConfectioneryContracts.SearchModels; namespace ConfectioneryFileImplement { @@ -15,9 +15,11 @@ namespace ConfectioneryFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string PastryFileName = "Pastry.xml"; + private readonly string ClientFileName = "Client.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Pastries { get; private set; } + public List Clients { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -29,11 +31,13 @@ namespace ConfectioneryFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SavePastries() => SaveData(Pastries, PastryFileName, "Pastries", 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)!)!; Pastries = LoadData(PastryFileName, "Pastry", x => Pastry.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) @@ -50,10 +54,8 @@ namespace ConfectioneryFileImplement { if (data != null) { - new XDocument(new XElement(xmlNodeName, - data.Select(selectFunction).ToArray())).Save(filename); + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); } - } } } \ No newline at end of file diff --git a/Confectionery/ConfectioneryFileImplement/Implements/ClientStorage.cs b/Confectionery/ConfectioneryFileImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..7f7006e --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/Implements/ClientStorage.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryFileImplement.Models; + +namespace ConfectioneryFileImplement.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.Email)) + { + return new(); + } + return source.Clients + .Where(x => x.Email.Contains(model.Email)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue) + { + return null; + } + return source.Clients + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + model.Id = source.Clients.Count > 0 ? source.Clients.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/Confectionery/ConfectioneryFileImplement/Implements/OrderStorage.cs b/Confectionery/ConfectioneryFileImplement/Implements/OrderStorage.cs index 5455bb6..91b9498 100644 --- a/Confectionery/ConfectioneryFileImplement/Implements/OrderStorage.cs +++ b/Confectionery/ConfectioneryFileImplement/Implements/OrderStorage.cs @@ -23,27 +23,34 @@ namespace ConfectioneryFileImplement.Implements public List GetFullList() { return source.Orders - .Select(x => AddPastryName(x.GetViewModel)) + .Select(x => AddInfo(x.GetViewModel)) .ToList(); } public List GetFilteredList(OrderSearchModel model) { - if (!model.Id.HasValue && !model.DateFrom.HasValue) - { - return new(); - } - if (model.DateFrom.HasValue) + if (model.Id.HasValue) { return source.Orders - .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) - .Select(x => AddPastryName(x.GetViewModel)) - .ToList(); + .Where(x => x.Id == model.Id) + .Select(x => AddInfo(x.GetViewModel)) + .ToList(); } - return source.Orders - .Where(x => x.Id == model.Id) - .Select(x => AddPastryName(x.GetViewModel)) - .ToList(); + else if (model.DateFrom.HasValue && model.DateTo.HasValue) + { + return source.Orders + .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) + .Select(x => AddInfo(x.GetViewModel)) + .ToList(); + } + else if (model.ClientId.HasValue) + { + return source.Orders + .Where(x => x.ClientId == model.ClientId) + .Select(x => AddInfo(x.GetViewModel)) + .ToList(); + } + return new(); } public OrderViewModel? GetElement(OrderSearchModel model) @@ -57,7 +64,7 @@ namespace ConfectioneryFileImplement.Implements { return null; } - return AddPastryName(order.GetViewModel); + return AddInfo(order.GetViewModel); } public OrderViewModel? Insert(OrderBindingModel model) @@ -70,7 +77,7 @@ namespace ConfectioneryFileImplement.Implements } source.Orders.Add(newOrder); source.SaveOrders(); - return AddPastryName(newOrder.GetViewModel); + return AddInfo(newOrder.GetViewModel); } public OrderViewModel? Update(OrderBindingModel model) @@ -82,7 +89,7 @@ namespace ConfectioneryFileImplement.Implements } order.Update(model); source.SaveOrders(); - return AddPastryName(order.GetViewModel); + return AddInfo(order.GetViewModel); } public OrderViewModel? Delete(OrderBindingModel model) @@ -92,18 +99,17 @@ namespace ConfectioneryFileImplement.Implements { source.Orders.Remove(element); source.SaveOrders(); - return AddPastryName(element.GetViewModel); + return AddInfo(element.GetViewModel); } return null; } - private OrderViewModel AddPastryName(OrderViewModel model) + private OrderViewModel AddInfo(OrderViewModel model) { var selectedPastry = source.Pastries.FirstOrDefault(x => x.Id == model.PastryId); - if (selectedPastry != null) - { - model.PastryName = selectedPastry.PastryName; - } + model.PastryName = selectedPastry?.PastryName ?? string.Empty; + var selectedClient = source.Clients.FirstOrDefault(x => x.Id == model.ClientId); + model.ClientFIO = selectedClient?.ClientFIO ?? string.Empty; return model; } } diff --git a/Confectionery/ConfectioneryFileImplement/Models/Client.cs b/Confectionery/ConfectioneryFileImplement/Models/Client.cs new file mode 100644 index 0000000..4afe205 --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/Models/Client.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement.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() + { + 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() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ClientFIO = element.Element("FIO")!.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("FIO", ClientFIO), + new XElement("Email", Email), + new XElement("Password", Password) + ); + } +} diff --git a/Confectionery/ConfectioneryFileImplement/Models/Order.cs b/Confectionery/ConfectioneryFileImplement/Models/Order.cs index 1bee80c..b2c8825 100644 --- a/Confectionery/ConfectioneryFileImplement/Models/Order.cs +++ b/Confectionery/ConfectioneryFileImplement/Models/Order.cs @@ -12,6 +12,8 @@ namespace ConfectioneryFileImplement.Models public int PastryId { get; private set; } + public int ClientId { get; private set; } + public int Count { get; private set; } public double Sum { get; private set; } @@ -32,6 +34,7 @@ namespace ConfectioneryFileImplement.Models { Id = model.Id, PastryId = model.PastryId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -50,6 +53,7 @@ namespace ConfectioneryFileImplement.Models { Id = Convert.ToInt32(element.Attribute("Id")!.Value), PastryId = Convert.ToInt32(element.Element("PastryId")!.Value), + ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), @@ -73,6 +77,7 @@ namespace ConfectioneryFileImplement.Models { Id = Id, PastryId = PastryId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status, @@ -83,6 +88,7 @@ namespace ConfectioneryFileImplement.Models public XElement GetXElement => new("Order", new XAttribute("Id", Id), new XElement("PastryId", PastryId), + new XElement("ClientId", ClientId), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), new XElement("Status", Status.ToString()), diff --git a/Confectionery/ConfectioneryListImplement/DataListSingleton.cs b/Confectionery/ConfectioneryListImplement/DataListSingleton.cs index c823733..461078e 100644 --- a/Confectionery/ConfectioneryListImplement/DataListSingleton.cs +++ b/Confectionery/ConfectioneryListImplement/DataListSingleton.cs @@ -3,20 +3,23 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ConfectioneryListImplement.Models; -namespace ConfectioneryListImplement.Models +namespace ConfectioneryListImplement { - internal class DataListSingleton + public class DataListSingleton { private static DataListSingleton? _instance; public List Components { get; set; } public List Orders { get; set; } public List Pastries { get; set; } + public List Clients { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Pastries = new List(); + Clients = new List(); } public static DataListSingleton GetInstance() { diff --git a/Confectionery/ConfectioneryListImplement/Implements/ClientStorage.cs b/Confectionery/ConfectioneryListImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..7bd7f30 --- /dev/null +++ b/Confectionery/ConfectioneryListImplement/Implements/ClientStorage.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryListImplement.Models; + +namespace ConfectioneryListImplement.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.Email)) + { + return result; + } + foreach (var client in _source.Clients) + { + if (client.Email.Contains(model.Email)) + { + result.Add(client.GetViewModel); + } + } + return result; + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue) + { + return null; + } + foreach (var client in _source.Clients) + { + if ((!string.IsNullOrEmpty(model.Email) && client.Email == model.Email + && !string.IsNullOrEmpty(model.Password) && client.Password == model.Password) + || (model.Id.HasValue && client.Id == model.Id)) + { + 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/Confectionery/ConfectioneryListImplement/Implements/OrderStorage.cs b/Confectionery/ConfectioneryListImplement/Implements/OrderStorage.cs index df15642..361e1b9 100644 --- a/Confectionery/ConfectioneryListImplement/Implements/OrderStorage.cs +++ b/Confectionery/ConfectioneryListImplement/Implements/OrderStorage.cs @@ -25,7 +25,7 @@ namespace ConfectioneryListImplement.Implements var result = new List(); foreach (var order in _source.Orders) { - result.Add(AddPastryName(order.GetViewModel)); + result.Add(AddInfo(order.GetViewModel)); } return result; } @@ -33,17 +33,33 @@ namespace ConfectioneryListImplement.Implements public List GetFilteredList(OrderSearchModel model) { var result = new List(); - if (!model.Id.HasValue && !model.DateFrom.HasValue) + if (model.Id.HasValue) { - return result; + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + result.Add(AddInfo(order.GetViewModel)); + } + } } - if (model.DateFrom.HasValue) + else if (model.DateFrom.HasValue && model.DateTo.HasValue) { foreach (var order in _source.Orders) { if (order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo) { - result.Add(AddPastryName(order.GetViewModel)); + result.Add(AddInfo(order.GetViewModel)); + } + } + } + else if (model.ClientId.HasValue) + { + foreach (var order in _source.Orders) + { + if (order.ClientId == model.ClientId) + { + result.Add(AddInfo(order.GetViewModel)); } } } @@ -60,7 +76,7 @@ namespace ConfectioneryListImplement.Implements { if (order.Id == model.Id) { - return AddPastryName(order.GetViewModel); + return AddInfo(order.GetViewModel); } } return null; @@ -82,7 +98,7 @@ namespace ConfectioneryListImplement.Implements return null; } _source.Orders.Add(newOrder); - return AddPastryName(newOrder.GetViewModel); + return AddInfo(newOrder.GetViewModel); } public OrderViewModel? Update(OrderBindingModel model) @@ -92,7 +108,7 @@ namespace ConfectioneryListImplement.Implements if (order.Id == model.Id) { order.Update(model); - return AddPastryName(order.GetViewModel); + return AddInfo(order.GetViewModel); } } return null; @@ -106,19 +122,27 @@ namespace ConfectioneryListImplement.Implements { var element = _source.Orders[i]; _source.Orders.RemoveAt(i); - return AddPastryName(element.GetViewModel); + return AddInfo(element.GetViewModel); } } return null; } - private OrderViewModel AddPastryName(OrderViewModel model) + private OrderViewModel AddInfo(OrderViewModel model) { var selectedPastry = _source.Pastries.Find(pastry => pastry.Id == model.PastryId); if (selectedPastry != null) { model.PastryName = selectedPastry.PastryName; } + foreach (var client in _source.Clients) + { + if (client.Id == model.ClientId) + { + model.ClientFIO = client.ClientFIO; + break; + } + } return model; } } diff --git a/Confectionery/ConfectioneryListImplement/Models/Client.cs b/Confectionery/ConfectioneryListImplement/Models/Client.cs new file mode 100644 index 0000000..833b7de --- /dev/null +++ b/Confectionery/ConfectioneryListImplement/Models/Client.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; + +namespace ConfectioneryListImplement.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() + { + 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/Confectionery/ConfectioneryListImplement/Models/Component.cs b/Confectionery/ConfectioneryListImplement/Models/Component.cs index 0602b42..0875853 100644 --- a/Confectionery/ConfectioneryListImplement/Models/Component.cs +++ b/Confectionery/ConfectioneryListImplement/Models/Component.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; namespace ConfectioneryListImplement.Models { - internal class Component : IComponentModel + public class Component : IComponentModel { public int Id { get; private set; } public string ComponentName { get; private set; } = string.Empty; diff --git a/Confectionery/ConfectioneryListImplement/Models/Order.cs b/Confectionery/ConfectioneryListImplement/Models/Order.cs index 82d0fda..03134d6 100644 --- a/Confectionery/ConfectioneryListImplement/Models/Order.cs +++ b/Confectionery/ConfectioneryListImplement/Models/Order.cs @@ -10,12 +10,14 @@ using System.Threading.Tasks; namespace ConfectioneryListImplement.Models { - internal class Order : IOrderModel + public class Order : IOrderModel { public int Id { get; private set; } public int PastryId { get; private set; } + public int ClientId { get; private set; } + public int Count { get; private set; } public double Sum { get; private set; } @@ -36,6 +38,7 @@ namespace ConfectioneryListImplement.Models { Id = model.Id, PastryId = model.PastryId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -58,6 +61,7 @@ namespace ConfectioneryListImplement.Models { Id = Id, PastryId = PastryId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status, diff --git a/Confectionery/ConfectioneryListImplement/Models/Pastry.cs b/Confectionery/ConfectioneryListImplement/Models/Pastry.cs index ce7f3f3..f83ddac 100644 --- a/Confectionery/ConfectioneryListImplement/Models/Pastry.cs +++ b/Confectionery/ConfectioneryListImplement/Models/Pastry.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; namespace ConfectioneryListImplement.Models { - internal class Pastry : IPastryModel + public class Pastry : IPastryModel { public int Id { get; private set; } public string PastryName { get; private set; } = string.Empty; diff --git a/Confectionery/ConfectioneryView/FormClients.Designer.cs b/Confectionery/ConfectioneryView/FormClients.Designer.cs new file mode 100644 index 0000000..bf8691a --- /dev/null +++ b/Confectionery/ConfectioneryView/FormClients.Designer.cs @@ -0,0 +1,99 @@ +namespace ConfectioneryView +{ + 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.buttonUpd = new System.Windows.Forms.Button(); + this.buttonDel = new System.Windows.Forms.Button(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // buttonUpd + // + this.buttonUpd.Location = new System.Drawing.Point(528, 79); + this.buttonUpd.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4); + this.buttonUpd.Name = "buttonUpd"; + this.buttonUpd.Size = new System.Drawing.Size(101, 36); + this.buttonUpd.TabIndex = 11; + this.buttonUpd.Text = "Обновить"; + this.buttonUpd.UseVisualStyleBackColor = true; + this.buttonUpd.Click += new System.EventHandler(this.ButtonUpd_Click); + // + // buttonDel + // + this.buttonDel.Location = new System.Drawing.Point(528, 16); + this.buttonDel.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(101, 36); + this.buttonDel.TabIndex = 10; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; + this.buttonDel.Click += new System.EventHandler(this.ButtonDel_Click); + // + // dataGridView + // + this.dataGridView.AllowUserToAddRows = false; + this.dataGridView.AllowUserToDeleteRows = false; + this.dataGridView.BackgroundColor = System.Drawing.SystemColors.ControlLightLight; + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Dock = System.Windows.Forms.DockStyle.Left; + this.dataGridView.Location = new System.Drawing.Point(0, 0); + this.dataGridView.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4); + this.dataGridView.MultiSelect = false; + this.dataGridView.Name = "dataGridView"; + this.dataGridView.ReadOnly = true; + this.dataGridView.RowHeadersVisible = false; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; + this.dataGridView.Size = new System.Drawing.Size(466, 506); + this.dataGridView.TabIndex = 9; + // + // FormClients + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(667, 506); + this.Controls.Add(this.buttonUpd); + this.Controls.Add(this.buttonDel); + 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 Button buttonUpd; + private Button buttonDel; + private DataGridView dataGridView; + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryView/FormClients.cs b/Confectionery/ConfectioneryView/FormClients.cs new file mode 100644 index 0000000..f40612e --- /dev/null +++ b/Confectionery/ConfectioneryView/FormClients.cs @@ -0,0 +1,84 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +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 ConfectioneryView +{ + 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) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ClientFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Clients loading"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Clients loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonDel_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); + _logger.LogInformation("Deletion of client"); + try + { + if (!_logic.Delete(new ClientBindingModel { Id = id })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Client deletion error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void ButtonUpd_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/Confectionery/ConfectioneryView/FormClients.resx b/Confectionery/ConfectioneryView/FormClients.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Confectionery/ConfectioneryView/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