using SYBDContracts.BindingModels; using SYBDContracts.BusinessLogicsContracts; using SYBDContracts.SearchModels; using SYBDContracts.StoragesContracts; using SYBDContracts.ViewModels; using Microsoft.Extensions.Logging; namespace SYBDBusinessLogic.BusinessLogics { public class WorkerLogic : IWorkerLogic { private readonly ILogger _logger; private readonly IWorkerStorage _workerStorage; public WorkerLogic(ILogger logger, IWorkerStorage workerStorage) { _logger = logger; _workerStorage = workerStorage; } public bool Create(WorkerBindingModel model) { CheckModel(model); CheckModel(model); if (_workerStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(WorkerBindingModel model) { CheckModel(model); if (_workerStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(WorkerBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_workerStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public WorkerViewModel? ReadElement(WorkerSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Fullname:{Fullname}. Id:{Id}", model.Fullname, model.Id); var element = _workerStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); return element; } public List? ReadList(WorkerSearchModel? model) { _logger.LogInformation("ReadList. Fullname:{Fullname}. Id:{Id}", model?.Fullname, model?.Id); var list = model == null ? _workerStorage.GetFullList() : _workerStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } private void CheckModel(WorkerBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.Fullname)) { throw new ArgumentNullException("Нет ФИО работника", nameof(model.Fullname)); } if (string.IsNullOrEmpty(model.Address)) { throw new ArgumentNullException("Нет адреса работника", nameof(model.Address)); } if (model.Salary <= 0) { throw new ArgumentNullException("Зарплата работника должна быть больше 0", nameof(model.Salary)); } _logger.LogInformation("Component. Fullname:{Fullname}. Address:{Address}.Salary:{Salary}. Id:{Id}", model.Fullname, model.Address, model.Salary, model.Id); var element = _workerStorage.GetElement(new WorkerSearchModel { Fullname = model.Fullname }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Работа с таким названием уже есть"); } } } }