From d32632eb7361833f444ea77b881d2d23c6a01395 Mon Sep 17 00:00:00 2001 From: Ismailov_Rovshan Date: Sat, 8 Apr 2023 21:01:58 +0400 Subject: [PATCH] Buisnes Logic --- .../HeadsetModuleBindingModel.cs | 6 + .../IFurnitureLogic.cs | 2 +- .../IHeadsetModuleLogic.cs | 2 +- .../BusinessLogicsContracts/IMaterialLogic.cs | 2 +- .../BusinessLogic/FurnitureLogic.cs | 118 +++++++++++++++++ .../BusinessLogic/HeadsetModuleLogic.cs | 116 +++++++++++++++++ .../BusinessLogic/MasterLogic.cs | 121 ++++++++++++++++++ .../BusinessLogic/MaterialLogic.cs | 119 +++++++++++++++++ 8 files changed, 483 insertions(+), 3 deletions(-) create mode 100644 FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/FurnitureLogic.cs create mode 100644 FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/HeadsetModuleLogic.cs create mode 100644 FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/MasterLogic.cs create mode 100644 FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/MaterialLogic.cs diff --git a/FurnitureFactory/FurnitureContracts/BindingModels/HeadsetModuleBindingModel.cs b/FurnitureFactory/FurnitureContracts/BindingModels/HeadsetModuleBindingModel.cs index 68db926..bb545a6 100644 --- a/FurnitureFactory/FurnitureContracts/BindingModels/HeadsetModuleBindingModel.cs +++ b/FurnitureFactory/FurnitureContracts/BindingModels/HeadsetModuleBindingModel.cs @@ -20,5 +20,11 @@ namespace FurnitureContracts.BindingModels public string MasterName { get; set; } = string.Empty; public Dictionary HeadsetModuleMaterial { get; set; } + + public string Name { get; set; } = string.Empty; + + public DateTime DateIssue => throw new NotImplementedException(); + + public DateTime DateDelivery => throw new NotImplementedException(); } } diff --git a/FurnitureFactory/FurnitureContracts/BusinessLogicsContracts/IFurnitureLogic.cs b/FurnitureFactory/FurnitureContracts/BusinessLogicsContracts/IFurnitureLogic.cs index 83c6d07..b1d58a9 100644 --- a/FurnitureFactory/FurnitureContracts/BusinessLogicsContracts/IFurnitureLogic.cs +++ b/FurnitureFactory/FurnitureContracts/BusinessLogicsContracts/IFurnitureLogic.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; namespace FurnitureContracts.BusinessLogicsContracts { - interface IFurnitureLogic + public interface IFurnitureLogic { List? ReadList(FurnitureSearchModel? model); FurnitureViewModel? ReadElement(FurnitureSearchModel model); diff --git a/FurnitureFactory/FurnitureContracts/BusinessLogicsContracts/IHeadsetModuleLogic.cs b/FurnitureFactory/FurnitureContracts/BusinessLogicsContracts/IHeadsetModuleLogic.cs index fcc064b..a8c507a 100644 --- a/FurnitureFactory/FurnitureContracts/BusinessLogicsContracts/IHeadsetModuleLogic.cs +++ b/FurnitureFactory/FurnitureContracts/BusinessLogicsContracts/IHeadsetModuleLogic.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; namespace FurnitureContracts.BusinessLogicsContracts { - internal interface IHeadsetModuleLogic + public interface IHeadsetModuleLogic { List? ReadList(HeadsetModuleSearchModel? model); HeadsetModuleViewModel? ReadElement(HeadsetModuleSearchModel model); diff --git a/FurnitureFactory/FurnitureContracts/BusinessLogicsContracts/IMaterialLogic.cs b/FurnitureFactory/FurnitureContracts/BusinessLogicsContracts/IMaterialLogic.cs index 1712c95..5f60307 100644 --- a/FurnitureFactory/FurnitureContracts/BusinessLogicsContracts/IMaterialLogic.cs +++ b/FurnitureFactory/FurnitureContracts/BusinessLogicsContracts/IMaterialLogic.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; namespace FurnitureContracts.BusinessLogicsContracts { - interface IMaterialLogic + public interface IMaterialLogic { List? ReadList(MaterialSearchModel? model); MaterialViewModel? ReadElement(MaterialSearchModel model); diff --git a/FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/FurnitureLogic.cs b/FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/FurnitureLogic.cs new file mode 100644 index 0000000..644e7b9 --- /dev/null +++ b/FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/FurnitureLogic.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FurnitureContracts.BindingModels; +using FurnitureContracts.BusinessLogicsContracts; +using FurnitureContracts.SearchModels; +using FurnitureContracts.StoragesContracts; +using FurnitureContracts.ViewModel; +using Microsoft.Extensions.Logging; + +namespace FurnitureFactoryBusinessLogic.BusinessLogic +{ + public class FurnitureLogic : IFurnitureLogic + { + private readonly ILogger _logger; + private readonly IFurnitureStorage _FurnitureStorage; + public FurnitureLogic(ILogger logger, IFurnitureStorage FurnitureStorage) + { + _logger = logger; + _FurnitureStorage = FurnitureStorage; + } + public List? ReadList(FurnitureSearchModel? model) + { + _logger.LogInformation("ReadList. Name:{Name}.Id:{ Id}", model?.Name, model?.Id); + var list = model == null ? _FurnitureStorage.GetFullList() : _FurnitureStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public FurnitureViewModel? ReadElement(FurnitureSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Name:{Name}. Id:{ Id}", model.Name, model.Id); + var element = _FurnitureStorage.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(FurnitureBindingModel model) + { + CheckModel(model); + if (_FurnitureStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(FurnitureBindingModel model) + { + CheckModel(model); + if (_FurnitureStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(FurnitureBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_FurnitureStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(FurnitureBindingModel model, bool withParams = + true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Не задано название", nameof(model.Name)); + } + if (model.MasterId <= 0) + { + throw new ArgumentNullException("Неверно задан идентификатор поручителя", nameof(model.MasterId)); + } + if (model.OrdersId <= 0) + { + throw new ArgumentNullException("Неверно задан идентификатор заказа", nameof(model.OrdersId)); + } + _logger.LogInformation("Furniture. Titel:{Name}.ProductId{ProductId}.MasterId:{MasterId}. Id: {Id}", model.Name, model.OrdersId, model.MasterId, model.Id); + var element = _FurnitureStorage.GetElement(new FurnitureSearchModel + { + Name = model.Name, + MasterId = model.MasterId + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Мебель с таким названием уже есть"); + } + } + } +} diff --git a/FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/HeadsetModuleLogic.cs b/FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/HeadsetModuleLogic.cs new file mode 100644 index 0000000..e594bd8 --- /dev/null +++ b/FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/HeadsetModuleLogic.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using FurnitureContracts.BindingModels; +using FurnitureContracts.BusinessLogicsContracts; +using FurnitureContracts.SearchModels; +using FurnitureContracts.StoragesContracts; +using FurnitureContracts.ViewModel; +using FurnitureFactoryDataBaseImplements.Implements; +using Microsoft.Extensions.Logging; + +namespace FurnitureFactoryBusinessLogic.BusinessLogic +{ + public class HeadsetModuleLogic : IHeadsetModuleLogic + { + private readonly ILogger _logger; + private readonly IHeadsetModuleStorage _HeadsetModuleStorage; + public HeadsetModuleLogic(ILogger logger, IHeadsetModuleStorage HeadsetModuleStorage) + { + _logger = logger; + _HeadsetModuleStorage = HeadsetModuleStorage; + } + public List? ReadList(HeadsetModuleSearchModel? model) + { + _logger.LogInformation("ReadList. Name:{Name}.Id:{ Id}", model?.Name, model?.Id); + var list = model == null ? _HeadsetModuleStorage.GetFullList() : _HeadsetModuleStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public HeadsetModuleViewModel? ReadElement(HeadsetModuleSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Name:{Name}. Id:{ Id}", model.Name, model.Id); + var element = _HeadsetModuleStorage.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(HeadsetModuleBindingModel model) + { + CheckModel(model); + if (_HeadsetModuleStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(HeadsetModuleBindingModel model) + { + CheckModel(model); + if (_HeadsetModuleStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(HeadsetModuleBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_HeadsetModuleStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(HeadsetModuleBindingModel model, bool withParams = + true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Не задано название", nameof(model.Name)); + } + if (model.MasterId <= 0) + { + throw new ArgumentNullException("Неверно задан идентификатор поручителя", nameof(model.MasterId)); + } + + _logger.LogInformation("HeadsetModule. Titel:{Name}.MasterId:{MasterId}. Id: {Id}", model.Name, model.MasterId, model.Id); + var element = _HeadsetModuleStorage.GetElement(new HeadsetModuleSearchModel + { + Name = model.Name, + MasterId = model.MasterId + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Модуль с таким названием уже есть"); + } + } + } +} diff --git a/FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/MasterLogic.cs b/FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/MasterLogic.cs new file mode 100644 index 0000000..de0f57e --- /dev/null +++ b/FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/MasterLogic.cs @@ -0,0 +1,121 @@ +using FurnitureContracts.BindingModels; +using FurnitureContracts.BusinessLogicsContracts; +using FurnitureContracts.SearchModels; +using FurnitureContracts.StoragesContracts; +using FurnitureContracts.ViewModel; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureFactoryBusinessLogic.BusinessLogic +{ + public class MasterLogic : IMasterLogic + { + private readonly ILogger _logger; + private readonly IMasterStorage _MasterStorage; + public MasterLogic(ILogger logger, IMasterStorage MasterStorage) + { + _logger = logger; + _MasterStorage = MasterStorage; + } + public List? ReadList(MasterSearchModel? model) + { + _logger.LogInformation("ReadList. Login:{Login}.Id:{ Id}", model?.Login, model?.Id); + var list = model == null ? _MasterStorage.GetFullList() : _MasterStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public MasterViewModel? ReadElement(MasterSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Login:{Login}. Id:{ Id}", model.Login, model.Id); + var element = _MasterStorage.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(MasterBindingModel model) + { + CheckModel(model); + if (_MasterStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(MasterBindingModel model) + { + CheckModel(model); + if (_MasterStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(MasterBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_MasterStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(MasterBindingModel model, bool withParams = + true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Login)) + { + throw new ArgumentNullException("Не задан логин", nameof(model.Login)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Не задан пароль", nameof(model.Password)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Не задана почта", nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Не задано ФИО", nameof(model.Name)); + } + _logger.LogInformation("Master. Name:{Name}.Email{Email}.Login{Login}.Password{Password} Id: {Id}", model.Name, model.Email, model.Login, model.Password, model.Id); + var element = _MasterStorage.GetElement(new MasterSearchModel + { + Login = model.Login + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Поручитель с таким логином уже есть"); + } + } + } +} diff --git a/FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/MaterialLogic.cs b/FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/MaterialLogic.cs new file mode 100644 index 0000000..f0902ca --- /dev/null +++ b/FurnitureFactory/FurnitureFactoryBusinessLogic/BusinessLogic/MaterialLogic.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FurnitureContracts.BindingModels; +using FurnitureContracts.BusinessLogicsContracts; +using FurnitureContracts.SearchModels; +using FurnitureContracts.StoragesContracts; +using FurnitureContracts.ViewModel; + +using Microsoft.Extensions.Logging; + +namespace FurnitureFactoryBusinessLogic.BusinessLogic +{ + public class MaterialLogic : IMaterialLogic + { + private readonly ILogger _logger; + private readonly IMaterialStorage _materialStorage; + public MaterialLogic(ILogger logger, IMaterialStorage materialStorage) + { + _logger = logger; + _materialStorage = materialStorage; + } + public List? ReadList(MaterialSearchModel? model) + { + _logger.LogInformation("ReadList. Name:{Name}.Id:{ Id}", model?.Name, model?.Id); + var list = model == null ? _materialStorage.GetFullList() : _materialStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public MaterialViewModel? ReadElement(MaterialSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Name:{Name}. Id:{ Id}", model.Name, model.Id); + var element = _materialStorage.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(MaterialBindingModel model) + { + CheckModel(model); + if (_materialStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(MaterialBindingModel model) + { + CheckModel(model); + if (_materialStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(MaterialBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_materialStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(MaterialBindingModel model, bool withParams = + true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Не задано название", nameof(model.Name)); + } + if (string.IsNullOrEmpty(model.Cost.ToString())) + { + throw new ArgumentNullException("Не задана цена", nameof(model.Cost)); + } + if (model.MasterId <= 0) + { + throw new ArgumentNullException("Неверно задан идентификатор поручителя", nameof(model.MasterId)); + } + _logger.LogInformation("Material. Titel:{Name}.Cost{Cost}.MasterId:{MasterId}. Id: {Id}", model.Name, model.Cost, model.MasterId, model.Id); + var element = _materialStorage.GetElement(new MaterialSearchModel + { + Name = model.Name, + MasterId = model.MasterId + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Материалы с таким названием уже есть"); + } + } + } +}