From 3a4c1738492b9838011ac8317b32f7e422e364cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 4 Mar 2023 14:23:29 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=20=D0=BD=D0=BE=D0=B2=D0=BE=D0=B9?= =?UTF-8?q?=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20(=D0=BF=D0=BE=D1=87?= =?UTF-8?q?=D0=B5=D0=BC=D1=83-=D1=82=D0=BE=20=D1=83=D0=B4=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=BB=D0=B0=D1=81=D1=8C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ClientLogic.cs | 116 +++++++++++++++++++- ConfectionaryFileImplement/Client.cs | 25 ++++- ConfectionaryFileImplement/ClientStorage.cs | 20 ++-- 3 files changed, 145 insertions(+), 16 deletions(-) diff --git a/ConfectionaryBusinessLogic/ClientLogic.cs b/ConfectionaryBusinessLogic/ClientLogic.cs index 53a0137..1a9b6c8 100644 --- a/ConfectionaryBusinessLogic/ClientLogic.cs +++ b/ConfectionaryBusinessLogic/ClientLogic.cs @@ -1,12 +1,116 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryBusinessLogic.BusinessLogics; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using Microsoft.Extensions.Logging; namespace ConfectioneryBusinessLogic { - internal class ClientLogic + public class ClientLogic : IClientLogic { + private readonly ILogger _logger; + private readonly IClientStorage _clientStorage; + public ClientLogic(ILogger logger, IClientStorage clientStorage) + { + _logger = logger; + _clientStorage = clientStorage; + } + + public bool Create(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Insert(model) == null) + { + _logger.LogWarning("Insert 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; + } + + public ClientViewModel? ReadElement(ClientSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Email:{Email}.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 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 bool Update(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Update(model) == null) + { + _logger.LogWarning("Update 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)); + } + _logger.LogInformation("Client. Id: {Id}, FIO: {fio}, 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/ConfectionaryFileImplement/Client.cs b/ConfectionaryFileImplement/Client.cs index a1f7048..4796c2d 100644 --- a/ConfectionaryFileImplement/Client.cs +++ b/ConfectionaryFileImplement/Client.cs @@ -6,8 +6,9 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; -namespace ConfectioneryListImplement +namespace ConfectioneryFileImplement { public class Client : IClientModel { @@ -33,6 +34,21 @@ namespace ConfectioneryListImplement 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) { @@ -52,5 +68,12 @@ namespace ConfectioneryListImplement 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/ConfectionaryFileImplement/ClientStorage.cs b/ConfectionaryFileImplement/ClientStorage.cs index d21fdbb..1de16fc 100644 --- a/ConfectionaryFileImplement/ClientStorage.cs +++ b/ConfectionaryFileImplement/ClientStorage.cs @@ -2,20 +2,15 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace ConfectioneryListImplement +namespace ConfectioneryFileImplement { public class ClientStorage : IClientStorage { - private readonly DataListSingleton _source; + private readonly DataFileSingleton _source; public ClientStorage() { - _source = DataListSingleton.GetInstance(); + _source = DataFileSingleton.GetInstance(); } public ClientViewModel? Delete(ClientBindingModel model) @@ -24,6 +19,7 @@ namespace ConfectioneryListImplement if (res != null) { _source.Clients.Remove(res); + _source.SaveClients(); } return res?.GetViewModel; } @@ -61,10 +57,12 @@ namespace ConfectioneryListImplement public ClientViewModel? Insert(ClientBindingModel model) { + model.Id = _source.Clients.Count > 0 ? _source.Clients.Max(x => x.Id) + 1 : 1; var res = Client.Create(model); if (res != null) { _source.Clients.Add(res); + _source.SaveClients(); } return res?.GetViewModel; } @@ -72,7 +70,11 @@ namespace ConfectioneryListImplement public ClientViewModel? Update(ClientBindingModel model) { var res = _source.Clients.FirstOrDefault(x => x.Id == model.Id); - res?.Update(model); + if (res != null) + { + res.Update(model); + _source.SaveClients(); + } return res?.GetViewModel; } }