154 lines
6.2 KiB
C#
154 lines
6.2 KiB
C#
// ***********************************************************************
|
|
// <copyright file="PermissionService.cs">
|
|
// AgileWebs
|
|
// </copyright>
|
|
// ***********************************************************************
|
|
using Core.Thalos.Adapters;
|
|
using Core.Blueprint.Mongo;
|
|
using Core.Blueprint.Redis;
|
|
using Core.Blueprint.Redis.Helpers;
|
|
using Mapster;
|
|
using Microsoft.Extensions.Options;
|
|
using MongoDB.Driver;
|
|
using Core.Thalos.Provider.Contracts;
|
|
using Core.Thalos.Domain.Contexts.Onboarding.Request;
|
|
|
|
namespace Core.Thalos.Provider.Providers.Onboarding
|
|
{
|
|
/// <summary>
|
|
/// Handles all services and business rules related to <see cref="PermissionAdapter"/>.
|
|
/// </summary>
|
|
public class PermissionProvider : IPermissionProvider
|
|
{
|
|
private readonly CollectionRepository<PermissionAdapter> repository;
|
|
private readonly CacheSettings cacheSettings;
|
|
private readonly IRedisCacheProvider cacheProvider;
|
|
|
|
public PermissionProvider(CollectionRepository<PermissionAdapter> repository,
|
|
IRedisCacheProvider cacheProvider, IOptions<CacheSettings> cacheSettings)
|
|
{
|
|
this.repository = repository;
|
|
this.repository.CollectionInitialization();
|
|
this.cacheSettings = cacheSettings.Value;
|
|
this.cacheProvider = cacheProvider;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new Permission.
|
|
/// </summary>
|
|
/// <param name="entity">The Permission to be created.</param>
|
|
/// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing
|
|
/// the asynchronous execution of the service.</returns>
|
|
public async ValueTask<PermissionAdapter> CreatePermission(PermissionRequest newPermission, CancellationToken cancellationToken)
|
|
{
|
|
var permissionCollection = newPermission.Adapt<PermissionAdapter>();
|
|
|
|
await repository.InsertOneAsync(permissionCollection);
|
|
|
|
return permissionCollection;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets an Permission by identifier.
|
|
/// </summary>
|
|
/// <param name="id">The Permission identifier.</param>
|
|
/// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing
|
|
/// the asynchronous execution of the service.</returns>0
|
|
public async ValueTask<PermissionAdapter> GetPermissionById(string _id, CancellationToken cancellationToken)
|
|
{
|
|
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetPermissionById", _id);
|
|
var cachedData = await cacheProvider.GetAsync<PermissionAdapter>(cacheKey);
|
|
|
|
if (cachedData is not null) { return cachedData; }
|
|
|
|
var permission = await repository.FindByIdAsync(_id);
|
|
|
|
await cacheProvider.SetAsync(cacheKey, permission);
|
|
|
|
return permission;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets all the permissions.
|
|
/// </summary>
|
|
/// <returns>A <see cref="{Task{IEnumerbale{PermissionAdapter}}}"/> representing
|
|
/// the asynchronous execution of the service.</returns>
|
|
public async ValueTask<IEnumerable<PermissionAdapter>> GetAllPermissions(CancellationToken cancellationToken)
|
|
{
|
|
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllPermissions");
|
|
var cachedData = await cacheProvider.GetAsync<IEnumerable<PermissionAdapter>>(cacheKey) ?? [];
|
|
|
|
if (cachedData.Any()) return cachedData;
|
|
|
|
var permissions = await repository.AsQueryable();
|
|
|
|
await cacheProvider.SetAsync(cacheKey, permissions);
|
|
|
|
return permissions;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets all the permissions by permissions identifier list.
|
|
/// </summary>
|
|
/// <param name="permissions">The list of permissions identifiers.</param>
|
|
/// <returns>A <see cref="Task{IEnumerable{PermissionAdapter}}"/> representing
|
|
/// the asynchronous execution of the service.</returns>
|
|
public async ValueTask<IEnumerable<PermissionAdapter>> GetAllPermissionsByList(string[] permissions, CancellationToken cancellationToken)
|
|
{
|
|
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllPermissionsByList", permissions);
|
|
|
|
var cachedData = await cacheProvider.GetAsync<IEnumerable<PermissionAdapter>>(cacheKey) ?? [];
|
|
|
|
if (cachedData.Any()) return cachedData;
|
|
|
|
var builder = Builders<PermissionAdapter>.Filter;
|
|
var filters = new List<FilterDefinition<PermissionAdapter>>();
|
|
|
|
if (permissions == null || !permissions.Any())
|
|
{
|
|
filters.Add(builder.In(x => x.Id, permissions));
|
|
}
|
|
|
|
var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty;
|
|
|
|
var permissionsList = await repository.FilterByMongoFilterAsync(finalFilter);
|
|
|
|
await cacheProvider.SetAsync(cacheKey, permissionsList);
|
|
|
|
return permissionsList;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Changes the status of the permission.
|
|
/// </summary>
|
|
/// <param name="id">The permission identifier.</param>
|
|
/// <param name="newStatus">The new status of the permission.</param>
|
|
/// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing
|
|
/// the asynchronous execution of the service.</returns>
|
|
public async ValueTask<PermissionAdapter> ChangePermissionStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken)
|
|
{
|
|
var entity = await repository.FindByIdAsync(id);
|
|
entity.Status = newStatus;
|
|
|
|
await repository.ReplaceOneAsync(entity);
|
|
|
|
return entity;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates a Permission by id.
|
|
/// </summary>
|
|
/// <param name="entity">The Permission to be updated.</param>
|
|
/// <param name="id">The Permission identifier.</param>
|
|
/// <returns>A <see cref="{Task{PermissionAdapter}}"/> representing
|
|
/// the asynchronous execution of the service.</returns>
|
|
public async ValueTask<PermissionAdapter> UpdatePermission(PermissionAdapter entity, CancellationToken cancellationToken)
|
|
{
|
|
await repository.ReplaceOneAsync(entity);
|
|
|
|
return entity;
|
|
}
|
|
}
|
|
}
|