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 DocumentLogic : IDocumentLogic { private readonly IDocumentStorage _documentStorage; public DocumentLogic(IDocumentStorage documentStorage) { _documentStorage = documentStorage; } public bool Create(DocumentBindingModel model) { CheckModel(model); if(_documentStorage.Insert(model) == null) { return false; } return true; } public bool Update(DocumentBindingModel model) { CheckModel(model, false); if (_documentStorage.Update(model) == null) { return false; } return true; } public bool Delete(DocumentBindingModel model) { CheckModel(model, false); if (_documentStorage.Delete(model) == null) { return false; } return true; } public DocumentViewModel? ReadElement(DocumentSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var document = _documentStorage.GetElement(model); if (document == null) { return null; } return document; } public List? ReadList(DocumentSearchModel? model) { var list = model == null ? _documentStorage.GetFullList() : _documentStorage.GetFilteredList(model); if (list == null) { return null; } return list; } private void CheckModel(DocumentBindingModel 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 (model.Date > DateTime.Now) { throw new ArgumentNullException("Неверно указана дата", nameof(model.Name)); } var document = _documentStorage.GetElement(new DocumentSearchModel { Name = model.Name, }); if (document != null && document.Id != model.Id) { throw new InvalidOperationException("Приказ с таким названием уже есть"); } } } }