using RestaurantContracts.BindingModels; using RestaurantContracts.BusinessLogicsContracts; using RestaurantContracts.SearchModels; using RestaurantContracts.StoragesContracts; using RestaurantContracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RestaurantBusinessLogic.BusinessLogics { public class ProductLogic : IProductLogic { private readonly IProductStorage _productStorage; public ProductLogic(IProductStorage productStorage) { _productStorage = productStorage; } public bool Create(ProductBindingModel model) { CheckModel(model); if (_productStorage.Insert(model) == null) { return false; } return true; } public bool Delete(ProductBindingModel model) { CheckModel(model, false); if (_productStorage.Delete(model) == null) { return false; } return true; } public ProductViewModel? ReadElement(ProductSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _productStorage.GetElement(model); if (element == null) { return null; } return element; } public List? ReadList(ProductSearchModel? model) { var list = model == null ? _productStorage.GetFullList() : _productStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public bool Update(ProductBindingModel model) { CheckModel(model); if (_productStorage.Update(model) == null) { return false; } return true; } private void CheckModel(ProductBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.Type)) { throw new ArgumentNullException("Нет вида продукта", nameof(model.Type)); } if (model.Price <= 0) { throw new ArgumentNullException("Нет цены у продукта", nameof(model.Price)); } if (model.Count <= 0) { throw new ArgumentNullException("Нет количества у продукта", nameof(model.Count)); } var element = _productStorage.GetElement(new ProductSearchModel { Type = model.Type, }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Компонент с таким логином уже есть"); } } } }