diff --git a/STOBusinessLogic/TechnicalWorkLogic.cs b/STOBusinessLogic/TechnicalWorkLogic.cs new file mode 100644 index 0000000..0f47e3b --- /dev/null +++ b/STOBusinessLogic/TechnicalWorkLogic.cs @@ -0,0 +1,126 @@ +using Microsoft.Extensions.Logging; +using STOContracts.BindingModels; +using STOContracts.BusinessLogicsContracts; +using STOContracts.SearchModels; +using STOContracts.StoragesContracts; +using STOContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace STOBusinessLogic +{ + public class TechnicalWorkLogic : ITechnicalWorkLogic + { + private readonly ILogger _logger; + private readonly ITechnicalWorkStorage _TechnicalWorkStorage; + public TechnicalWorkLogic(ILogger logger, ITechnicalWorkStorage TechnicalWorkStorage) + { + _logger = logger; + _TechnicalWorkStorage = TechnicalWorkStorage; + } + + public List? ReadList(TechnicalWorkSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); + var list = model == null ? _TechnicalWorkStorage.GetFullList() : _TechnicalWorkStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public TechnicalWorkViewModel? ReadElement(TechnicalWorkSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement.Id:{ Id}", model.Id); + var element = _TechnicalWorkStorage.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(TechnicalWorkBindingModel model) + { + CheckModel(model); + if (_TechnicalWorkStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(TechnicalWorkBindingModel model) + { + CheckModel(model); + if (_TechnicalWorkStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(TechnicalWorkBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_TechnicalWorkStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(TechnicalWorkBindingModel 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.Description)) + { + throw new ArgumentNullException("Нет описания для ТО", + nameof(model.Name)); + } + //по умолчанию если дата не была присвоена вручную то мы ей ставили минимальное значение + if (model.Date == DateTime.MinValue) + { + throw new ArgumentNullException("Нет даты", + nameof(model.Date)); + } + _logger.LogInformation("TechnicalWork. Name: {Name}.Id: { Id}", model.Name, model.Id); + //имена ТО должны быть уникальными? + var element = _TechnicalWorkStorage.GetElement(new TechnicalWorkSearchModel + { + Name = model.Name + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("ТО с таким названием уже есть"); + } + } + } +} diff --git a/STOBusinessLogic/WorkLogic.cs b/STOBusinessLogic/WorkLogic.cs new file mode 100644 index 0000000..8cd25a9 --- /dev/null +++ b/STOBusinessLogic/WorkLogic.cs @@ -0,0 +1,101 @@ +using Microsoft.Extensions.Logging; +using STOContracts.BindingModels; +using STOContracts.BusinessLogicContracts; +using STOContracts.SearchModels; +using STOContracts.StorageContracts; +using STOContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace STOBusinessLogic +{ + public class WorkLogic : IWorkLogic + { + private readonly ILogger _logger; + private readonly IWorkStorage _WorkStorage; + public WorkLogic(ILogger logger, IWorkStorage WorkStorage) + { + _logger = logger; + _WorkStorage = WorkStorage; + } + + public List? ReadList(WorkSearchModel? model) + { + _logger.LogInformation("ReadList.Id:{ Id}", model?.Id); + var list = model == null ? _WorkStorage.GetFullList() : _WorkStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public WorkViewModel? ReadElement(WorkSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadList.Id:{ Id}", model.Id); + var element = _WorkStorage.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(WorkBindingModel model) + { + CheckModel(model); + if (_WorkStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(WorkBindingModel model) + { + CheckModel(model); + if (_WorkStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(WorkBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_WorkStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(WorkBindingModel model, bool withParams = + true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + _logger.LogInformation("ReadList.Id:{ Id}", model.Id); + } + } +}