using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TaskTrackerContracts.BindingModels; using TaskTrackerContracts.BusinessLogicContracts; using TaskTrackerContracts.SearchModels; using TaskTrackerContracts.StoragesContracts; using TaskTrackerContracts.ViewModels; namespace TaskTrackerBusinessLogic { public class CompanyLogic : ICompanyLogic { private readonly ILogger _logger; private readonly ICompanyStorage _companyStorage; public CompanyLogic(ILogger logger, ICompanyStorage companyStorage) { _logger = logger; _companyStorage = companyStorage; } public List? ReadList(CompanySearchModel? model) { _logger.LogInformation("ReadList. CompanyName:{CompanyName}.Id:{ Id}", model?.Name, model?.Id); var list = model == null ? _companyStorage.GetFullList() : _companyStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public CompanyViewModel? ReadElement(CompanySearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. CompanyName:{CompanyName}. Id:{ Id}", model.Name, model.Id); var element = _companyStorage.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(CompanyBindingModel model) { CheckModel(model); if (_companyStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(CompanyBindingModel model) { CheckModel(model); if (_companyStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(CompanyBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_companyStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } private void CheckModel(CompanyBindingModel 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.Login)) { throw new ArgumentNullException("Нет логина ", nameof(model.Name)); } if (string.IsNullOrEmpty(model.Password)) { throw new ArgumentNullException("Нет пароля", nameof(model.Name)); } var element = _companyStorage.GetElement(new CompanySearchModel { Name = model.Name, }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Компонент с таким названием уже есть"); } } } }