From aaaa7470b096ffdfc9de9752e8436f5186941625 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Mon, 22 Apr 2024 10:24:39 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ClientLogic.cs | 125 ++++++++++++++++++ .../BindingModels/ClientBindingModel.cs | 17 +++ .../BusinessLogicsContracts/IClientLogic.cs | 19 +++ .../SearchModels/ClientSearchModel.cs | 17 +++ .../StoragesContracts/IClientStorage.cs | 20 +++ .../ViewModels/ClientViewModel.cs | 21 +++ .../IClientModel.cs | 15 +++ .../Client.cs | 68 ++++++++++ .../ClientStorage.cs | 88 ++++++++++++ .../SoftwareInstallationDatabase.cs | 1 + 10 files changed, 391 insertions(+) create mode 100644 SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/ClientLogic.cs create mode 100644 SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ClientBindingModel.cs create mode 100644 SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IClientLogic.cs create mode 100644 SoftwareInstallation/SoftwareInstallationContracts/SearchModels/ClientSearchModel.cs create mode 100644 SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IClientStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ClientViewModel.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDataModels/IClientModel.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDatabaseImplement/Client.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDatabaseImplement/ClientStorage.cs diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/ClientLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/ClientLogic.cs new file mode 100644 index 0000000..51bea21 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/ClientLogic.cs @@ -0,0 +1,125 @@ +using Microsoft.Extensions.Logging; +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.BusinessLogicsContracts; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationBusinessLogic.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 + { + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Клиент с таким названием уже есть"); + } + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ClientBindingModel.cs b/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ClientBindingModel.cs new file mode 100644 index 0000000..9992d50 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ClientBindingModel.cs @@ -0,0 +1,17 @@ +using SoftwareInstallationDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.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/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IClientLogic.cs b/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IClientLogic.cs new file mode 100644 index 0000000..02b41fd --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IClientLogic.cs @@ -0,0 +1,19 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.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/SoftwareInstallation/SoftwareInstallationContracts/SearchModels/ClientSearchModel.cs b/SoftwareInstallation/SoftwareInstallationContracts/SearchModels/ClientSearchModel.cs new file mode 100644 index 0000000..883e5d6 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationContracts/SearchModels/ClientSearchModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.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/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IClientStorage.cs b/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IClientStorage.cs new file mode 100644 index 0000000..d9cdab5 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IClientStorage.cs @@ -0,0 +1,20 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.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/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ClientViewModel.cs b/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ClientViewModel.cs new file mode 100644 index 0000000..4367ac1 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ClientViewModel.cs @@ -0,0 +1,21 @@ +using SoftwareInstallationDataModels; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.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/SoftwareInstallation/SoftwareInstallationDataModels/IClientModel.cs b/SoftwareInstallation/SoftwareInstallationDataModels/IClientModel.cs new file mode 100644 index 0000000..ee3dbf4 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataModels/IClientModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationDataModels +{ + public interface IClientModel : IId + { + string ClientFIO { get; } + string Email { get; } + string Password { get; } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Client.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Client.cs new file mode 100644 index 0000000..b572d75 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/Client.cs @@ -0,0 +1,68 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels; +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 SoftwareInstallationDatabaseImplement +{ + public class Client : IClientModel + { + public int Id { get; private set; } + [Required] + public string ClientFIO { get; private set; } = string.Empty; + [Required] + public string Email { get; set; } = string.Empty; + [Required] + public string Password { get; 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 Client() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }; + } + public static Client Create(ClientViewModel model) + { + 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/SoftwareInstallation/SoftwareInstallationDatabaseImplement/ClientStorage.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/ClientStorage.cs new file mode 100644 index 0000000..518b78e --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/ClientStorage.cs @@ -0,0 +1,88 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationDatabaseImplement +{ + public class ClientStorage : IClientStorage + { + public List GetFullList() + { + using var context = new SoftwareInstallationDatabase(); + return context.Clients + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email) && + string.IsNullOrEmpty(model.Password) && + string.IsNullOrEmpty(model.ClientFIO)) + { + return new(); + } + using var context = new SoftwareInstallationDatabase(); + return context.Clients + .Where(x => (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO.Contains(model.ClientFIO) && + string.IsNullOrEmpty(model.Email) || x.ClientFIO.Contains(model.Email) && + string.IsNullOrEmpty(model.Password) || x.ClientFIO.Contains(model.Password))) + .Select(x => x.GetViewModel) + .ToList(); + } + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email) && + !model.Id.HasValue) + { + return null; + } + using var context = new SoftwareInstallationDatabase(); + return context.Clients + .FirstOrDefault(x => (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO == model.ClientFIO) && + (!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Email) || x.Email == model.Email)) + ?.GetViewModel; + } + public ClientViewModel? Insert(ClientBindingModel model) + { + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + using var context = new SoftwareInstallationDatabase(); + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient.GetViewModel; + } + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new SoftwareInstallationDatabase(); + 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 SoftwareInstallationDatabase(); + var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabase.cs b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabase.cs index 6b4f1b9..aad70ef 100644 --- a/SoftwareInstallation/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabase.cs +++ b/SoftwareInstallation/SoftwareInstallationDatabaseImplement/SoftwareInstallationDatabase.cs @@ -22,5 +22,6 @@ namespace SoftwareInstallationDatabaseImplement public virtual DbSet Packages { set; get; } public virtual DbSet PackageComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Clients { set; get; } } }