PIbd-22-Ismailov_SUBD/BlogDataModels/BusinessLogic/MessageLogic.cs
2023-09-06 22:02:39 +04:00

103 lines
2.7 KiB
C#

using BlogContracts.BindingModels;
using BlogContracts.BusinessLogicContracts;
using BlogContracts.SearchModels;
using BlogContracts.StoragesContracts;
using BlogContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlogBusinessLogic
{
public class MessageLogic : IMessageLogic
{
private readonly IMessageStorage _messageStorage;
public MessageLogic(IMessageStorage messageStorage)
{
_messageStorage = messageStorage;
}
public bool Create(MessageBindingModel model)
{
CheckModel(model);
if (_messageStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(MessageBindingModel model)
{
CheckModel(model, false);
if (_messageStorage.Delete(model) == null)
{
return false;
}
return true;
}
public MessageViewModel? ReadElement(MessageSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _messageStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<MessageViewModel>? ReadList(MessageSearchModel? model)
{
var list = model == null ? _messageStorage.GetFullList() : _messageStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public string TestInsertList(int num, List<UserViewModel> users, List<TopicViewModel> topics)
{
return _messageStorage.TestInsertList(num, users, topics);
}
public string TestReadList(int num)
{
return _messageStorage.TestReadList(num);
}
public string TestJoinReadList(int num)
{
return _messageStorage.TestJoinReadList(num);
}
public bool Update(MessageBindingModel model)
{
CheckModel(model);
if (_messageStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(MessageBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
}
}
}