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 ProviderLogic : IProviderLogic { private readonly IProviderStorage _providerStorage; public ProviderLogic(IProviderStorage providerStorage) { _providerStorage = providerStorage; } public bool Delete(ProviderBindingModel model) { CheckModel(model, false); if (_providerStorage.Delete(model) == null) { return false; } return true; } public bool Create(ProviderBindingModel model) { CheckModel(model); if (_providerStorage.Insert(model) == null) { return false; } return true; } public bool Update(ProviderBindingModel model) { CheckModel(model); if (_providerStorage.Update(model) == null) { return false; } return true; } private void CheckModel(ProviderBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.Name)) { throw new ArgumentNullException("Нет названия поставщика", nameof(model.Name)); } if (string.IsNullOrEmpty(model.Address)) { throw new ArgumentNullException("Нет адресса", nameof(model.Address)); } var element = _providerStorage.GetElement(new ProviderSearchModel { Name = model.Name, }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Поставщик с таким названием уже есть"); } } public List? ReadList(ProviderSearchModel? model) { var list = (model == null) ? _providerStorage.GetFullList() : _providerStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public ProviderViewModel? ReadElement(ProviderSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _providerStorage.GetElement(model); if (element == null) { return null; } return element; } } }