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

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