MyBookmark/Modules.Account.Application/Commands/CreateAccountCommand.cs
2024-10-21 01:52:41 +03:00

25 lines
972 B
C#

using MediatR;
using Modules.Account.Application.Gateways;
using Modules.Account.Domain.Gateways;
namespace Modules.Account.Application.Commands;
public class CreateAccountCommand : IRequest<Guid>
{
public string Email { get; set; } = default!;
public string Password { get; set; } = default!;
}
public class CreateAccountCommandHandler(IAccountGateway gateway, IPasswordHasher passwordHasher) : IRequestHandler<CreateAccountCommand, Guid>
{
public async Task<Guid> Handle(CreateAccountCommand request, CancellationToken cancellationToken)
{
if (await gateway.IsExists(request.Email)) throw new Exception("Account with the same eamil already exists");
var newAccount = Domain.Entities.Account.Create(request.Email, request.Password, passwordHasher);
return await gateway.Create(new Models.Account
{
Email = newAccount.Email.Value,
HashedPassword = newAccount.HashedPassword,
});
}
}