using BlogContracts.BindingModel; using BlogContracts.BusinessLogicContracts; using BlogContracts.SearchModels; using BlogContracts.StorageContracts; using BlogContracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BusinessLogic { public class CommentLogic : ICommentLogic { private readonly ICommentStorage _commentStorage; public CommentLogic(ICommentStorage CommentStorage) { _commentStorage = CommentStorage ?? throw new ArgumentNullException(nameof(CommentStorage)); } public bool Create(CommentBindingModel model) { CheckModel(model); if (_commentStorage.Insert(model) == null) { return false; } return true; } public bool Delete(CommentBindingModel model) { CheckModel(model); if (_commentStorage.Delete(model) == null) { return false; } return true; } public CommentViewModel? ReadElement(CommentSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _commentStorage.GetElement(model); if (element == null) { return null; } return element; } public List? ReadList(CommentSearchModel? model) { var list = model == null ? _commentStorage.GetFullList() : _commentStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public bool Update(CommentBindingModel model) { CheckModel(model); if (_commentStorage.Update(model) == null) { return false; } return true; } private void CheckModel(CommentBindingModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (string.IsNullOrEmpty(model.Text)) { throw new ArgumentException("Отсутвует текст", nameof(model.Text)); } if (_commentStorage.GetElement(new CommentSearchModel { Text = model.Text, }) != null) { throw new InvalidOperationException("Такой тег уже существует"); } } } }