176 lines
7.7 KiB
C#
176 lines
7.7 KiB
C#
// ***********************************************************************
|
|
// <copyright file="PermissionService.cs">
|
|
// AgileWebs
|
|
// </copyright>
|
|
// ***********************************************************************
|
|
|
|
using Core.Blueprint.Mongo;
|
|
using Core.Blueprint.Redis;
|
|
using Core.Blueprint.Redis.Helpers;
|
|
using Core.Thalos.BuildingBlocks;
|
|
using Core.Thalos.Domain.Contexts.Onboarding.Request;
|
|
using Core.Thalos.Provider.Contracts;
|
|
using Mapster;
|
|
using MongoDB.Driver;
|
|
|
|
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 ICacheSettings cacheSettings;
|
|
private readonly IRedisCacheProvider cacheProvider;
|
|
|
|
public PermissionProvider(
|
|
CollectionRepository<PermissionAdapter> repository,
|
|
IRedisCacheProvider cacheProvider,
|
|
ICacheSettings cacheSettings)
|
|
{
|
|
this.repository = repository;
|
|
this.repository.CollectionInitialization();
|
|
this.cacheProvider = cacheProvider;
|
|
this.cacheSettings = cacheSettings;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new Permission.
|
|
/// </summary>
|
|
/// <param name="newPermission">The Permission to be created.</param>
|
|
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
|
|
/// <returns>
|
|
/// A <see cref="ValueTask{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 a Permission by identifier.
|
|
/// </summary>
|
|
/// <param name="_id">The Permission Mongo identifier.</param>
|
|
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
|
|
/// <returns>
|
|
/// A <see cref="ValueTask{PermissionAdapter}"/> representing the asynchronous execution of the service.
|
|
/// </returns>
|
|
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 Permissions.
|
|
/// </summary>
|
|
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
|
|
/// <returns>
|
|
/// A <see cref="ValueTask{IEnumerable{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, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
|
|
|
|
return permissions;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets all Permissions by a list of identifiers.
|
|
/// </summary>
|
|
/// <param name="permissions">The list of Permission identifiers.</param>
|
|
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
|
|
/// <returns>
|
|
/// A <see cref="ValueTask{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 is { Length: > 0 })
|
|
{
|
|
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, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
|
|
return permissionsList;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Changes the status of a Permission.
|
|
/// </summary>
|
|
/// <param name="_id">The Permission Mongo identifier.</param>
|
|
/// <param name="newStatus">The new status of the Permission.</param>
|
|
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
|
|
/// <returns>
|
|
/// A <see cref="ValueTask{PermissionAdapter}"/> representing the asynchronous execution of the service.
|
|
/// </returns>
|
|
public async ValueTask<PermissionAdapter?> ChangePermissionStatus(string _id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken)
|
|
{
|
|
var entity = await repository.FindByIdAsync(_id);
|
|
|
|
if (entity is not null)
|
|
{
|
|
entity.Status = newStatus;
|
|
|
|
return repository.ReplaceOneAsync(entity).Result;
|
|
}
|
|
else return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates a Permission.
|
|
/// </summary>
|
|
/// <param name="entity">The Permission to be updated.</param>
|
|
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
|
|
/// <returns>
|
|
/// A <see cref="ValueTask{PermissionAdapter}"/> representing the asynchronous execution of the service.
|
|
/// </returns>
|
|
public async ValueTask<PermissionAdapter?> UpdatePermission(PermissionAdapter entity, CancellationToken cancellationToken)
|
|
{
|
|
var updatedEntity = await repository.ReplaceOneAsync(entity);
|
|
return updatedEntity;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes a Permission by identifier.
|
|
/// </summary>
|
|
/// <param name="_id">The Permission Mongo identifier.</param>
|
|
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
|
|
/// <returns>
|
|
/// A <see cref="ValueTask{PermissionAdapter}"/> representing the asynchronous deletion result.
|
|
/// The deleted Permission entity if found; otherwise, null.
|
|
/// </returns>
|
|
public async ValueTask<PermissionAdapter?> DeletePermission(string _id, CancellationToken cancellationToken)
|
|
{
|
|
var entity = await this.repository.DeleteOneAsync(doc => doc._Id == _id);
|
|
return entity;
|
|
}
|
|
}
|
|
}
|