Loading

造轮子之角色管理

在asp.net core identity中已经有RoleManager,我们只需要封装一下API操作Role即可完成我们角色管理相关功能,这里API打算只提供分页查询,创建以及删除,不提供修改API。

实现RoleManageAppService

IRoleManageAppService

namespace Wheel.Services.Roles
{
    public interface IRoleManageAppService : ITransientDependency
    {
        Task<Page<RoleDto>> GetRolePageList(PageRequest pageRequest);
        Task<R> CreateRole(CreateRoleDto dto);
        Task<R> DeleteRole(string roleName);
    }
}

RoleManageAppService

namespace Wheel.Services.Roles
{
    public class RoleManageAppService : WheelServiceBase, IRoleManageAppService
    {
        private readonly RoleManager<Role> _roleManager;
        private readonly IBasicRepository<Role, string> _roleRepository;

        public RoleManageAppService(RoleManager<Role> roleManager, IBasicRepository<Role, string> roleRepository)
        {
            _roleManager = roleManager;
            _roleRepository = roleRepository;
        }
        public async Task<Page<RoleDto>> GetRolePageList(PageRequest pageRequest)
        {
            var (items, total) = await _roleRepository.SelectPageListAsync(
                a => true,
                a => new RoleDto { Id = a.Id, Name = a.Name },
                (pageRequest.PageIndex - 1) * pageRequest.PageSize,
                pageRequest.PageSize,
                pageRequest.OrderBy
                );
            return new Page<RoleDto>(items, total);
        }

        public async Task<R> CreateRole(CreateRoleDto dto)
        {
            var exist = await _roleManager.RoleExistsAsync(dto.Name);
            if (exist)
            {
                throw new BusinessException(ErrorCode.RoleExist, "RoleExist");
            }
            var result = await _roleManager.CreateAsync(new Role(dto.Name, dto.RoleType));
            if(result.Succeeded)
            {
                return new R();
            }
            else
            {
                throw new BusinessException(ErrorCode.CreateRoleError, string.Join("\r\n", result.Errors.Select(a => a.Description)));
            }
        }

        public async Task<R> DeleteRole(string roleName)
        {
            var exist = await _roleManager.RoleExistsAsync(roleName);
            if (exist)
            {
                var role = await _roleManager.FindByNameAsync(roleName);
                await _roleManager.DeleteAsync(role);
            }
            else
            {
                throw new BusinessException(ErrorCode.RoleNotExist, "RoleNotExist");
            }
            return new R();
        }
    }
}

实现RoleManageController

namespace Wheel.Controllers
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class RoleManageController : WheelControllerBase
    {
        private readonly IRoleManageAppService _roleManageAppService;

        public RoleManageController(IRoleManageAppService roleManageAppService)
        {
            _roleManageAppService = roleManageAppService;
        }
        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<R> CreateRole(CreateRoleDto dto)
        {
            return await _roleManageAppService.CreateRole(dto);
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<R> DeleteRole(string roleName)
        {
            return await _roleManageAppService.DeleteRole(roleName);
        }
        /// <summary>
        /// 角色分页查询
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpGet("Page")]
        public async Task<Page<RoleDto>> GetRolePageList([FromQuery]PageRequest pageRequest)
        {
            return await _roleManageAppService.GetRolePageList(pageRequest);
        }
    }
}

非常简单就实现了一个角色管理的API。

轮子仓库地址https://github.com/Wheel-Framework/Wheel
欢迎进群催更。

image.png

posted @ 2023-10-12 11:49  饭勺oO  阅读(151)  评论(0编辑  收藏  举报