using BankContracts.BindingModels; using BankContracts.BusinessLogicsContracts; using BankContracts.SearchModels; using BankContracts.StoragesContracts; using BankContracts.ViewModels; using Microsoft.Extensions.Logging; namespace BankBusinessLogic.BusinessLogics { public class ClercLogic : IClercLogic { private readonly int _loginMaxLength = 50; private readonly int _passwordMaxLength = 50; private readonly int _passwordMinLength = 10; private readonly ILogger _logger; private readonly IClercStorage _clercStorage; public ClercLogic(ILogger logger, IClercStorage clercStorage) { _logger = logger; _clercStorage = clercStorage; } public bool Create(ClercBindingModel model) { CheckModel(model); if (_clercStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(ClercBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id: {Id}", model.Id); if (_clercStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public ClercViewModel? ReadElement(ClercSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. ClercFIO: {ClercFIO}. ClercLogin: {ClercLogin}. Id: {Id}.", model.ClercFIO, model.ClercLogin, model.Id); var element = _clercStorage.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(ClercSearchModel? model) { _logger.LogInformation("ReadList. ClercFIO: {ClercFIO}. ClercLogin: {ClercLogin}. Id: {Id}.", model?.ClercFIO, model?.ClercLogin, model?.Id); var list = model == null ? _clercStorage.GetFullList() : _clercStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } public bool Update(ClercBindingModel model) { CheckModel(model); if (_clercStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } private void CheckModel(ClercBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.ClercFIO)) { throw new ArgumentNullException("Нет ФИО организатора", nameof(model.ClercFIO)); } if (string.IsNullOrEmpty(model.ClercLogin)) { throw new ArgumentNullException("Нет логина организатора", nameof(model.ClercLogin)); } if (model.ClercLogin.Length>_loginMaxLength) { throw new ArgumentNullException("Логин слишком длинный", nameof(model.ClercLogin)); } if (string.IsNullOrEmpty(model.ClercNumber)) { throw new ArgumentNullException("Нет номера телефона организатора", nameof(model.ClercNumber)); } if (string.IsNullOrEmpty(model.ClercEmail)) { throw new ArgumentNullException("Нет почты организатора", nameof(model.ClercEmail)); } if (string.IsNullOrEmpty(model.ClercPassword)) { throw new ArgumentNullException("Нет пароля организатора", nameof(model.ClercPassword)); } if (model.ClercPassword.Length < _passwordMinLength) { throw new ArgumentNullException("Пароль слишком короткий", nameof(model.ClercPassword)); } if (model.ClercPassword.Length > _passwordMaxLength) { throw new ArgumentNullException("Пароль слишком длинный", nameof(model.ClercPassword)); } _logger.LogInformation("Clerc. ClercFIO: {ClercFIO}. ClercLogin: {ClercLogin}. Id: {Id}", model.ClercFIO, model.ClercLogin, model.Id); var element = _clercStorage.GetElement(new ClercSearchModel { ClercLogin = model.ClercLogin }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Организатор с таким логином уже есть"); } } } }