using BeautySaloonContracts.BindingModels; using BeautySaloonContracts.BusinessLogicsContracts; using BeautySaloonContracts.SearchModels; using BeautySaloonContracts.StoragesContracts; using BeautySaloonContracts.ViewModels; namespace BeautySaloonBusinessLogic { public class ServiceLogic : IServiceLogic { private readonly IServiceStorage _serviceStorage; public ServiceLogic(IServiceStorage serviceStorage) { _serviceStorage = serviceStorage; } public bool Create(ServiceBindingModel model) { CheckModel(model); if (_serviceStorage.Insert(model) == null) { return false; } return true; } public bool Delete(ServiceBindingModel model) { CheckModel(model, false); if (_serviceStorage.Delete(model) == null) { return false; } return true; } public ServiceViewModel? ReadElement(ServiceSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _serviceStorage.GetElement(model); if (element == null) { return null; } return element; } public List? ReadList(ServiceSearchModel? model) { var list = model == null ? _serviceStorage.GetFullList() : _serviceStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public bool Update(ServiceBindingModel model) { CheckModel(model); if (_serviceStorage.Update(model) == null) { return false; } return true; } private void CheckModel(ServiceBindingModel 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)); } var element = _serviceStorage.GetElement(new ServiceSearchModel { Name = model.Name }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Услуга с таким названием уже есть"); } } } }