You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
5.4 KiB
172 lines
5.4 KiB
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Linq;
|
|
using Microsoft.Extensions.Logging;
|
|
using CellularManagement.Domain.Entities;
|
|
using CellularManagement.Domain.Repositories;
|
|
using CellularManagement.Infrastructure.Repositories.Base;
|
|
using CellularManagement.Domain.Repositories.Base;
|
|
using CellularManagement.Domain.Repositories.Identity;
|
|
|
|
namespace CellularManagement.Infrastructure.Repositories.Identity;
|
|
|
|
/// <summary>
|
|
/// 权限仓储实现类
|
|
/// </summary>
|
|
public class PermissionRepository : BaseRepository<Permission>, IPermissionRepository
|
|
{
|
|
private readonly ILogger<PermissionRepository> _logger;
|
|
|
|
/// <summary>
|
|
/// 初始化仓储
|
|
/// </summary>
|
|
public PermissionRepository(
|
|
ICommandRepository<Permission> commandRepository,
|
|
IQueryRepository<Permission> queryRepository,
|
|
ILogger<PermissionRepository> logger)
|
|
: base(commandRepository, queryRepository, logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
#region IPermissionCommandRepository 实现
|
|
|
|
/// <summary>
|
|
/// 添加权限
|
|
/// </summary>
|
|
public async Task<Permission> AddPermissionAsync(Permission permission, CancellationToken cancellationToken = default)
|
|
{
|
|
return await CommandRepository.AddAsync(permission, cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新权限
|
|
/// </summary>
|
|
public async Task UpdatePermissionAsync(Permission permission, CancellationToken cancellationToken = default)
|
|
{
|
|
CommandRepository.Update(permission);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除权限
|
|
/// </summary>
|
|
public async Task DeletePermissionAsync(string permissionId, CancellationToken cancellationToken = default)
|
|
{
|
|
var permission = await QueryRepository.GetByIdAsync(permissionId, cancellationToken);
|
|
if (permission != null)
|
|
{
|
|
CommandRepository.Delete(permission);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量添加权限
|
|
/// </summary>
|
|
public async Task AddPermissionsAsync(IEnumerable<Permission> permissions, CancellationToken cancellationToken = default)
|
|
{
|
|
await CommandRepository.AddRangeAsync(permissions, cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量更新权限
|
|
/// </summary>
|
|
public async Task UpdatePermissionsAsync(IEnumerable<Permission> permissions, CancellationToken cancellationToken = default)
|
|
{
|
|
CommandRepository.UpdateRange(permissions);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量删除权限
|
|
/// </summary>
|
|
public async Task DeletePermissionsAsync(IEnumerable<string> permissionIds, CancellationToken cancellationToken = default)
|
|
{
|
|
var permissions = await QueryRepository.FindAsync(p => permissionIds.Contains(p.Id), cancellationToken);
|
|
if (permissions.Any())
|
|
{
|
|
CommandRepository.DeleteRange(permissions);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IPermissionQueryRepository 实现
|
|
|
|
/// <summary>
|
|
/// 获取所有权限
|
|
/// </summary>
|
|
public async Task<IEnumerable<Permission>> GetAllPermissionsAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
return await QueryRepository.GetAllAsync(cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID获取权限
|
|
/// </summary>
|
|
public async Task<Permission?> GetPermissionByIdAsync(string permissionId, CancellationToken cancellationToken = default)
|
|
{
|
|
return await QueryRepository.GetByIdAsync(permissionId, cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据名称获取权限
|
|
/// </summary>
|
|
public async Task<Permission?> GetPermissionByNameAsync(string name, CancellationToken cancellationToken = default)
|
|
{
|
|
return await QueryRepository.FirstOrDefaultAsync(p => p.Name == name, cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据类型获取权限
|
|
/// </summary>
|
|
public async Task<IEnumerable<Permission>> GetPermissionsByTypeAsync(string type, CancellationToken cancellationToken = default)
|
|
{
|
|
return await QueryRepository.FindAsync(p => p.Type == type, cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查权限是否存在
|
|
/// </summary>
|
|
public async Task<bool> ExistsAsync(string permissionId, CancellationToken cancellationToken = default)
|
|
{
|
|
return await QueryRepository.AnyAsync(p => p.Id == permissionId, cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查权限名称是否存在
|
|
/// </summary>
|
|
public async Task<bool> NameExistsAsync(string name, CancellationToken cancellationToken = default)
|
|
{
|
|
return await QueryRepository.AnyAsync(p => p.Name == name, cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据名称获取权限
|
|
/// </summary>
|
|
public async Task<Permission?> GetByNameAsync(string name, CancellationToken cancellationToken = default)
|
|
{
|
|
return await QueryRepository.FirstOrDefaultAsync(p => p.Name == name, cancellationToken);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IPermissionRepository 实现
|
|
|
|
/// <summary>
|
|
/// 更新权限
|
|
/// </summary>
|
|
public async Task UpdateAsync(Permission permission, CancellationToken cancellationToken = default)
|
|
{
|
|
CommandRepository.Update(permission);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除权限
|
|
/// </summary>
|
|
public async Task DeleteAsync(Permission permission, CancellationToken cancellationToken = default)
|
|
{
|
|
CommandRepository.Delete(permission);
|
|
}
|
|
|
|
#endregion
|
|
}
|