PIbd-32_Artamonova_T.V._COP_2/UniversityBusinessLogic/BusinessLogics/DirectionLogic.cs

63 lines
2.0 KiB
C#

using Microsoft.Extensions.Logging;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicsContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
namespace UniversityBusinessLogic.BusinessLogics
{
public class DirectionLogic : IDirectionLogic
{
private readonly IDirectionStorage _directionStorage;
public DirectionLogic(IDirectionStorage directionStorage)
{
_directionStorage = directionStorage;
}
public void CreateOrUpdate(DirectionBindingModel model)
{
var element = _directionStorage.GetElement(
new DirectionBindingModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new Exception("Такое направление уже существует");
}
if (model.Id.HasValue)
{
_directionStorage.Update(model);
}
else
{
_directionStorage.Insert(model);
}
}
public void Delete(DirectionBindingModel model)
{
var element = _directionStorage.GetElement(new DirectionBindingModel { Id = model.Id });
if (element == null)
{
throw new Exception("Направление не найдено");
}
_directionStorage.Delete(model);
}
public List<DirectionViewModel> Read(DirectionBindingModel model)
{
if (model == null)
{
return _directionStorage.GetFullList();
}
if (!string.IsNullOrEmpty(model.Name))
{
return new List<DirectionViewModel> { _directionStorage.GetElement(model) };
}
return _directionStorage.GetFilteredList(model);
}
}
}