diff --git a/Course/BusinessLogic/BusinessLogic/DetailLogic.cs b/Course/BusinessLogic/BusinessLogic/DetailLogic.cs new file mode 100644 index 0000000..85666a5 --- /dev/null +++ b/Course/BusinessLogic/BusinessLogic/DetailLogic.cs @@ -0,0 +1,110 @@ +using Contracts.BindingModels; +using Contracts.BusinessLogicsContracts; +using Contracts.SearchModels; +using Contracts.StoragesContracts; +using Contracts.ViewModels; +using Microsoft.Extensions.Logging; +using System.Xml.Linq; + +namespace BusinessLogic.BusinessLogic +{ + public class DetailLogic : IDetailLogic + { + private readonly ILogger _logger; + private readonly IDetailStorage _detailStorage; + public DetailLogic(ILogger logger, IDetailStorage detailStorage) + { + _logger = logger; + _detailStorage = detailStorage; + } + public List? ReadList(DetailSearchModel? model) + { + _logger.LogInformation("ReadList. DetailName:{name}. Id:{Id}", model.Name, model.Id); + var list = model == null ? _detailStorage.GetFullList() : _detailStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogWarning("ReadList. Count:{Count}", list.Count); + return list; + } + public DetailViewModel? ReadElement(DetailSearchModel? model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. DetailName:{Name}. Id:{Id}", model.Name, model.Id); + var elem = _detailStorage.GetElement(model); + if (elem == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", elem.Id); + return elem; + } + + + private void CheckModel(DetailBindingModel? 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.Cost <= 0) + { + throw new ArgumentNullException("Цена детали должна быть больше 0", nameof(model.Cost)); + } + _logger.LogInformation("Detail. DetailName:{Name}. Cost:{Cost}. Id:{Id}", model.Name, model.Cost, model.Id); + var elem = _detailStorage.GetElement(new DetailSearchModel + { + Name = model.Name + }); + if (elem != null && elem.Id != model.Id) + { + throw new InvalidOperationException("Деталь с таким названием уже существует"); + } + } + public bool Create(DetailBindingModel? model) + { + CheckModel(model); + if (_detailStorage.Insert(model!) == null) + { + _logger.LogWarning("Insert error"); + return false; + } + return true; + } + public bool Update(DetailBindingModel? model) + { + CheckModel(model); + if (_detailStorage.Update(model!) == null) + { + _logger.LogWarning("Update error"); + return false; + } + return true; + } + public bool Delete(DetailBindingModel? model) + { + CheckModel(model, false); + _logger.LogInformation("Delete Id:{Id}", model!.Id); + if (_detailStorage.Delete(model!) == null) + { + _logger.LogWarning("Delete error"); + return false; + } + return true; + } + } +} diff --git a/Course/BusinessLogic/DetailLogic.cs b/Course/BusinessLogic/DetailLogic.cs deleted file mode 100644 index d4aa38d..0000000 --- a/Course/BusinessLogic/DetailLogic.cs +++ /dev/null @@ -1,110 +0,0 @@ -using Contracts.BindingModels; -using Contracts.BusinessLogicsContracts; -using Contracts.SearchModels; -using Contracts.StoragesContracts; -using Contracts.ViewModels; -using Microsoft.Extensions.Logging; -using System.Xml.Linq; - -namespace BusinessLogic -{ - public class DetailLogic : IDetailLogic - { - private readonly ILogger _logger; - private readonly IDetailStorage _detailStorage; - public DetailLogic(ILogger logger, IDetailStorage detailStorage) - { - _logger = logger; - _detailStorage = detailStorage; - } - public List? ReadList(DetailSearchModel? model) - { - _logger.LogInformation("ReadList. DetailName:{name}. Id:{Id}", model.Name, model.Id); - var list = model == null ? _detailStorage.GetFullList() : _detailStorage.GetFilteredList(model); - if (list == null) - { - _logger.LogWarning("ReadList return null list"); - return null; - } - _logger.LogWarning("ReadList. Count:{Count}", list.Count); - return list; - } - public DetailViewModel? ReadElement(DetailSearchModel? model) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - _logger.LogInformation("ReadElement. DetailName:{Name}. Id:{Id}", model.Name, model.Id); - var elem = _detailStorage.GetElement(model); - if (elem == null) - { - _logger.LogWarning("ReadElement element not found"); - return null; - } - _logger.LogInformation("ReadElement find. Id:{Id}", elem.Id); - return elem; - } - - - private void CheckModel(DetailBindingModel? 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.Cost <= 0) - { - throw new ArgumentNullException("Цена детали должна быть больше 0", nameof(model.Cost)); - } - _logger.LogInformation("Detail. DetailName:{Name}. Cost:{Cost}. Id:{Id}", model.Name, model.Cost, model.Id); - var elem = _detailStorage.GetElement(new DetailSearchModel - { - Name = model.Name - }); - if (elem != null && elem.Id != model.Id) - { - throw new InvalidOperationException("Деталь с таким названием уже существует"); - } - } - public bool Create(DetailBindingModel? model) - { - CheckModel(model); - if (_detailStorage.Insert(model!) == null) - { - _logger.LogWarning("Insert error"); - return false; - } - return true; - } - public bool Update(DetailBindingModel? model) - { - CheckModel(model); - if (_detailStorage.Update(model!) == null) - { - _logger.LogWarning("Update error"); - return false; - } - return true; - } - public bool Delete(DetailBindingModel? model) - { - CheckModel(model, false); - _logger.LogInformation("Delete Id:{Id}", model!.Id); - if (_detailStorage.Delete(model!) == null) - { - _logger.LogWarning("Delete error"); - return false; - } - return true; - } - } -}