using ComputerHardwareStoreContracts.BindingModels; using ComputerHardwareStoreContracts.BusinessLogicsContracts; using ComputerHardwareStoreContracts.SearchModels; using ComputerHardwareStoreContracts.StorageContracts; using ComputerHardwareStoreContracts.ViewModels; using Microsoft.Extensions.Logging; namespace ComputerHardwareStoreBusinessLogic.BusinessLogic { public class CommentLogic : ICommentLogic { private readonly ILogger _logger; private readonly ICommentStorage _commentStorage; public CommentLogic(ILogger logger, ICommentStorage commentStorage) { _logger = logger; _commentStorage = commentStorage; } public List? ReadList(CommentSearchModel? model) { _logger.LogInformation("ReadList Comment. Id:{Id}", model?.Id); var list = model == null ? _commentStorage.GetFullList() : _commentStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList Comment return null list"); return null; } _logger.LogInformation("ReadList Comment. Count:{Count}", list.Count); return list; } public CommentViewModel? ReadElement(CommentSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement Comment. Id:{ Id}", model.Id); var element = _commentStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement Comment element not found"); return null; } _logger.LogInformation("ReadElement Comment find. Id:{Id}", element.Id); return element; } public bool Create(CommentBindingModel model) { CheckModel(model); if (_commentStorage.Insert(model) == null) { _logger.LogWarning("Insert Comment operation failed"); return false; } return true; } public bool Update(CommentBindingModel model) { CheckModel(model); if (_commentStorage.Update(model) == null) { _logger.LogWarning("Update Comment operation failed"); return false; } return true; } public bool Delete(CommentBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_commentStorage.Delete(model) == null) { _logger.LogWarning("Delete Comment operation failed"); return false; } return true; } private void CheckModel(CommentBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.Text)) { throw new ArgumentNullException("Нет текста комментария", nameof(model.Text)); } } } }