using SushiBarContracts.BindingModels; using SushiBarContracts.BusinessLogicContracts; using SushiBarContracts.SearchModels; using SushiBarContracts.StoragesContracts; using SushiBarContracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SushiBarBusinessLogic.BusinessLogics { public class BuyerLogic : IBuyerLogic { private readonly IBuyerStorage _buyerStorage; public BuyerLogic(IBuyerStorage buyerStorage) { _buyerStorage = buyerStorage; } public BuyerViewModel? ReadElement(BuyerSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _buyerStorage.GetElement(model); if(element == null) { return null; } return element; } public List? ReadList(BuyerSearchModel? model) { var list = model == null ? _buyerStorage.GetFullList() : _buyerStorage.GetFilteredList(model); if(list == null) { return null; } return list; } public bool Create(BuyerBindingModel model) { CheckModel(model); if (_buyerStorage.Insert(model) == null) { return false; } return true; } public bool Delete(BuyerBindingModel model) { CheckModel(model, false); if(_buyerStorage.Delete(model) == null) { return false; } return true; } public bool Update(BuyerBindingModel model) { CheckModel(model); if(_buyerStorage.Update(model) == null) { return false; } return true; } private void CheckModel(BuyerBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.BuyerName)) { throw new ArgumentNullException("Нет названия", nameof(model.BuyerName)); } } public void ClearEntity() { _buyerStorage.ClearEntity(); } } }