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.Runtime.CompilerServices; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; namespace ServiceSourceBusinessLogic.BusinessLogic { public class ClientLogic : IClientLogic { private readonly ILogger _logger; private readonly IClientStorage _storage; public ClientLogic(ILogger logger, IClientStorage storage) { _logger = logger; _storage = storage; } public bool Create(ClientBindingModel model) { CheckModel(model); if (_storage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(ClientBindingModel 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(ClientBindingModel model) { CheckModel(model); if (_storage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public ClientViewModel? ReadElement(ClientSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation($"ReadElement.Id:{model.Id}.login:{model.Login}"); 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() { 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(ClientBindingModel model, bool withParams = true) { if (string.IsNullOrEmpty((model.Id).ToString())) { throw new ArgumentNullException("Нет Id клиента", nameof(model.Id)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.FIO)) { throw new ArgumentNullException("Нет ФИО клиента", nameof(model.FIO)); } if (string.IsNullOrEmpty(model.Password)) { throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password)); } if (string.IsNullOrEmpty(model.Login)) { throw new ArgumentNullException("Нет номера телефона пользователя", nameof(model.Login)); } _logger.LogInformation($"Client.Id:{model.Id}.FIO:{model.FIO}.Password:{model.Password}.Login:{model.Login}"); var element = _storage.GetElement(new ClientSearchModel { Login = model.Login}); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Такой логин пользователя уже есть"); } } } }