122 lines
4.0 KiB
C#
122 lines
4.0 KiB
C#
using ForumContracts.BindingModels;
|
||
using ForumContracts.BusinessLogicContracts;
|
||
using ForumContracts.SearchModels;
|
||
using ForumContracts.StorageContracts;
|
||
using ForumContracts.ViewModels;
|
||
using Microsoft.Extensions.Logging;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace ForumBusinessLogic.BusinessLogic
|
||
{
|
||
public class CategoryLogic : ICategoryLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly ICategoryStorage _categoryStorage;
|
||
|
||
public CategoryLogic(ILogger<CategoryLogic> logger, ICategoryStorage categoryStorage)
|
||
{
|
||
_logger = logger;
|
||
_categoryStorage = categoryStorage;
|
||
}
|
||
|
||
public bool Create(CategoryBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_categoryStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(CategoryBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||
if (_categoryStorage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public CategoryViewModel? ReadElement(CategorySearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation("ReadElement. Name:{FIO}.Id:{ Id}",
|
||
model.Name, model.Id);
|
||
var element = _categoryStorage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
_logger.LogWarning("ReadElement element not found");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
||
return element;
|
||
}
|
||
|
||
public List<CategoryViewModel>? ReadList(CategorySearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. Name:{Name}.Id:{ Id} ", model?.Name, model?.Id);
|
||
var list = (model == null) ? _categoryStorage.GetFullList() :
|
||
_categoryStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||
return list;
|
||
}
|
||
|
||
public bool Update(CategoryBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_categoryStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
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));
|
||
}
|
||
if (string.IsNullOrEmpty(model.Description))
|
||
{
|
||
throw new ArgumentNullException("Нет описания категории", nameof(model.Description));
|
||
}
|
||
_logger.LogInformation("Caregory. Id: {Id}, Name: {Name}", model.Id, model.Name);
|
||
var element = _categoryStorage.GetElement(new CategorySearchModel
|
||
{
|
||
Name = model.Name,
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Категория с таким названием уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|