PIbd-22_Bondarenko_M.S_SUBD/PersonnelDepartmentView/PersonnelDepartmentBusinessLogic/BusinessLogics/DealLogic.cs

138 lines
3.2 KiB
C#
Raw Normal View History

2023-05-01 22:55:10 +04:00
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));
}
2023-05-05 22:16:51 +04:00
public bool ClearList()
{
return _dealStorage.ClearList();
}
2023-05-01 22:55:10 +04:00
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;
}
2023-05-05 22:16:51 +04:00
public string DiffGetTest(int count)
{
return _dealStorage.DiffGetTest(count);
}
public string GetTest(int count)
{
return _dealStorage.GetTest(count);
}
2023-05-01 22:55:10 +04:00
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;
}
2023-05-05 22:16:51 +04:00
public string SetTest(int count)
{
return _dealStorage.SetTest(count);
}
2023-05-01 22:55:10 +04:00
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("Договор с такими атрибутами уже есть");
}
}
}
}