Files
social-media/src/Infrastructure/Identity/RoleService.cs
2024-06-02 15:59:17 -04:00

50 lines
1.4 KiB
C#

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;
}
}