PIbd-22-Ismailov_SUBD/BlogDataModels/BusinessLogic/TopicLogic.cs

100 lines
2.6 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 TopicLogic : ITopicLogic
{
private readonly ITopicStorage _topicStorage;
public TopicLogic(ITopicStorage topicStorage)
{
_topicStorage = topicStorage;
}
public bool Create(TopicBindingModel model)
{
CheckModel(model);
if (_topicStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(TopicBindingModel model)
{
CheckModel(model, false);
if (_topicStorage.Delete(model) == null)
{
return false;
}
return true;
}
public TopicViewModel? ReadElement(TopicSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _topicStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<TopicViewModel>? ReadList(TopicSearchModel? model)
{
var list = model == null ? _topicStorage.GetFullList() : _topicStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Update(TopicBindingModel model)
{
CheckModel(model);
if (_topicStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(TopicBindingModel 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 = _topicStorage.GetElement(new TopicSearchModel
{
Name = model.Name
}
);
}
}
}