using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TaskTrackerContracts.BindingModels; using TaskTrackerContracts.BusinessLogicsContracts; using TaskTrackerContracts.SearchModels; using TaskTrackerContracts.StoragesContracts; using TaskTrackerContracts.ViewModels; namespace TaskTrackerBusinessLogics.BusinessLogic { public class ResultLogic : IResultLogic { private readonly IResultStorage _resultStorage; public ResultLogic(IResultStorage resultStorage) { _resultStorage = resultStorage; } public List? ReadList(ResultSearchModel? model) { var list = model == null ? _resultStorage.GetFullList() : _resultStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public ResultViewModel? ReadElement(ResultSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _resultStorage.GetElement(model); if (element == null) { return null; } return element; } public bool Create(ResultBindingModel model) { CheckModel(model); if (_resultStorage.Insert(model) == null) { return false; } return true; } public bool Update(ResultBindingModel model) { CheckModel(model); if (_resultStorage.Update(model) == null) { return false; } return true; } public bool Delete(ResultBindingModel model) { CheckModel(model, false); if (_resultStorage.Delete(model) == null) { return false; } return true; } private void CheckModel(ResultBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } var element = _resultStorage.GetElement(new ResultSearchModel { StudentId = model.StudentId, SubjectId = model.SubjectId, }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Проект с таким названием уже есть"); } } } }