From e46735d32d106994006ca1d5764045a3f7f77d52 Mon Sep 17 00:00:00 2001 From: Ismailov_Rovshan Date: Wed, 6 Sep 2023 20:52:08 +0400 Subject: [PATCH] =?UTF-8?q?=D0=91=D0=B8=D0=B7=D0=BD=D0=B5=D1=81-=D0=BB?= =?UTF-8?q?=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BlogDataModels/BusinessLogic/CategoryLogic.cs | 103 ++++++++++++++++ BlogDataModels/BusinessLogic/MessageLogic.cs | 102 +++++++++++++++ BlogDataModels/BusinessLogic/RoleLogic.cs | 108 ++++++++++++++++ BlogDataModels/BusinessLogic/TopicLogic.cs | 99 +++++++++++++++ BlogDataModels/BusinessLogic/UserLogic.cs | 116 ++++++++++++++++++ 5 files changed, 528 insertions(+) create mode 100644 BlogDataModels/BusinessLogic/CategoryLogic.cs create mode 100644 BlogDataModels/BusinessLogic/MessageLogic.cs create mode 100644 BlogDataModels/BusinessLogic/RoleLogic.cs create mode 100644 BlogDataModels/BusinessLogic/TopicLogic.cs create mode 100644 BlogDataModels/BusinessLogic/UserLogic.cs diff --git a/BlogDataModels/BusinessLogic/CategoryLogic.cs b/BlogDataModels/BusinessLogic/CategoryLogic.cs new file mode 100644 index 0000000..863933c --- /dev/null +++ b/BlogDataModels/BusinessLogic/CategoryLogic.cs @@ -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? 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("Категория с таким названием уже есть"); + } + } + } +} diff --git a/BlogDataModels/BusinessLogic/MessageLogic.cs b/BlogDataModels/BusinessLogic/MessageLogic.cs new file mode 100644 index 0000000..b27b545 --- /dev/null +++ b/BlogDataModels/BusinessLogic/MessageLogic.cs @@ -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? 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 users, List 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; + } + } + } +} diff --git a/BlogDataModels/BusinessLogic/RoleLogic.cs b/BlogDataModels/BusinessLogic/RoleLogic.cs new file mode 100644 index 0000000..53c4869 --- /dev/null +++ b/BlogDataModels/BusinessLogic/RoleLogic.cs @@ -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? 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("Роль с таким названием уже есть"); + } + } + } +} diff --git a/BlogDataModels/BusinessLogic/TopicLogic.cs b/BlogDataModels/BusinessLogic/TopicLogic.cs new file mode 100644 index 0000000..7e15a7a --- /dev/null +++ b/BlogDataModels/BusinessLogic/TopicLogic.cs @@ -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? 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 + } + ); + } + } +} diff --git a/BlogDataModels/BusinessLogic/UserLogic.cs b/BlogDataModels/BusinessLogic/UserLogic.cs new file mode 100644 index 0000000..ffb0454 --- /dev/null +++ b/BlogDataModels/BusinessLogic/UserLogic.cs @@ -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? 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 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("Пользователь с такими данными уже есть"); + } + } + } +}