using Microsoft.Extensions.Logging; using ServiceStationContracts.BindingModels; using ServiceStationContracts.BusinessLogicContracts; using ServiceStationContracts.SearchModels; using ServiceStationContracts.ViewModels; using ServiceStationsContracts.StorageContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServiceSourceBusinessLogic.BusinessLogic { public class TaskLogic : ITaskLogic { private readonly ILogger _logger; private readonly ITaskStorage _storage; public TaskLogic(ILogger logger, ITaskStorage storage) { _logger = logger; _storage = storage; } public bool Create(TaskBindingModel model) { CheckModel(model); if (_storage.Insert(model) == null) { _logger.LogInformation("Insert operatin failed"); return false; } return true; } public bool Update(TaskBindingModel model) { CheckModel(model); if (_storage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(TaskBindingModel model) { CheckModel(model); _logger.LogInformation($"Delete.Id:{model.Id}"); if (_storage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public TaskViewModel? ReadElement(TaskSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation($"ReadElement.Id:{model.Id}"); var element = _storage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement.Element not found"); return null; } _logger.LogInformation($"ReadElement.Find.Id:{element.Id}"); return element; } public List? ReadList(TaskSearchModel model) { _logger.LogInformation($"ReadList.Id:{model.Id}"); var list = _storage.GetFullList(); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation($"ReadList.Count:{list.Count}"); return list; } private void CheckModel (TaskBindingModel model, bool withParams = true) { if (string.IsNullOrEmpty(model.Id.ToString())) { throw new ArgumentNullException("Нет номера задачи", nameof(model.Id)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.Name)) { throw new ArgumentNullException("Нет номера модели", nameof(model.Name)); } _logger.LogInformation($"Task.Id:{model.Id}.Name:{model.Name}"); var task = _storage.GetElement(new TaskSearchModel { Name = model.Name }); if (task != null && task.Id != model.Id) { throw new InvalidOperationException("Такая задача уже создана"); } } } }