using CarCenterContracts.BindingModels; using CarCenterContracts.BusinessLogicsContracts; using CarCenterContracts.SearchModels; using CarCenterContracts.StoragesContracts; using CarCenterContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CarCenterBusinessLogic.BusinessLogics { public class EmployeeLogic : IEmployeeLogic { private readonly ILogger _logger; private readonly IEmployeeStorage _EmployeeStorage; public EmployeeLogic(ILogger logger, IEmployeeStorage EmployeeStorage) { _logger = logger; _EmployeeStorage = EmployeeStorage; } public List? ReadList(EmployeeSearchModel? model) { _logger.LogInformation("ReadList. Id:{Id}", model?.Id); var list = model == null ? _EmployeeStorage.GetFullList() : _EmployeeStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public EmployeeViewModel? ReadElement(EmployeeSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Id:{Id}", model.Id); var element = _EmployeeStorage.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(EmployeeBindingModel model) { CheckModel(model); if (_EmployeeStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(EmployeeBindingModel model) { CheckModel(model); if (_EmployeeStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(EmployeeBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_EmployeeStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } private void CheckModel(EmployeeBindingModel 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.Surname)) { throw new ArgumentNullException("Нет фамилии сотрудника", nameof(model.Surname)); } if (string.IsNullOrEmpty(model.Login)) { throw new ArgumentNullException("Нет логина сотрудника", nameof(model.Login)); } if (string.IsNullOrEmpty(model.Password)) { throw new ArgumentNullException("Нет пароля сотрудника", nameof(model.Password)); } _logger.LogInformation("Employee. Surname:{Surname}. Name:{Name}. Login:{Login}. Password:{Password}. Id:{Id}", model.Surname, model.Name, model.Login, model.Password, model.Id); var element = _EmployeeStorage.GetElement(new EmployeeSearchModel { Login = model.Login }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Сотрудник с таким логином уже есть"); } } } }