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

118 lines
2.9 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 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<DealViewModel>? 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("Договор с такими атрибутами уже есть");
}
}
}
}