using AbstractSoftwareInstallationContracts.BusinessLogicsContracts; using AbstractSoftwareInstallationContracts.BindingModels; using AbstractSoftwareInstallationContracts.SearchModels; using AbstractSoftwareInstallationContracts.ViewModels; using AbstractSoftwareInstallationContracts.StoragesContracts; using Microsoft.Extensions.Logging; namespace AbstractSoftwareInstallationBusinessLogic { public class SoftwareLogic : ISoftwareLogic { private readonly ILogger _logger; private readonly ISoftwareStorage _softwareStorage; public SoftwareLogic(ILogger logger, ISoftwareStorage SoftwareStorage) { _logger = logger; _softwareStorage = SoftwareStorage; } public List? ReadList(SoftwareSearchModel? model) { _logger.LogInformation("ReadList. SoftwareName:{SoftwareName}.Id:{ Id}", model?.SoftwareName, model?.Id); var list = model == null ? _softwareStorage.GetFullList() : _softwareStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public SoftwareViewModel? ReadElement(SoftwareSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. SoftwareName:{SoftwareName}.Id:{ Id}", model.SoftwareName, model.Id); var element = _softwareStorage.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(SoftwareBindingModel model) { CheckModel(model); if (_softwareStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(SoftwareBindingModel model) { CheckModel(model); if (_softwareStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(SoftwareBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_softwareStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } private void CheckModel(SoftwareBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.SoftwareName)) { throw new ArgumentNullException("Нет названия ПО", nameof(model.SoftwareName)); } if (model.Cost <= 0) { throw new ArgumentNullException("Цена ПО должна быть больше 0", nameof(model.Cost)); } _logger.LogInformation("Software. SoftwareName:{SoftwareName}.Cost:{ Cost}. Id: { Id}", model.SoftwareName, model.Cost, model.Id); var element = _softwareStorage.GetElement(new SoftwareSearchModel { SoftwareName = model.SoftwareName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("ПО с таким названием уже есть"); } } }}