Skip to content

Instantly share code, notes, and snippets.

@ginomessmer
Created May 25, 2020 09:25
Show Gist options
  • Save ginomessmer/bd534ae64e3e89f9aa876bd60fc7fedc to your computer and use it in GitHub Desktop.
Save ginomessmer/bd534ae64e3e89f9aa876bd60fc7fedc to your computer and use it in GitHub Desktop.
Unidash Command
using MediatR;
namespace Unidash.Auth.Users.Commands
{
public class CreateUserCommand : IRequest
{
public string Id { get; private set; }
public string DisplayName { get; private set; }
public string EmailAddress { get; private set; }
public string Password { get; private set; }
public CreateUserCommand(string id, string displayName, string emailAddress, string password)
{
Id = id;
DisplayName = displayName;
EmailAddress = emailAddress;
Password = password;
}
}
}
using System.Threading;
using System.Threading.Tasks;
using AutoMapper;
using Unidash.Auth.Domain.UserAggregate;
using Unidash.Core.Infrastructure;
using Unidash.Core.Security;
using MediatR;
namespace Unidash.Auth.Users.Commands
{
public class CreateUserCommandHandler : IRequestHandler<CreateUserCommand>
{
private readonly IEntityRepository<User> _repository;
private readonly IMapper _mapper;
private readonly PasswordService _passwordService;
public CreateUserCommandHandler(IEntityRepository<User> repository, IMapper mapper,
PasswordService passwordService)
{
_repository = repository;
_mapper = mapper;
_passwordService = passwordService;
}
public async Task<Unit> Handle(CreateUserCommand request, CancellationToken cancellationToken)
{
var user = _mapper.Map<User>(request);
user.PasswordSalt = _passwordService.GenerateRandomSaltAsBase64();
user.Password = _passwordService.Hash(user.Password, user.PasswordSalt);
await _repository.GetOrCreateAsync(user.Id, user);
return Unit.Value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment