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 ExecutorLogic : IExecutorLogic { private readonly ILogger _logger; private readonly IExecutorStorage _storage; public ExecutorLogic(ILogger logger, IExecutorStorage storage) { _logger = logger; _storage = storage; } public bool Create(ExecutorBindingModel model) { CheckModel(model); if (_storage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(ExecutorBindingModel model) { CheckModel(model, false); _logger.LogInformation($"Delete.Id:{model.Id}"); if (_storage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public bool Update(ExecutorBindingModel model) { CheckModel(model); if (_storage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public ExecutorViewModel? ReadElement(ExecutorSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation($"ReadElement.Id:{model.Id}.LastName:{model.LastName}"); var element = _storage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement. element not fount"); return null; } _logger.LogInformation($"ReadElement.find.Id:{element.Id}"); return element; } public List? ReadList(ExecutorSearchModel? model = null) { _logger.LogInformation($"ReadList.ClientId:{model?.Id}"); var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList. return null list"); return null; } _logger.LogInformation($"ReadList.Count:{list.Count}"); return list; } private void CheckModel(ExecutorBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty((model.Id).ToString())) { throw new ArgumentNullException("Нет Id исполнителя", nameof(model.Id)); } if (string.IsNullOrEmpty(model.FirstName)) { throw new ArgumentNullException("Нет имени пользователя", nameof(model.FirstName)); } if (string.IsNullOrEmpty(model.LastName)) { throw new ArgumentNullException("Нет фамилии пользователя", nameof(model.LastName)); } if (string.IsNullOrEmpty(model.Email)) { throw new ArgumentNullException("Нет почты клиента", nameof(model.Email)); } if (string.IsNullOrEmpty(model.Password)) { throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password)); } if (string.IsNullOrEmpty(model.PhoneNumber)) { throw new ArgumentNullException("Нет номера телефона пользователя", nameof(model.PhoneNumber)); } _logger.LogInformation($"Client. Id:{model.Id}.FirstName:{model.FirstName}.LastName:{model.LastName}." + $"Email:{model.Email}.Password:{model.Password}.PhoneNumber:{model.PhoneNumber}.Login:{model.Login}"); var element = _storage.GetElement(new ExecutorSearchModel { Email = model.Email }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Такая почта уже имеется"); } } } }