99 lines
2.6 KiB
C#

using Subd_4.BindingModels;
using Subd_4.BusinessLogicContracts;
using Subd_4.SearchModels;
using Subd_4.StoragesContracts;
using Subd_4.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructionFirmBusinessLogic.BusinessLogics
{
public class SpecialtyLogic : ISpecialtyLogic
{
private readonly ISpecialtyStorage _SpecialtyStorage;
public SpecialtyLogic(ISpecialtyStorage specialtyStorage)
{
_SpecialtyStorage = specialtyStorage;
}
public SpecialtyViewModel? ReadElement(SpecialtySearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _SpecialtyStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<SpecialtyViewModel>? ReadList(SpecialtySearchModel? model)
{
var list = model == null ? _SpecialtyStorage.GetFullList() : _SpecialtyStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Create(SpecialtyBindingModel model)
{
CheckModel(model);
if (_SpecialtyStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(SpecialtyBindingModel model)
{
CheckModel(model, false);
if (_SpecialtyStorage.Delete(model) == null)
{
return false;
}
return true;
}
public bool Update(SpecialtyBindingModel model)
{
CheckModel(model);
if (_SpecialtyStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(SpecialtyBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.SpecialtyName))
{
throw new ArgumentNullException("Нет названия", nameof(model.SpecialtyName));
}
}
public void ClearEntity()
{
_SpecialtyStorage.ClearEntity();
}
}
}