首页 > 其他分享 >造轮子之用户管理

造轮子之用户管理

时间:2023-10-12 11:55:06浏览次数:31  
标签:await pageRequest 管理 updateUserDto userManager 用户 user 轮子 public

跟角色管理一样,asp.net core identity中已经包含了UserManager,我们只需要简单包装一下逻辑即可完成我们的用户管理相关功能。
这里只打算添加分页查询,创建以及修改用户的API,不提供删除API。

实现UserManageAppService

IUserManageAppService

namespace Wheel.Services.Users
{
    public interface IUserManageAppService : ITransientDependency
    {
        Task<Page<UserDto>> GetUserPageList(UserPageRequest pageRequest);
        Task<R> CreateUser(CreateUserDto userDto);
        Task<R> UpdateUser(string userId, UpdateUserDto updateUserDto);
    }
}

UserManageAppService

namespace Wheel.Services.Users
{
    public class UserManageAppService : WheelServiceBase, IUserManageAppService
    {
        private readonly IBasicRepository<User, string> _userRepository;
        private readonly UserManager<User> _userManager;
        private readonly IUserStore<User> _userStore;

        public UserManageAppService(IBasicRepository<User, string> userRepository, UserManager<User> userManager, IUserStore<User> userStore)
        {
            _userRepository = userRepository;
            _userManager = userManager;
            _userStore = userStore;
        }

        public async Task<Page<UserDto>> GetUserPageList(UserPageRequest pageRequest)
        {
            var (items, total) = await _userRepository.SelectPageListAsync(
                _userRepository.BuildPredicate(
                    (!string.IsNullOrWhiteSpace(pageRequest.UserName), u => u.UserName.Contains(pageRequest.UserName)),
                    (!string.IsNullOrWhiteSpace(pageRequest.Email), u => u.Email.Contains(pageRequest.Email)),
                    (!string.IsNullOrWhiteSpace(pageRequest.PhoneNumber), u => u.PhoneNumber.Contains(pageRequest.PhoneNumber)),
                    (pageRequest.EmailConfirmed.HasValue, u => u.EmailConfirmed.Equals(pageRequest.EmailConfirmed)),
                    (pageRequest.PhoneNumberConfirmed.HasValue, u => u.PhoneNumberConfirmed.Equals(pageRequest.PhoneNumberConfirmed))
                    ),
                o => new UserDto
                {
                    UserName = o.UserName,
                    Email = o.Email,
                    PhoneNumber = o.PhoneNumber,
                    EmailConfirmed = o.EmailConfirmed,
                    PhoneNumberConfirmed = o.PhoneNumberConfirmed,
                    CreationTime = o.CreationTime
                },
                (pageRequest.PageIndex - 1) * pageRequest.PageSize,
                pageRequest.PageSize,
                pageRequest.OrderBy
                );

            return new Page<UserDto>(items, total);
        }
        public async Task<R> CreateUser(CreateUserDto userDto)
        {
            var user = new User();
            await _userManager.SetUserNameAsync(user, userDto.UserName);

            if (userDto.Email != null)
            {
                var emailStore = (IUserEmailStore<User>)_userStore;
                await emailStore.SetEmailAsync(user, userDto.Email, default);
            }

            var result = await _userManager.CreateAsync(user, userDto.Passowrd);
            if (result.Succeeded)
            {
                if (userDto.Roles.Count > 0)
                {
                    await _userManager.AddToRolesAsync(user, userDto.Roles);
                    await _userManager.UpdateAsync(user);
                }
                return new R();
            }
            else
                throw new BusinessException(ErrorCode.CreateUserError, string.Join("\r\n", result.Errors.Select(a => a.Description)));
        }
        public async Task<R> UpdateUser(string userId, UpdateUserDto updateUserDto)
        {
            var user = await _userManager.FindByIdAsync(userId);
            if (user == null)
            {
                throw new BusinessException(ErrorCode.UserNotExist, L["UserNotExist"]);
            }
            if (updateUserDto.Email != null)
            {
                var emailStore = (IUserEmailStore<User>)_userStore;
                await emailStore.SetEmailAsync(user, updateUserDto.Email, default);
            }
            if (updateUserDto.PhoneNumber != null)
            {
                await _userManager.SetPhoneNumberAsync(user, updateUserDto.PhoneNumber);
            }
            if (updateUserDto.Roles.Count > 0)
            {
                var existRoles = await _userManager.GetRolesAsync(user);
                await _userManager.RemoveFromRolesAsync(user, existRoles);
                await _userManager.AddToRolesAsync(user, updateUserDto.Roles);
            }
            await _userManager.UpdateAsync(user);
            return new R();
        }
    }
}

实现UserManageController

namespace Wheel.Controllers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class UserManageController : WheelControllerBase
    {
        private readonly IUserManageAppService _userManageAppService;

        public UserManageController(IUserManageAppService userManageAppService)
        {
            _userManageAppService = userManageAppService;
        }
        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="userDto"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<R> CreateUser(CreateUserDto userDto)
        {
            return _userManageAppService.CreateUser(userDto);
        }
        /// <summary>
        /// 用户分页查询
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<Page<UserDto>> GetUserPageList([FromQuery]UserPageRequest pageRequest)
        {
            return _userManageAppService.GetUserPageList(pageRequest);
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="updateUserDto"></param>
        /// <returns></returns>
        [HttpPut("{userId}")]
        public Task<R> UpdateUser(string userId, UpdateUserDto updateUserDto)
        {
            return _userManageAppService.UpdateUser(userId, updateUserDto);
        }
    }
}

非常简单就实现了一个用户管理的API。

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

image.png

标签:await,pageRequest,管理,updateUserDto,userManager,用户,user,轮子,public
From: https://www.cnblogs.com/fanshaoO/p/17759176.html

相关文章