BusinessLogic

This commit is contained in:
ArtemEmelyanov 2023-04-29 18:39:18 +04:00
parent 448b5de277
commit c6c53b144d
7 changed files with 528 additions and 0 deletions

View File

@ -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

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,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ForumContracts\ForumContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -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<MessageViewModel>? 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;
}
}
}
}

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 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 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,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<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("Тема с таким названием уже есть");
}
}
}
}

View File

@ -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<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;
}
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("Пользователь с такими данными уже есть");
}
}
}
}