Бизнес-логика

This commit is contained in:
Ismailov_Rovshan 2023-09-06 20:52:08 +04:00
parent 289ed32187
commit e46735d32d
5 changed files with 528 additions and 0 deletions

View File

@ -0,0 +1,103 @@
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 CategoryLogic : ICategoryLogic
{
private readonly ICategoryStorage _categoryStorage;
public CategoryLogic(ICategoryStorage categoryStorage)
{
_categoryStorage = categoryStorage;
}
public bool Create(CategoryBindingModel model)
{
CheckModel(model);
if (_categoryStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(CategoryBindingModel model)
{
CheckModel(model, false);
if (_categoryStorage.Delete(model) == null)
{
return false;
}
return true;
}
public CategoryViewModel? ReadElement(CategorySearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _categoryStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<CategoryViewModel>? ReadList(CategorySearchModel? model)
{
var list = model == null ? _categoryStorage.GetFullList() : _categoryStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Update(CategoryBindingModel model)
{
CheckModel(model);
if (_categoryStorage.Update(model) == null)
{
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));
}
var element = _categoryStorage.GetElement(new CategorySearchModel
{
Name = model.Name
}
);
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Категория с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,102 @@
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 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;
}
}
}
}

View File

@ -0,0 +1,108 @@
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 RoleLogic : IRoleLogic
{
private readonly IRoleStorage _roleStorage;
public RoleLogic(IRoleStorage roleStorage)
{
_roleStorage = roleStorage;
}
public bool Create(RoleBindingModel model)
{
CheckModel(model);
if (_roleStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(RoleBindingModel model)
{
CheckModel(model, false);
if (_roleStorage.Delete(model) == null)
{
return false;
}
return true;
}
public RoleViewModel? ReadElement(RoleSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _roleStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<RoleViewModel>? ReadList(RoleSearchModel? model)
{
var list = model == null ? _roleStorage.GetFullList() : _roleStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public void RoleInsertList(int num)
{
_roleStorage.RoleInsertList(num);
}
public bool Update(RoleBindingModel model)
{
CheckModel(model);
if (_roleStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(RoleBindingModel 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 = _roleStorage.GetElement(new RoleSearchModel
{
Name = model.Name
}
);
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Роль с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,99 @@
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
}
);
}
}
}

View File

@ -0,0 +1,116 @@
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 UserLogic : IUserLogic
{
private readonly IUserStorage _userStorage;
public UserLogic(IUserStorage userStorage)
{
_userStorage = userStorage;
}
public bool Create(UserBindingModel model)
{
CheckModel(model);
if (_userStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(UserBindingModel model)
{
CheckModel(model, false);
if (_userStorage.Delete(model) == null)
{
return false;
}
return true;
}
public UserViewModel? ReadElement(UserSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _userStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<UserViewModel>? ReadList(UserSearchModel? model)
{
var list = model == null ? _userStorage.GetFullList() : _userStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Update(UserBindingModel model)
{
CheckModel(model);
if (_userStorage.Update(model) == null)
{
return false;
}
return true;
}
public void UserInsertList(int num, List<RoleViewModel> roles)
{
_userStorage.UserInsertList(num, roles);
}
private void CheckModel(UserBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Username))
{
throw new ArgumentNullException("Нет имени пользователя", nameof(model.Username));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Нет почты", nameof(model.Email));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля", nameof(model.Password));
}
var element = _userStorage.GetElement(new UserSearchModel
{
Username = model.Username,
Email = model.Email,
}
);
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Пользователь с такими данными уже есть");
}
}
}
}