PIbd-31_Putincev.D.M._COP_29/COP/PortalAccountsBusinessLogic/BusinessLogics/InterestLogic.cs

74 lines
2.3 KiB
C#
Raw Permalink Normal View History

2024-11-19 23:18:49 +04:00
using PortalAccountsContracts.BindingModels;
using PortalAccountsContracts.BusinessLogicsContracts;
using PortalAccountsContracts.SearchModels;
using PortalAccountsContracts.StoragesContracts;
using PortalAccountsContracts.ViewModels;
namespace PortalAccountsBusinessLogic.BusinessLogics
{
public class InterestLogic : IInterestLogic
{
private readonly IInterestStorage _interestStorage;
public InterestLogic(IInterestStorage InterestStorage)
{
_interestStorage = InterestStorage;
}
public List<InterestViewModel>? ReadList(InterestSearchModel? model)
{
return model == null ? _interestStorage.GetFullList() : _interestStorage.GetFilteredList(model);
}
public InterestViewModel? ReadElement(InterestSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
return _interestStorage.GetElement(model);
}
public bool Create(InterestBindingModel model)
{
CheckModel(model);
return _interestStorage.Insert(model) != null;
}
public bool Update(InterestBindingModel model)
{
CheckModel(model);
return _interestStorage.Update(model) != null;
}
public bool Delete(InterestBindingModel model)
{
CheckModel(model, false);
return _interestStorage.Delete(model) != null;
}
private void CheckModel(InterestBindingModel 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 = _interestStorage.GetElement(new InterestSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Интерес с таким названием уже есть");
}
}
}
}