You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
3.3 KiB
92 lines
3.3 KiB
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.Extensions.Logging;
|
|
using MediatR;
|
|
using CellularManagement.Domain.Entities;
|
|
using CellularManagement.Domain.Repositories;
|
|
using CellularManagement.Domain.Common;
|
|
using System.Threading.Tasks;
|
|
using System.Threading;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
namespace CellularManagement.Application.Features.Users.Commands.CreateUser;
|
|
|
|
/// <summary>
|
|
/// 创建用户命令处理器
|
|
/// 处理创建新用户的业务逻辑
|
|
/// </summary>
|
|
public sealed class CreateUserCommandHandler : IRequestHandler<CreateUserCommand, OperationResult<CreateUserResponse>>
|
|
{
|
|
private readonly UserManager<AppUser> _userManager;
|
|
private readonly ILogger<CreateUserCommandHandler> _logger;
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="userManager">用户管理器,用于管理用户身份</param>
|
|
/// <param name="logger">日志记录器</param>
|
|
/// <param name="unitOfWork">工作单元,用于事务管理</param>
|
|
public CreateUserCommandHandler(
|
|
UserManager<AppUser> userManager,
|
|
ILogger<CreateUserCommandHandler> logger,
|
|
IUnitOfWork unitOfWork)
|
|
{
|
|
_userManager = userManager;
|
|
_logger = logger;
|
|
_unitOfWork = unitOfWork;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理创建用户命令
|
|
/// </summary>
|
|
/// <param name="request">创建用户命令</param>
|
|
/// <param name="cancellationToken">取消令牌</param>
|
|
/// <returns>操作结果,包含创建的用户ID</returns>
|
|
public async Task<OperationResult<CreateUserResponse>> Handle(
|
|
CreateUserCommand request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
// 检查用户是否已存在
|
|
var existingUser = await _userManager.FindByEmailAsync(request.Email);
|
|
if (existingUser != null)
|
|
{
|
|
_logger.LogWarning("用户邮箱 {Email} 已存在", request.Email);
|
|
return OperationResult<CreateUserResponse>.CreateFailure("用户已存在");
|
|
}
|
|
|
|
// 创建新用户
|
|
var user = new AppUser
|
|
{
|
|
UserName = request.UserName,
|
|
Email = request.Email,
|
|
PhoneNumber = request.PhoneNumber
|
|
};
|
|
|
|
// 在事务中创建用户
|
|
await _unitOfWork.ExecuteTransactionAsync(async () =>
|
|
{
|
|
var result = await _userManager.CreateAsync(user, request.Password);
|
|
if (!result.Succeeded)
|
|
{
|
|
var errors = result.Errors.Select(e => e.Description).ToList();
|
|
_logger.LogWarning("创建用户失败: {Errors}", string.Join(", ", errors));
|
|
throw new InvalidOperationException(string.Join(", ", errors));
|
|
}
|
|
}, cancellationToken: cancellationToken);
|
|
|
|
_logger.LogInformation("用户 {UserId} 创建成功", user.Id);
|
|
|
|
return OperationResult<CreateUserResponse>.CreateSuccess(
|
|
new CreateUserResponse(user.Id));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "创建用户时发生错误");
|
|
_logger.LogError(ex, "Error creating user");
|
|
return OperationResult<CreateUserResponse>.CreateFailure("Failed to create user");
|
|
}
|
|
}
|
|
}
|