SUBD/Forum/ForumBusinessLogic/TopicLogic.cs
2023-04-29 18:39:18 +04:00

104 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}
);
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Тема с таким названием уже есть");
}
}
}
}