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

96 lines
2.8 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 PositionLogic : IPositionLogic
{
private readonly IPositionStorage _positionStorage;
public PositionLogic(IPositionStorage positionStorage)
{
_positionStorage = positionStorage;
}
public bool Create(PositionBindingModel model)
{
CheckModel(model);
if (_positionStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(PositionBindingModel model)
{
CheckModel(model, false);
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, 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));
}
var element = _positionStorage.GetElement(new PositionSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Должность с таким названием уже есть");
}
}
}
}