173 lines
6.7 KiB
C#
173 lines
6.7 KiB
C#
// ***********************************************************************
|
|
// <copyright file="RoleService.cs">
|
|
// AgileWebs
|
|
// </copyright>
|
|
// ***********************************************************************
|
|
using Core.Blueprint.Mongo;
|
|
using Core.Blueprint.Redis;
|
|
using Core.Blueprint.Redis.Helpers;
|
|
using Core.Thalos.Adapters;
|
|
using Core.Thalos.Adapters.Common.Enums;
|
|
using Core.Thalos.Domain.Contexts.Onboarding.Request;
|
|
using Core.Thalos.Provider.Contracts;
|
|
using Mapster;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.Graph;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Bson.Serialization;
|
|
using MongoDB.Driver;
|
|
using System.ComponentModel.Design;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Core.Thalos.Provider.Providers.Onboarding
|
|
{
|
|
/// <summary>
|
|
/// Handles all services and business rules related to <see cref="RoleAdapter"/>.
|
|
/// </summary>
|
|
public class RoleProvider : IRoleProvider
|
|
{
|
|
private readonly CollectionRepository<RoleAdapter> repository;
|
|
private readonly CacheSettings cacheSettings;
|
|
private readonly IRedisCacheProvider cacheProvider;
|
|
|
|
public RoleProvider(CollectionRepository<RoleAdapter> repository,
|
|
IRedisCacheProvider cacheProvider,
|
|
IOptions<CacheSettings> cacheSettings
|
|
)
|
|
{
|
|
this.repository = repository;
|
|
this.repository.CollectionInitialization();
|
|
this.cacheSettings = cacheSettings.Value;
|
|
this.cacheProvider = cacheProvider;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new Role.
|
|
/// </summary>
|
|
/// <param name="entity">The Role to be created.</param>
|
|
/// <returns>A <see cref="{Task{RoleAdapter}}"/> representing
|
|
/// the asynchronous execution of the service.</returns>
|
|
public async ValueTask<RoleAdapter> CreateRole(RoleRequest newRole, CancellationToken cancellationToken)
|
|
{
|
|
var roleCollection = newRole.Adapt<RoleAdapter>();
|
|
|
|
await repository.InsertOneAsync(roleCollection);
|
|
|
|
return roleCollection;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets an Role by identifier.
|
|
/// </summary>
|
|
/// <param name="id">The Role identifier.</param>
|
|
/// <returns>A <see cref="{Task{RoleAdapter}}"/> representing
|
|
/// the asynchronous execution of the service.</returns>
|
|
public async ValueTask<RoleAdapter> GetRoleById(string _id, CancellationToken cancellationToken)
|
|
{
|
|
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetRoleById", _id);
|
|
var cachedData = await cacheProvider.GetAsync<RoleAdapter>(cacheKey);
|
|
|
|
if (cachedData is not null) { return cachedData; }
|
|
|
|
var role = await repository.FindByIdAsync(_id);
|
|
|
|
await cacheProvider.SetAsync(cacheKey, role);
|
|
|
|
return role;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets all the roles.
|
|
/// </summary>
|
|
/// <returns>A <see cref="{Task{IEnumerbale{RoleAdapter}}}"/> representing
|
|
/// the asynchronous execution of the service.</returns>
|
|
public async ValueTask<IEnumerable<RoleAdapter>> GetAllRoles(CancellationToken cancellationToken)
|
|
{
|
|
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllRoles");
|
|
var cachedData = await cacheProvider.GetAsync<IEnumerable<RoleAdapter>>(cacheKey) ?? [];
|
|
|
|
if (cachedData.Any()) return cachedData;
|
|
|
|
var roles = await repository.AsQueryable();
|
|
|
|
await cacheProvider.SetAsync(cacheKey, roles);
|
|
|
|
return roles;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Changes the status of the role.
|
|
/// </summary>
|
|
/// <param name="id">The role identifier.</param>
|
|
/// <param name="newStatus">The new status of the role.</param>
|
|
/// <returns>A <see cref="{Task{RoleAdapter}}"/> representing
|
|
/// the asynchronous execution of the service.</returns>
|
|
public async ValueTask<RoleAdapter> ChangeRoleStatus(string id, Core.Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken)
|
|
{
|
|
var entity = await repository.FindByIdAsync(id);
|
|
entity.Status = newStatus;
|
|
|
|
await repository.ReplaceOneAsync(entity);
|
|
|
|
return entity;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates a Role by id.
|
|
/// </summary>
|
|
/// <param name="entity">The Role to be updated.</param>
|
|
/// <param name="id">The Role identifier.</param>
|
|
/// <returns>A <see cref="{Task{RoleAdapter}}"/> representing
|
|
/// the asynchronous execution of the service.</returns>
|
|
public async ValueTask<RoleAdapter> UpdateRole(RoleAdapter entity, CancellationToken cancellationToken)
|
|
{
|
|
await repository.ReplaceOneAsync(entity);
|
|
|
|
return entity;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds an application to the role's list of applications.
|
|
/// </summary>
|
|
/// <param name="roleId">The identifier of the role to which the application will be added.</param>
|
|
/// <param name="application">The application enum value to add.</param>
|
|
/// <returns>A <see cref="{Task{RoleAdapter}}"/> representing the asynchronous operation, with the updated role object.</returns>
|
|
public async ValueTask<RoleAdapter> AddApplicationToRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken)
|
|
{
|
|
var role = await repository.FindOneAsync(
|
|
u => u._Id == roleId &&
|
|
u.Status == Core.Blueprint.Mongo.StatusEnum.Active);
|
|
|
|
var updatedApplications = role.Applications.Append(application).Distinct().ToArray();
|
|
role.Applications = updatedApplications;
|
|
|
|
await repository.ReplaceOneAsync(role);
|
|
|
|
return role;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes an application from the role's list of applications.
|
|
/// </summary>
|
|
/// <param name="roleId">The identifier of the role from which the application will be removed.</param>
|
|
/// <param name="application">The application enum value to remove.</param>
|
|
/// <returns>A <see cref="{Task{RoleAdapter}}"/> representing the asynchronous operation, with the updated role object.</returns>
|
|
public async ValueTask<RoleAdapter> RemoveApplicationFromRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken)
|
|
{
|
|
var role = await repository.FindOneAsync(
|
|
u => u._Id == roleId &&
|
|
u.Status == Core.Blueprint.Mongo.StatusEnum.Active);
|
|
|
|
var updatedApplications = role.Applications
|
|
?.Where(c => c != application)
|
|
.ToArray();
|
|
|
|
role.Applications = updatedApplications;
|
|
|
|
await repository.ReplaceOneAsync(role);
|
|
|
|
return role;
|
|
}
|
|
}
|
|
}
|