diff --git a/FishFactory/FishFactoryBusinessLogic/BusinessLogic/ClientLogic.cs b/FishFactory/FishFactoryBusinessLogic/BusinessLogic/ClientLogic.cs new file mode 100644 index 0000000..d438c2e --- /dev/null +++ b/FishFactory/FishFactoryBusinessLogic/BusinessLogic/ClientLogic.cs @@ -0,0 +1,120 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.BusinessLogicsContracts; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryBusinessLogic.BusinessLogic +{ + 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/FishFactory/FishFactoryContracts/BindingModels/ClientBindingModel.cs b/FishFactory/FishFactoryContracts/BindingModels/ClientBindingModel.cs new file mode 100644 index 0000000..97c6709 --- /dev/null +++ b/FishFactory/FishFactoryContracts/BindingModels/ClientBindingModel.cs @@ -0,0 +1,17 @@ +using FishFactoryDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryContracts.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/FishFactory/FishFactoryContracts/BusinessLogicsContracts/IClientLogic.cs b/FishFactory/FishFactoryContracts/BusinessLogicsContracts/IClientLogic.cs new file mode 100644 index 0000000..90fad64 --- /dev/null +++ b/FishFactory/FishFactoryContracts/BusinessLogicsContracts/IClientLogic.cs @@ -0,0 +1,20 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryContracts.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/FishFactory/FishFactoryContracts/SearchModels/ClientSearchModel.cs b/FishFactory/FishFactoryContracts/SearchModels/ClientSearchModel.cs new file mode 100644 index 0000000..1b5dd7d --- /dev/null +++ b/FishFactory/FishFactoryContracts/SearchModels/ClientSearchModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryContracts.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/FishFactory/FishFactoryContracts/StoragesContracts/IClientStorage.cs b/FishFactory/FishFactoryContracts/StoragesContracts/IClientStorage.cs new file mode 100644 index 0000000..06771f1 --- /dev/null +++ b/FishFactory/FishFactoryContracts/StoragesContracts/IClientStorage.cs @@ -0,0 +1,21 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryContracts.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/FishFactory/FishFactoryContracts/ViewModels/ClientViewModel.cs b/FishFactory/FishFactoryContracts/ViewModels/ClientViewModel.cs new file mode 100644 index 0000000..9737cf0 --- /dev/null +++ b/FishFactory/FishFactoryContracts/ViewModels/ClientViewModel.cs @@ -0,0 +1,21 @@ +using FishFactoryDataModels; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryContracts.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/FishFactory/FishFactoryDataModels/IClientModel.cs b/FishFactory/FishFactoryDataModels/IClientModel.cs new file mode 100644 index 0000000..1fa77cc --- /dev/null +++ b/FishFactory/FishFactoryDataModels/IClientModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryDataModels +{ + public interface IClientModel : IId + { + string ClientFIO { get; } + string Email { get; } + string Password { get; } + } +}