Logic contracts and implements

This commit is contained in:
mfnefd 2024-06-04 23:44:36 +04:00
parent be9c47aefa
commit 72e40c79e6
6 changed files with 251 additions and 5 deletions

View File

@ -7,7 +7,11 @@
</PropertyGroup>
<ItemGroup>
<Folder Include="BusinessLogic\" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Contracts\Contracts.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,99 @@
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.Converters;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogic.BusinessLogic
{
public class RoleLogic : IRoleLogic
{
private readonly ILogger _logger;
private readonly IRoleStorage _roleStorage;
public RoleLogic(ILogger<RoleLogic> logger, IRoleStorage roleStorage)
{
_logger = logger;
_roleStorage = roleStorage;
}
public RoleViewModel? Create(RoleBindingModel model)
{
ArgumentNullException.ThrowIfNull(model);
var role = _roleStorage.Insert(model);
if (role is null)
{
_logger.LogWarning("Insert operation failed.");
return null;
}
return RoleConverter.ToView(role);
}
public RoleViewModel? Delete(RoleSearchModel model)
{
ArgumentNullException.ThrowIfNull(model);
_logger.LogInformation("Delete role. Id: {0}", model.Id);
var role = _roleStorage.Delete(model);
if (role is null)
{
_logger.LogWarning("Delete operation failed.");
return null;
}
return RoleConverter.ToView(role);
}
public RoleViewModel? ReadElement(RoleSearchModel model)
{
ArgumentNullException.ThrowIfNull(model);
_logger.LogInformation("ReadElement. Id: {0}", model.Id);
var role = _roleStorage.GetElement(model);
if (role is null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id: {0}", role.Id);
return RoleConverter.ToView(role);
}
public IEnumerable<RoleViewModel> ReadElements(RoleSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
var list = _roleStorage.GetList(model);
if (list is null || list.Count() == 0)
{
_logger.LogWarning("ReadList return null list");
return [];
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count());
return list.Select(RoleConverter.ToView);
}
public RoleViewModel? Update(RoleBindingModel model)
{
ArgumentNullException.ThrowIfNull(model);
var role = _roleStorage.Update(model);
if (role is null)
{
_logger.LogWarning("Update operation failed.");
return null;
}
return RoleConverter.ToView(role);
}
}
}

View File

@ -0,0 +1,99 @@
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.Converters;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogic.BusinessLogic
{
public class UserLogic : IUserLogic
{
private readonly ILogger _logger;
private readonly IUserStorage _userStorage;
public UserLogic(ILogger<UserLogic> logger, IUserStorage userStorage)
{
_logger = logger;
_userStorage = userStorage;
}
public UserViewModel? Create(UserBindingModel model)
{
ArgumentNullException.ThrowIfNull(model);
var user = _userStorage.Insert(model);
if (user is null)
{
_logger.LogWarning("Insert operation failed.");
return null;
}
return UserConverter.ToView(user);
}
public UserViewModel? Delete(UserSearchModel model)
{
ArgumentNullException.ThrowIfNull(model);
_logger.LogInformation("Delete user. Id: {0}", model.Id);
var user = _userStorage.Delete(model);
if (user is null)
{
_logger.LogWarning("Delete operation failed.");
return null;
}
return UserConverter.ToView(user);
}
public IEnumerable<UserViewModel> ReadElements(UserSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
var list = _userStorage.GetList(model);
if (list is null || list.Count() == 0)
{
_logger.LogWarning("ReadList return null list");
return [];
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count());
return list.Select(UserConverter.ToView);
}
public UserViewModel? ReadElement(UserSearchModel model)
{
ArgumentNullException.ThrowIfNull(model);
_logger.LogInformation("ReadElement. Id: {0}", model.Id);
var user = _userStorage.GetElement(model);
if (user is null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id: {0}", user.Id);
return UserConverter.ToView(user);
}
public UserViewModel? Update(UserBindingModel model)
{
ArgumentNullException.ThrowIfNull(model);
var user = _userStorage.Update(model);
if (user is null)
{
_logger.LogWarning("Update operation failed.");
return null;
}
return UserConverter.ToView(user);
}
}
}

View File

@ -0,0 +1,24 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BusinessLogicContracts
{
public interface IRoleLogic
{
RoleViewModel? Create(RoleBindingModel model);
RoleViewModel? Update(RoleBindingModel model);
RoleViewModel? ReadElement(RoleSearchModel model);
IEnumerable<RoleViewModel> ReadElements(RoleSearchModel? model);
RoleViewModel? Delete(RoleSearchModel model);
}
}

View File

@ -0,0 +1,24 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.BusinessLogicContracts
{
public interface IUserLogic
{
UserViewModel? Create(UserBindingModel model);
UserViewModel? Update(UserBindingModel model);
UserViewModel? ReadElement(UserSearchModel model);
IEnumerable<UserViewModel> ReadElements(UserSearchModel? model);
UserViewModel? Delete(UserSearchModel model);
}
}

View File

@ -6,8 +6,4 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Folder Include="BusinessLogicContracts\" />
</ItemGroup>
</Project>