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 EmployeeLogic : IEmployeeLogic { private readonly IEmployeeStorage _employeeStorage; public EmployeeLogic(IEmployeeStorage employeeStorage) { _employeeStorage = employeeStorage ?? throw new ArgumentNullException(nameof(employeeStorage)); } public bool Create(EmployeeBindingModel model) { CheckModel(model); if (_employeeStorage.Insert(model) == null) { return false; } return true; } public bool Delete(EmployeeBindingModel model) { CheckModel(model); if (_employeeStorage.Delete(model) == null) { return false; } return true; } public EmployeeViewModel? ReadElement(EmployeeSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _employeeStorage.GetElement(model); if (element == null) { return null; } return element; } public List? ReadList(EmployeeSearchModel? model) { var list = model == null ? _employeeStorage.GetFullList() : _employeeStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public bool Update(EmployeeBindingModel model) { CheckModel(model); if (_employeeStorage.Update(model) == null) { return false; } return true; } private void CheckModel(EmployeeBindingModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (string.IsNullOrEmpty(model.FirstName)) { throw new ArgumentException("Отсутвует имя сотрудника", nameof(model.FirstName)); } if (string.IsNullOrEmpty(model.LastName)) { throw new ArgumentException("Отсутвует фамилия сотрудника", nameof(model.LastName)); } if (string.IsNullOrEmpty(model.Patronymic)) { throw new ArgumentException("Отсутвует отчество сотрудника", nameof(model.Patronymic)); } if (_employeeStorage.GetElement(new EmployeeSearchModel { FirstName = model.FirstName, LastName = model.LastName, Patronymic = model.Patronymic }) != null) { throw new InvalidOperationException("Сотрудник с такими атрибутами уже есть"); } } } }