using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; namespace BlueWest.WebApi.Context.Users; /// /// Role storage management /// public class RoleStore : IRoleStore { private ApplicationUserDbContext _dbContext; /// /// Role Store constructor /// /// public RoleStore(ApplicationUserDbContext dbContext) { _dbContext = dbContext; } /// /// /// /// public void Dispose() { _dbContext = null; } /// /// Get role name /// /// /// /// /// public async Task GetRoleNameAsync(ApplicationUserRole role, CancellationToken cancellationToken) { var foundRole = await _dbContext.Roles .FirstOrDefaultAsync(x => x.Id == role.RoleId, cancellationToken: cancellationToken); if (foundRole != null) { return foundRole.Name; } return string.Empty; } public async Task CreateAsync(ApplicationRole role, CancellationToken cancellationToken) { _dbContext.Roles.Add(role); return await _dbContext.SaveChangesAsync(cancellationToken) >= 0 ? IdentityResult.Success : IdentityResult.Failed(); } public async Task UpdateAsync(ApplicationRole role, CancellationToken cancellationToken) { _dbContext.Roles.Update(role); return await _dbContext.SaveChangesAsync(cancellationToken) >= 0 ? IdentityResult.Success : IdentityResult.Failed(); } public async Task DeleteAsync(ApplicationRole role, CancellationToken cancellationToken) { _dbContext.Roles.Remove(role); return await _dbContext.SaveChangesAsync(cancellationToken) >= 0 ? IdentityResult.Success : IdentityResult.Failed(); } public async Task GetRoleIdAsync(ApplicationRole role, CancellationToken cancellationToken) { var x = await _dbContext.Roles.FirstOrDefaultAsync(x => x.Id == role.Id, cancellationToken: cancellationToken); if (x != null) { return x.Id; } return string.Empty; } public Task GetRoleNameAsync(ApplicationRole role, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task SetRoleNameAsync(ApplicationRole role, string roleName, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task GetNormalizedRoleNameAsync(ApplicationRole role, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task SetNormalizedRoleNameAsync(ApplicationRole role, string normalizedName, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task FindByIdAsync(string roleId, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken) { throw new NotImplementedException(); } }