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

100 lines
2.6 KiB
C#
Raw Normal View History

2023-09-06 22:02:39 +04:00
using BlogContracts.BindingModels;
using BlogContracts.BusinessLogicContracts;
using BlogContracts.SearchModels;
using BlogContracts.StoragesContracts;
using BlogContracts.ViewModels;
2023-09-06 20:52:08 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2023-09-06 22:02:39 +04:00
namespace BlogBusinessLogic
2023-09-06 20:52:08 +04:00
{
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
}
);
}
}
}