38 lines
1.4 KiB
C#
38 lines
1.4 KiB
C#
using Hutopy.Application.Common.Interfaces;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace Hutopy.Application.Users.Commands;
|
|
public record CreateUserCommand : IRequest<IResult>
|
|
{
|
|
public required string FirstName { get; init; }
|
|
public required string LastName { get; init; }
|
|
public required string EmailAddress { get; init; }
|
|
public required string UserName { get; init; }
|
|
public required string Password { get; init; }
|
|
}
|
|
|
|
public class CreateUserCommandHandler : IRequestHandler<CreateUserCommand, IResult>
|
|
{
|
|
private readonly IApplicationDbContext _context;
|
|
private readonly IIdentityService _identityService;
|
|
|
|
public CreateUserCommandHandler(IApplicationDbContext context, IIdentityService identityService)
|
|
{
|
|
_context = context;
|
|
_identityService = identityService;
|
|
}
|
|
|
|
public async Task<IResult> Handle(CreateUserCommand request, CancellationToken cancellationToken)
|
|
{
|
|
await _identityService.CreateUserAsync(request.EmailAddress, request.UserName, request.FirstName, request.LastName, request.Password);
|
|
|
|
var user = await _identityService.FindUserByEmailAsync(request.EmailAddress);
|
|
|
|
if (user is null) throw new InvalidOperationException("This should never happen, we just created the user.");
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return Results.Ok(user.Id);
|
|
}
|
|
}
|