104 lines
2.9 KiB
C#
Raw Normal View History

2024-05-14 23:07:08 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.BusinessLogicsContracts;
using TaskTrackerContracts.SearchModels;
using TaskTrackerContracts.StoragesContracts;
using TaskTrackerContracts.ViewModels;
namespace TaskTrackerBusinessLogics.BusinessLogic
{
public class DirectionLogic : IDirectionLogic
{
private readonly IDirectionStorage _directionStorage;
public DirectionLogic(IDirectionStorage directionStorage)
{
_directionStorage = directionStorage;
}
public List<DirectionViewModel>? ReadList(DirectionSearchModel? model)
{
var list = model == null ? _directionStorage.GetFullList() : _directionStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public DirectionViewModel? ReadElement(DirectionSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _directionStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(DirectionBindingModel model)
{
CheckModel(model);
if (_directionStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(DirectionBindingModel model)
{
CheckModel(model);
if (_directionStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(DirectionBindingModel model)
{
CheckModel(model, false);
if (_directionStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(DirectionBindingModel 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 = _directionStorage.GetElement(new DirectionSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Организация с таким названием уже есть");
}
}
}
}