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 ProjectLogic : IProjectLogic { private readonly IProjectStorage _projectStorage; public ProjectLogic(IProjectStorage projectStorage) { _projectStorage = projectStorage; } public List? ReadList(ProjectSearchModel? model) { var list = model == null ? _projectStorage.GetFullList() : _projectStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public ProjectViewModel? ReadElement(ProjectSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _projectStorage.GetElement(model); if (element == null) { return null; } return element; } public bool Create(ProjectBindingModel model) { CheckModel(model); if (_projectStorage.Insert(model) == null) { return false; } return true; } public bool Update(ProjectBindingModel model) { CheckModel(model); if (_projectStorage.Update(model) == null) { return false; } return true; } public bool Delete(ProjectBindingModel model) { CheckModel(model, false); if (_projectStorage.Delete(model) == null) { return false; } return true; } private void CheckModel(ProjectBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.ProjectName)) { throw new ArgumentNullException("Нет названия", nameof(model.ProjectName)); } var element = _projectStorage.GetElement(new ProjectSearchModel { ProjectName = model.ProjectName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Проект с таким названием уже есть"); } } } }