PIbd-22_Bondarenko_M.S_SUBD/PersonnelDepartmentView/PersonnelDepartmentBusinessLogic/BusinessLogics/EmployeeLogic.cs
2023-05-01 22:55:10 +04:00

110 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<EmployeeViewModel>? 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("Сотрудник с такими атрибутами уже есть");
}
}
}
}