SUBD_PIbd-21_Balberova_D.N./BeautySaloon/BeautySaloonBusinessLogic/EmployeeLogic.cs
2023-03-28 17:32:55 +04:00

108 lines
3.4 KiB
C#
Raw Permalink 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 BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.BusinessLogicsContracts;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.StoragesContracts;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonBusinessLogic
{
public class EmployeeLogic : IEmployeeLogic
{
private readonly IEmployeeStorage _employeeStorage;
public EmployeeLogic(IEmployeeStorage employeeStorage)
{
_employeeStorage = employeeStorage;
}
public bool Create(EmployeeBindingModel model)
{
CheckModel(model);
if (_employeeStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(EmployeeBindingModel model)
{
CheckModel(model, false);
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, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет имени сотрудника", nameof(model.Name));
}
if (string.IsNullOrEmpty(model.Surname))
{
throw new ArgumentNullException("Нет фамилии сотрудника", nameof(model.Surname));
}
if (string.IsNullOrEmpty(model.Phone))
{
throw new ArgumentNullException("Нет телефона сотрудника", nameof(model.Phone));
}
if (model.PositionId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор у должности", nameof(model.PositionId));
}
var element = _employeeStorage.GetElement(new EmployeeSearchModel
{
Phone = model.Phone
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Сотрудник с таким номером телефона уже есть");
}
}
}
}