From 0d1e70e04d271b64a5d598d40e0323eb87d4d2eb Mon Sep 17 00:00:00 2001 From: DeerElk Date: Wed, 24 Apr 2024 15:03:13 +0400 Subject: [PATCH] lab5 alpha --- .../BusinessLogics/ClientLogic.cs | 122 ++++++++++++++++++ .../BindingModels/ClientBindingModel.cs | 12 ++ .../BusinessLogicsContracts/IClientLogic.cs | 14 ++ .../StoragesContracts/IClientStorage.cs | 15 +++ .../ViewModels/ClientViewModel.cs | 15 +++ .../Models/IClientModel.cs | 9 ++ 6 files changed, 187 insertions(+) create mode 100644 Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs create mode 100644 Confectionery/ConfectioneryContracts/BindingModels/ClientBindingModel.cs create mode 100644 Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IClientLogic.cs create mode 100644 Confectionery/ConfectioneryContracts/StoragesContracts/IClientStorage.cs create mode 100644 Confectionery/ConfectioneryContracts/ViewModels/ClientViewModel.cs create mode 100644 Confectionery/ConfectioneryDataModels/Models/IClientModel.cs diff --git a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs new file mode 100644 index 0000000..fa287f5 --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs @@ -0,0 +1,122 @@ +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using Microsoft.Extensions.Logging; +namespace ConfectioneryBusinessLogic.BusinessLogics +{ + public class ClientLogic : IClientLogic + { + private readonly ILogger _logger; + private readonly IClientStorage _clientStorage; + public ClientLogic(ILogger logger, IClientStorage + componentStorage) + { + _logger = logger; + _clientStorage = componentStorage; + } + public List? ReadList(ClientSearchModel? model) + { + _logger.LogInformation("ReadList. ClientFIO:{ClientFIO}. Id:{ Id}", + model?.ClientFIO, 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. " + + "ClientFIO:{ClientFIO}.Id:{ Id}", + model.ClientFIO, 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("Нет Email клиента", + nameof(model.ClientFIO)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля клиента", + nameof(model.ClientFIO)); + } + _logger.LogInformation("Client. ClientFIO:{ClientFIO}." + + "Email:{ Email}. Password:{ Password}. Id: { Id} ", + model.ClientFIO, model.Email, model.Password, model.Id); + var element = _clientStorage.GetElement(new ClientSearchModel + { + Email = model.Email, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException( + "Клиент с таким лоигном уже есть"); + } + } + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryContracts/BindingModels/ClientBindingModel.cs b/Confectionery/ConfectioneryContracts/BindingModels/ClientBindingModel.cs new file mode 100644 index 0000000..da1ef7d --- /dev/null +++ b/Confectionery/ConfectioneryContracts/BindingModels/ClientBindingModel.cs @@ -0,0 +1,12 @@ +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/ConfectioneryContracts/BusinessLogicsContracts/IClientLogic.cs b/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IClientLogic.cs new file mode 100644 index 0000000..e2bb24c --- /dev/null +++ b/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IClientLogic.cs @@ -0,0 +1,14 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +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/ConfectioneryContracts/StoragesContracts/IClientStorage.cs b/Confectionery/ConfectioneryContracts/StoragesContracts/IClientStorage.cs new file mode 100644 index 0000000..2f3d536 --- /dev/null +++ b/Confectionery/ConfectioneryContracts/StoragesContracts/IClientStorage.cs @@ -0,0 +1,15 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +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/ConfectioneryContracts/ViewModels/ClientViewModel.cs b/Confectionery/ConfectioneryContracts/ViewModels/ClientViewModel.cs new file mode 100644 index 0000000..6f9d1a0 --- /dev/null +++ b/Confectionery/ConfectioneryContracts/ViewModels/ClientViewModel.cs @@ -0,0 +1,15 @@ +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/ConfectioneryDataModels/Models/IClientModel.cs b/Confectionery/ConfectioneryDataModels/Models/IClientModel.cs new file mode 100644 index 0000000..ed854ae --- /dev/null +++ b/Confectionery/ConfectioneryDataModels/Models/IClientModel.cs @@ -0,0 +1,9 @@ +namespace ConfectioneryDataModels.Models +{ + public interface IClientModel : IId + { + string ClientFIO { get; } + string Email { get; } + string Password { get; } + } +}