Files
pos-system/services/iam-service-net/src/IamService.API/Application/Commands/Auth/RegisterUserCommandHandler.cs
Ho Ngoc Hai 07f96a8eb2 feat(docs): Enhance Vietnamese documentation with new sections and updates
- Added new sections on API Design, Caching Patterns, and Testing Patterns to the Vietnamese documentation.
- Updated sidebar configurations for improved navigation and accessibility.
- Removed outdated onboarding guides to streamline content and focus on relevant resources.
2026-01-12 13:36:53 +07:00

72 lines
2.5 KiB
C#

using MediatR;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using IamService.Domain.AggregatesModel.UserAggregate;
using IamService.Domain.Events;
namespace IamService.API.Application.Commands.Auth;
/// <summary>
/// EN: Handler for RegisterUserCommand.
/// VI: Handler cho RegisterUserCommand.
/// </summary>
public class RegisterUserCommandHandler : IRequestHandler<RegisterUserCommand, RegisterUserCommandResult>
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly ILogger<RegisterUserCommandHandler> _logger;
public RegisterUserCommandHandler(
UserManager<ApplicationUser> userManager,
ILogger<RegisterUserCommandHandler> logger)
{
_userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<RegisterUserCommandResult> Handle(
RegisterUserCommand request,
CancellationToken cancellationToken)
{
_logger.LogInformation(
"Registering new user with email {Email}",
request.Email);
// EN: Check if user already exists
// VI: Kiểm tra xem user đã tồn tại chưa
var existingUser = await _userManager.FindByEmailAsync(request.Email);
if (existingUser != null)
{
_logger.LogWarning("User with email {Email} already exists", request.Email);
throw new InvalidOperationException($"User with email {request.Email} already exists");
}
// EN: Create new user
// VI: Tạo user mới
var user = new ApplicationUser(request.Email, request.FirstName, request.LastName);
// EN: Add domain event
// VI: Thêm domain event
user.AddDomainEvent(new UserRegisteredDomainEvent(user));
// EN: Create user with password
// VI: Tạo user với password
var result = await _userManager.CreateAsync(user, request.Password);
if (!result.Succeeded)
{
var errors = string.Join(", ", result.Errors.Select(e => e.Description));
_logger.LogWarning("Failed to create user: {Errors}", errors);
throw new InvalidOperationException($"Failed to create user: {errors}");
}
_logger.LogInformation(
"Successfully registered user {UserId} with email {Email}",
user.Id, user.Email);
return new RegisterUserCommandResult(
user.Id,
user.Email!,
user.FullName);
}
}