#48 added basic role features and some cleanUp
This commit is contained in:
49
src/Infrastructure/Identity/RoleService.cs
Normal file
49
src/Infrastructure/Identity/RoleService.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using Hutopy.Application.Common.Interfaces;
|
||||
using Hutopy.Application.Common.Models;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
|
||||
namespace Hutopy.Infrastructure.Identity;
|
||||
|
||||
public class RoleService(
|
||||
RoleManager<ApplicationRole> roleManager
|
||||
)
|
||||
: IRoleService
|
||||
{
|
||||
public async Task<Result> CreateRoleAsync(string roleName)
|
||||
{
|
||||
var role = new ApplicationRole { Name = roleName, Id = Guid.NewGuid().ToString()};
|
||||
var result = await roleManager.CreateAsync(role);
|
||||
|
||||
return result.ToApplicationResult();
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteRoleAsync(string roleName)
|
||||
{
|
||||
var role = new ApplicationRole { Name = roleName };
|
||||
var result = await roleManager.DeleteAsync(role);
|
||||
|
||||
return result.ToApplicationResult();
|
||||
}
|
||||
|
||||
public async Task<RoleModel?> FindRoleByIdAsync(string roleId)
|
||||
{
|
||||
var result = await roleManager.FindByIdAsync(roleId);
|
||||
|
||||
if (result is null) return null;
|
||||
|
||||
var roleModel = new RoleModel { Id = result.Id, Name = result.Name };
|
||||
|
||||
return roleModel;
|
||||
}
|
||||
|
||||
public async Task<RoleModel?> FindRoleByNameAsync(string roleName)
|
||||
{
|
||||
var result = await roleManager.FindByNameAsync(roleName);
|
||||
|
||||
if (result is null) return null;
|
||||
|
||||
var roleModel = new RoleModel { Id = result.Id, Name = result.Name };
|
||||
|
||||
return roleModel;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user