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 TaskAssigmentLogic :ITaskAssigmentLogic { private readonly ILogger _logger; private readonly ITaskAssigmentStorage _taskAssigmentStorage; public TaskAssigmentLogic(ILogger logger, ITaskAssigmentStorage taskAssigmentStorage) { _logger = logger; _taskAssigmentStorage = taskAssigmentStorage; } public List? ReadList(TaskAssigmentSearchModel? model) { var list = model == null ? _taskAssigmentStorage.GetFullList() : _taskAssigmentStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public List? ReadFilteredEmployeeList(EmployeeSearchModel? model) { var list = _taskAssigmentStorage.GetFilteredAssigmentList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public TaskAssigmentViewModel? ReadElement(TaskAssigmentSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _taskAssigmentStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } return element; } public bool Create(TaskAssigmentBindingModel model) { CheckModel(model); if (_taskAssigmentStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(TaskAssigmentBindingModel model) { CheckModel(model); if (_taskAssigmentStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(TaskAssigmentBindingModel model) { CheckModel(model, false); if (_taskAssigmentStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } private void CheckModel(TaskAssigmentBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.Role)) { throw new ArgumentNullException("Нет имени", nameof(model.Role)); } var element = _taskAssigmentStorage.GetElement(new TaskAssigmentSearchModel { TaskId = model.TaskId, EmployeeId = model.EmployeeId }); } } }