using PersonnelDepartmentContracts.BindingModels; using PersonnelDepartmentContracts.BusinessLogicContracts; using PersonnelDepartmentContracts.SearchModels; using PersonnelDepartmentContracts.StoragesContracts; using PersonnelDepartmentContracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PersonnelDepartmentBusinessLogic.BusinessLogics { public class DealLogic : IDealLogic { private readonly IDealStorage _dealStorage; public DealLogic(IDealStorage dealStorage) { _dealStorage = dealStorage ?? throw new ArgumentNullException(nameof(dealStorage)); } public bool Create(DealBindingModel model) { CheckModel(model); if (_dealStorage.Insert(model) == null) { return false; } return true; } public bool Delete(DealBindingModel model) { CheckModel(model); if (_dealStorage.Delete(model) == null) { return false; } return true; } public DealViewModel? ReadElement(DealSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _dealStorage.GetElement(model); if (element == null) { return null; } return element; } public List? ReadList(DealSearchModel? model) { var list = model == null ? _dealStorage.GetFullList() : _dealStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public bool Update(DealBindingModel model) { CheckModel(model); if (_dealStorage.Update(model) == null) { return false; } return true; } private void CheckModel(DealBindingModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (model.DepartmentId < 0) { throw new ArgumentNullException("Некорректный идентификатор отдела", nameof(model.DepartmentId)); } if (model.EmployeeId < 0) { throw new ArgumentNullException("Некорректный идентификатор сотрудника", nameof(model.EmployeeId)); } if (model.PositionId < 0) { throw new ArgumentNullException("Некорректный идентификатор должности", nameof(model.PositionId)); } if (model.TypeId < 0) { throw new ArgumentNullException("Некорректный идентификатор типа договора", nameof(model.TypeId)); } if (_dealStorage.GetElement(new DealSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo, DepartmentId = model.DepartmentId, EmployeeId = model.EmployeeId, PositionId = model.PositionId, TypeId = model.TypeId }) != null) { throw new InvalidOperationException("Договор с такими атрибутами уже есть"); } } } }