using CarRentContracts.BindingModels; using CarRentContracts.BusinessLogicContracts; using CarRentContracts.SearchModels; using CarRentContracts.StoragesContracts; using CarRentContracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CarRentBusinessLogic.BusinessLogics { public class BranchLogic : IBranchLogic { private readonly IBranchStorage _branchStorage; public BranchLogic(IBranchStorage branchStorage) { _branchStorage = branchStorage; } public List? ReadList(BranchSearchModel? model) { var list = model == null ? _branchStorage.GetFullList() : _branchStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public BranchViewModel? ReadElement(BranchSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _branchStorage.GetElement(model); if (element == null) { return null; } return element; } public bool Create(BranchBindingModel model) { CheckModel(model); if (_branchStorage.Insert(model) == null) { return false; } return true; } public bool Update(BranchBindingModel model) { CheckModel(model); if (_branchStorage.Update(model) == null) { return false; } return true; } public bool Delete(BranchBindingModel model) { CheckModel(model, false); if (_branchStorage.Delete(model) == null) { return false; } return true; } private void CheckModel(BranchBindingModel 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)); } if (string.IsNullOrEmpty(model.PhoneNumber)) { throw new ArgumentNullException("Нет номера телефона филиала", nameof(model.PhoneNumber)); } var element = _branchStorage.GetElement( new BranchSearchModel { Name = model.Name } ); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Филиал с таким названием уже есть"); } } } }