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

104 lines
2.5 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 DepartmentLogic : IDepartmentLogic
{
private readonly IDepartmentStorage _departmentStorage;
public DepartmentLogic(IDepartmentStorage departmentStorage)
{
_departmentStorage = departmentStorage ?? throw new ArgumentNullException(nameof(departmentStorage));
}
public bool Create(DepartmentBindingModel model)
{
CheckModel(model);
if (_departmentStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(DepartmentBindingModel model)
{
CheckModel(model);
if (_departmentStorage.Delete(model) == null)
{
return false;
}
return true;
}
public DepartmentViewModel? ReadElement(DepartmentSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _departmentStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<DepartmentViewModel>? ReadList(DepartmentSearchModel? model)
{
var list = model == null ? _departmentStorage.GetFullList() : _departmentStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Update(DepartmentBindingModel model)
{
CheckModel(model);
if (_departmentStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(DepartmentBindingModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentException("Отсутвует название",
nameof(model.Name));
}
if (model.Telephone < 0)
{
throw new ArgumentException("Некоректный контактный телефон",
nameof(model.Telephone));
}
if (_departmentStorage.GetElement(new DepartmentSearchModel
{
Name = model.Name,
Telephone = model.Telephone
}) != null)
{
throw new InvalidOperationException("Отдел с такими атрибутами уже есть");
}
}
}
}