using SecureShopContracts.BindingModels; using SecureShopContracts.BusinessLogicsContracts; using SecureShopContracts.SearchModels; using SecureShopContracts.StoragesContracts; using SecureShopContracts.ViewModels; using Microsoft.Extensions.Logging; namespace SecureShopBusinessLogic.BusinessLogics { public class SecureLogic : ISecureLogic { private readonly ILogger _logger; private readonly ISecureStorage _SecureStorage; public SecureLogic(ILogger logger, ISecureStorage SecureStorage) { _logger = logger; _SecureStorage = SecureStorage; } public List? ReadList(SecureSearchModel? model) { _logger.LogInformation("ReadList. SecureName:{SecureName}.Id:{Id}", model?.SecureName, model?.Id); var list = model == null ? _SecureStorage.GetFullList() : _SecureStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public SecureViewModel? ReadElement(SecureSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. SecureName:{SecureName}. Id:{Id}", model.SecureName, model.Id); var element = _SecureStorage.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(SecureBindingModel model) { CheckModel(model); if (_SecureStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(SecureBindingModel model) { CheckModel(model); if (_SecureStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(SecureBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_SecureStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } private void CheckModel(SecureBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.SecureName)) { throw new ArgumentNullException("Нет названия Сиситемы безопасности ", nameof(model.SecureName)); } if (model.Price <= 0) { throw new ArgumentNullException("Цена Сиситемы безопасности должна быть больше 0", nameof(model.Price)); } _logger.LogInformation("Secure. SecureName:{SecureName}. Cost:{Cost}. Id: {Id}", model.SecureName, model.Price, model.Id); var element = _SecureStorage.GetElement(new SecureSearchModel { SecureName = model.SecureName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Сиситема безопасности с таким названием уже есть"); } } } }