From c6c53b144dbf2c07e321b9665c5c48408f98717c Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Sat, 29 Apr 2023 18:39:18 +0400 Subject: [PATCH] BusinessLogic --- Forum/Forum.sln | 6 + Forum/ForumBusinessLogic/CategoryLogic.cs | 103 ++++++++++++++++ .../ForumBusinessLogic.csproj | 13 ++ Forum/ForumBusinessLogic/MessageLogic.cs | 89 ++++++++++++++ Forum/ForumBusinessLogic/RoleLogic.cs | 103 ++++++++++++++++ Forum/ForumBusinessLogic/TopicLogic.cs | 103 ++++++++++++++++ Forum/ForumBusinessLogic/UserLogic.cs | 111 ++++++++++++++++++ 7 files changed, 528 insertions(+) create mode 100644 Forum/ForumBusinessLogic/CategoryLogic.cs create mode 100644 Forum/ForumBusinessLogic/ForumBusinessLogic.csproj create mode 100644 Forum/ForumBusinessLogic/MessageLogic.cs create mode 100644 Forum/ForumBusinessLogic/RoleLogic.cs create mode 100644 Forum/ForumBusinessLogic/TopicLogic.cs create mode 100644 Forum/ForumBusinessLogic/UserLogic.cs diff --git a/Forum/Forum.sln b/Forum/Forum.sln index 2395210..eccb031 100644 --- a/Forum/Forum.sln +++ b/Forum/Forum.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ForumDataModels", "ForumDat EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ForumContracts", "ForumContracts\ForumContracts.csproj", "{5C97629C-A864-4DF7-9371-04F7E6CE0E71}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ForumBusinessLogic", "ForumBusinessLogic\ForumBusinessLogic.csproj", "{ED013C29-0AF9-427C-8967-7B003E297B8B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {5C97629C-A864-4DF7-9371-04F7E6CE0E71}.Debug|Any CPU.Build.0 = Debug|Any CPU {5C97629C-A864-4DF7-9371-04F7E6CE0E71}.Release|Any CPU.ActiveCfg = Release|Any CPU {5C97629C-A864-4DF7-9371-04F7E6CE0E71}.Release|Any CPU.Build.0 = Release|Any CPU + {ED013C29-0AF9-427C-8967-7B003E297B8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ED013C29-0AF9-427C-8967-7B003E297B8B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ED013C29-0AF9-427C-8967-7B003E297B8B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ED013C29-0AF9-427C-8967-7B003E297B8B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Forum/ForumBusinessLogic/CategoryLogic.cs b/Forum/ForumBusinessLogic/CategoryLogic.cs new file mode 100644 index 0000000..863933c --- /dev/null +++ b/Forum/ForumBusinessLogic/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/Forum/ForumBusinessLogic/ForumBusinessLogic.csproj b/Forum/ForumBusinessLogic/ForumBusinessLogic.csproj new file mode 100644 index 0000000..39af0e9 --- /dev/null +++ b/Forum/ForumBusinessLogic/ForumBusinessLogic.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/Forum/ForumBusinessLogic/MessageLogic.cs b/Forum/ForumBusinessLogic/MessageLogic.cs new file mode 100644 index 0000000..cb9684b --- /dev/null +++ b/Forum/ForumBusinessLogic/MessageLogic.cs @@ -0,0 +1,89 @@ +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 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/Forum/ForumBusinessLogic/RoleLogic.cs b/Forum/ForumBusinessLogic/RoleLogic.cs new file mode 100644 index 0000000..831684f --- /dev/null +++ b/Forum/ForumBusinessLogic/RoleLogic.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 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 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/Forum/ForumBusinessLogic/TopicLogic.cs b/Forum/ForumBusinessLogic/TopicLogic.cs new file mode 100644 index 0000000..1391f4c --- /dev/null +++ b/Forum/ForumBusinessLogic/TopicLogic.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 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 + } + ); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Тема с таким названием уже есть"); + } + } + } +} diff --git a/Forum/ForumBusinessLogic/UserLogic.cs b/Forum/ForumBusinessLogic/UserLogic.cs new file mode 100644 index 0000000..2d1951c --- /dev/null +++ b/Forum/ForumBusinessLogic/UserLogic.cs @@ -0,0 +1,111 @@ +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; + } + + 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("Пользователь с такими данными уже есть"); + } + } + } +}