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

63 lines
2.0 KiB
C#
Raw Permalink Normal View History

2023-10-28 09:06:35 +04:00
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;
2023-11-09 04:07:06 +04:00
public DirectionLogic(IDirectionStorage directionStorage)
2023-10-28 09:06:35 +04:00
{
_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("Такое направление уже существует");
}
2023-11-09 04:07:06 +04:00
if (model.Id.HasValue)
2023-10-28 09:06:35 +04:00
{
_directionStorage.Update(model);
}
else
{
_directionStorage.Insert(model);
}
}
2023-11-09 04:07:06 +04:00
public void Delete(DirectionBindingModel model)
2023-10-28 09:06:35 +04:00
{
2023-11-09 04:07:06 +04:00
var element = _directionStorage.GetElement(new DirectionBindingModel { Id = model.Id });
2023-10-28 09:06:35 +04:00
if (element == null)
{
2023-11-09 04:07:06 +04:00
throw new Exception("Направление не найдено");
2023-10-28 09:06:35 +04:00
}
2023-11-09 04:07:06 +04:00
_directionStorage.Delete(model);
2023-10-28 09:06:35 +04:00
}
2023-11-09 04:07:06 +04:00
public List<DirectionViewModel> Read(DirectionBindingModel model)
2023-10-28 09:06:35 +04:00
{
if (model == null)
{
2023-11-09 04:07:06 +04:00
return _directionStorage.GetFullList();
2023-10-28 09:06:35 +04:00
}
2023-11-09 04:07:06 +04:00
if (!string.IsNullOrEmpty(model.Name))
2023-10-28 09:06:35 +04:00
{
2023-11-09 04:07:06 +04:00
return new List<DirectionViewModel> { _directionStorage.GetElement(model) };
2023-10-28 09:06:35 +04:00
}
2023-11-09 04:07:06 +04:00
return _directionStorage.GetFilteredList(model);
2023-10-28 09:06:35 +04:00
}
}
}