159 lines
5.9 KiB
C#
159 lines
5.9 KiB
C#
using Marco.Pms.DataAccess.Data;
|
|
using Marco.Pms.Model.MongoDBModels;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using MongoDB.Driver;
|
|
|
|
namespace Marco.Pms.CacheHelper
|
|
{
|
|
public class EmployeeCache
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
//private readonly IMongoDatabase _mongoDB;
|
|
private readonly IMongoCollection<EmployeePermissionMongoDB> _collection;
|
|
public EmployeeCache(ApplicationDbContext context, IConfiguration configuration)
|
|
{
|
|
var connectionString = configuration["MongoDB:ConnectionString"];
|
|
_context = context;
|
|
var mongoUrl = new MongoUrl(connectionString);
|
|
var client = new MongoClient(mongoUrl); // Your MongoDB connection string
|
|
var mongoDB = client.GetDatabase(mongoUrl.DatabaseName); // Your MongoDB Database name
|
|
_collection = mongoDB.GetCollection<EmployeePermissionMongoDB>("EmployeeProfile");
|
|
}
|
|
public async Task<bool> AddApplicationRoleToCache(Guid employeeId, List<Guid> roleIds)
|
|
{
|
|
var newRoleIds = roleIds.Select(r => r.ToString()).ToList();
|
|
var newPermissionIds = await _context.RolePermissionMappings
|
|
.Where(rp => roleIds.Contains(rp.ApplicationRoleId))
|
|
.Select(p => p.FeaturePermissionId.ToString())
|
|
.Distinct()
|
|
.ToListAsync();
|
|
|
|
var filter = Builders<EmployeePermissionMongoDB>.Filter.Eq(e => e.EmployeeId, employeeId.ToString());
|
|
|
|
var update = Builders<EmployeePermissionMongoDB>.Update
|
|
.AddToSetEach(e => e.ApplicationRoleIds, newRoleIds)
|
|
.AddToSetEach(e => e.PermissionIds, newPermissionIds);
|
|
|
|
var result = await _collection.UpdateOneAsync(filter, update, new UpdateOptions { IsUpsert = true });
|
|
if (result.MatchedCount == 0)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
public async Task<bool> AddProjectsToCache(Guid employeeId, List<Guid> projectIds)
|
|
{
|
|
var newprojectIds = projectIds.Select(p => p.ToString()).ToList();
|
|
|
|
var filter = Builders<EmployeePermissionMongoDB>.Filter.Eq(e => e.EmployeeId, employeeId.ToString());
|
|
|
|
var update = Builders<EmployeePermissionMongoDB>.Update
|
|
.AddToSetEach(e => e.ProjectIds, newprojectIds);
|
|
|
|
var result = await _collection.UpdateOneAsync(filter, update, new UpdateOptions { IsUpsert = true });
|
|
if (result.MatchedCount == 0)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
public async Task<List<Guid>> GetProjectsFromCache(Guid employeeId)
|
|
{
|
|
var filter = Builders<EmployeePermissionMongoDB>.Filter.Eq(e => e.EmployeeId, employeeId.ToString());
|
|
|
|
|
|
var result = await _collection
|
|
.Find(filter)
|
|
.FirstOrDefaultAsync();
|
|
|
|
var projectIds = new List<Guid>();
|
|
if (result != null)
|
|
{
|
|
projectIds = result.ProjectIds.Select(Guid.Parse).ToList();
|
|
}
|
|
|
|
return projectIds;
|
|
}
|
|
public async Task<List<Guid>> GetPermissionsFromCache(Guid employeeId)
|
|
{
|
|
var filter = Builders<EmployeePermissionMongoDB>.Filter.Eq(e => e.EmployeeId, employeeId.ToString());
|
|
|
|
|
|
var result = await _collection
|
|
.Find(filter)
|
|
.FirstOrDefaultAsync();
|
|
|
|
var permissionIds = new List<Guid>();
|
|
if (result != null)
|
|
{
|
|
permissionIds = result.PermissionIds.Select(Guid.Parse).ToList();
|
|
}
|
|
|
|
return permissionIds;
|
|
}
|
|
public async Task<bool> ClearAllProjectIdsFromCache(Guid employeeId)
|
|
{
|
|
var filter = Builders<EmployeePermissionMongoDB>.Filter
|
|
.Eq(e => e.EmployeeId, employeeId.ToString());
|
|
|
|
var update = Builders<EmployeePermissionMongoDB>.Update
|
|
.Set(e => e.ProjectIds, new List<string>());
|
|
|
|
var result = await _collection.UpdateOneAsync(filter, update);
|
|
|
|
if (result.MatchedCount == 0)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
public async Task<bool> RemoveRoleIdFromCache(Guid employeeId, Guid roleId)
|
|
{
|
|
var filter = Builders<EmployeePermissionMongoDB>.Filter
|
|
.Eq(e => e.EmployeeId, employeeId.ToString());
|
|
|
|
var update = Builders<EmployeePermissionMongoDB>.Update
|
|
.Pull(e => e.ApplicationRoleIds, roleId.ToString());
|
|
|
|
var result = await _collection.UpdateOneAsync(filter, update);
|
|
|
|
if (result.MatchedCount == 0)
|
|
return false;
|
|
|
|
if (result.ModifiedCount == 0)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
public async Task<bool> ClearAllPermissionIdsByEmployeeIDFromCache(Guid employeeId)
|
|
{
|
|
var filter = Builders<EmployeePermissionMongoDB>.Filter
|
|
.Eq(e => e.EmployeeId, employeeId.ToString());
|
|
|
|
var update = Builders<EmployeePermissionMongoDB>.Update
|
|
.Set(e => e.PermissionIds, new List<string>());
|
|
|
|
var result = await _collection.UpdateOneAsync(filter, update);
|
|
|
|
if (result.MatchedCount == 0)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
public async Task<bool> ClearAllPermissionIdsByRoleIdFromCache(Guid roleId)
|
|
{
|
|
var filter = Builders<EmployeePermissionMongoDB>.Filter.AnyEq(e => e.ApplicationRoleIds, roleId.ToString());
|
|
|
|
var update = Builders<EmployeePermissionMongoDB>.Update
|
|
.Set(e => e.PermissionIds, new List<string>());
|
|
|
|
var result = await _collection.UpdateOneAsync(filter, update);
|
|
|
|
if (result.MatchedCount == 0)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|