96 lines
2.8 KiB
C#
96 lines
2.8 KiB
C#
|
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("Должность с таким названием уже есть");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|