104 lines
2.9 KiB
C#
104 lines
2.9 KiB
C#
|
using ForumContracts.BindingModels;
|
|||
|
using ForumContracts.BusinessLogicContracts;
|
|||
|
using ForumContracts.SearchModels;
|
|||
|
using ForumContracts.StoragesContracts;
|
|||
|
using ForumContracts.ViewModels;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace ForumBusinessLogic
|
|||
|
{
|
|||
|
public class CategoryLogic : ICategoryLogic
|
|||
|
{
|
|||
|
private readonly ICategoryStorage _categoryStorage;
|
|||
|
|
|||
|
public CategoryLogic(ICategoryStorage categoryStorage)
|
|||
|
{
|
|||
|
_categoryStorage = categoryStorage;
|
|||
|
}
|
|||
|
|
|||
|
public bool Create(CategoryBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (_categoryStorage.Insert(model) == null)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public bool Delete(CategoryBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model, false);
|
|||
|
if (_categoryStorage.Delete(model) == null)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public CategoryViewModel? ReadElement(CategorySearchModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
var element = _categoryStorage.GetElement(model);
|
|||
|
if (element == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
return element;
|
|||
|
}
|
|||
|
|
|||
|
public List<CategoryViewModel>? ReadList(CategorySearchModel? model)
|
|||
|
{
|
|||
|
var list = model == null ? _categoryStorage.GetFullList() : _categoryStorage.GetFilteredList(model);
|
|||
|
if (list == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
return list;
|
|||
|
}
|
|||
|
|
|||
|
public bool Update(CategoryBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (_categoryStorage.Update(model) == null)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private void CheckModel(CategoryBindingModel 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 = _categoryStorage.GetElement(new CategorySearchModel
|
|||
|
{
|
|||
|
Name = model.Name
|
|||
|
}
|
|||
|
);
|
|||
|
if (element != null && element.Id != model.Id)
|
|||
|
{
|
|||
|
throw new InvalidOperationException("Категория с таким названием уже есть");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|