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 PositionLogic : IPositionLogic { private readonly IPositionStorage _positionStorage; public PositionLogic(IPositionStorage positionStorage) { _positionStorage = positionStorage ?? throw new ArgumentNullException(nameof(positionStorage)); } public bool Create(PositionBindingModel model) { CheckModel(model); if (_positionStorage.Insert(model) == null) { return false; } return true; } public bool Delete(PositionBindingModel model) { CheckModel(model); if (_positionStorage.Delete(model) == null) { return false; } return true; } public PositionViewModel? ReadElement(PositionSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _positionStorage.GetElement(model); if (element == null) { return null; } return element; } public List? ReadList(PositionSearchModel? model) { var list = model == null ? _positionStorage.GetFullList() : _positionStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public bool Update(PositionBindingModel model) { CheckModel(model); if (_positionStorage.Update(model) == null) { return false; } return true; } private void CheckModel(PositionBindingModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (string.IsNullOrEmpty(model.Name)) { throw new ArgumentException("Отсутвует имя сотрудника", nameof(model.Name)); } if (_positionStorage.GetElement(new PositionSearchModel { Name = model.Name }) != null) { throw new InvalidOperationException("Сотрудник с такими атрибутами уже есть"); } } } }