using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UniversityContracts.BindingModels; using UniversityContracts.BusinessLogicContracts; using UniversityContracts.SearchModels; using UniversityContracts.StoragesContracts; using UniversityContracts.ViewModels; namespace UniversityBusinessLogic.BusinessLogics { public class DisciplineLogic : IDisciplineLogic { private readonly IDisciplineStorage _disciplineStorage; public DisciplineLogic(IDisciplineStorage disciplineStorage) { _disciplineStorage = disciplineStorage; } public bool Create(DisciplineBindingModel model) { CheckModel(model); if (_disciplineStorage.Insert(model) == null) { return false; } return true; } public bool Delete(DisciplineBindingModel model) { CheckModel(model, false); if (_disciplineStorage.Delete(model) == null) { return false; } return true; } public DisciplineViewModel? ReadElement(DisciplineSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var discipline = _disciplineStorage.GetElement(model); if (discipline == null) { return null; } return discipline; } public List? ReadList(DisciplineSearchModel? model) { var list = model == null ? _disciplineStorage.GetFullList() : _disciplineStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public bool Update(DisciplineBindingModel model) { CheckModel(model, false); if (_disciplineStorage.Update(model) == null) { return false; } return true; } public int GetNumberOfPages(int userId, int pageSize = 10) { return _disciplineStorage.GetNumberOfPages(userId, pageSize); } private void CheckModel(DisciplineBindingModel 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 discipline = _disciplineStorage.GetElement(new DisciplineSearchModel { Name = model.Name }); if (discipline != null) { throw new InvalidOperationException("Дисциплина с таким названием уже есть"); } } } }