48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
using LanguageExt.Common;
|
|
using MediatR;
|
|
using W542.GandalfReborn.Commands;
|
|
using W542.GandalfReborn.Data.Database.Repositories;
|
|
using W542.GandalfReborn.Data.Entities.Base;
|
|
using W542.GandalfReborn.Data.Entities.Subject;
|
|
using W542.GandalfReborn.Data.Entities.Subject.SignIn;
|
|
using W542.GandalfReborn.Data.Entities.Tenant;
|
|
using W542.GandalfReborn.Extensions;
|
|
using W542.GandalfReborn.Handlers.Security;
|
|
|
|
namespace W542.GandalfReborn.Handlers.Commands;
|
|
|
|
public class RegisterCommandHandler(ISubjectRepository subjectRepository, IMediator mediator) : IGrCommandHandler<RegisterCommand, Result<SubjectEntity>>
|
|
{
|
|
public async Task<Result<SubjectEntity>> Handle(RegisterCommand command, CancellationToken cancellationToken)
|
|
{
|
|
var passwordHashResult = await mediator.Send(new HashPasswordCommand { RawPassword = command.Password }, cancellationToken);
|
|
|
|
if (passwordHashResult.IsFaulted)
|
|
{
|
|
return passwordHashResult.AsErrorResult<SubjectEntity, string>();
|
|
}
|
|
|
|
var passwordHash = passwordHashResult.GetValue();
|
|
|
|
var subject = new SubjectEntity
|
|
{
|
|
Visibility = EntityVisibility.Active,
|
|
Name = command.Username,
|
|
SignInMethods =
|
|
[
|
|
new SignInEntity
|
|
{
|
|
Visibility = EntityVisibility.Active,
|
|
Method = SignInMethod.Simple,
|
|
IsLegacy = false,
|
|
PasswordHash = passwordHash,
|
|
Email = command.Email
|
|
}
|
|
]
|
|
};
|
|
|
|
var upsertResult = await subjectRepository.Upsert(subject);
|
|
|
|
return upsertResult;
|
|
}
|
|
} |