public async Task HandleAsync(DeleteProductCommand command, CancellationToken cancellationToken = default)
        {
            var aggregateProduct = await _repo.GetAsync(command.Id, cancellationToken : cancellationToken);

            if (aggregateProduct == null)
            {
                throw new CannotFindException("No data on this Id");
            }

            aggregateProduct.Delete(command);
            await _repo.SaveAsync(aggregateProduct, cancellationToken);
        }
        public async Task ExecuteAsync(LogoutCommand command, CancellationToken cancellationToken = default)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (cancellationToken.IsCancellationRequested)
            {
                _logger.LogInformation($"{nameof(LogoutCommandHandler)}.{nameof(ExecuteAsync)} was cancelled before execution");
                cancellationToken.ThrowIfCancellationRequested();
            }

            await _signInManager.SignOutAsync();

            var logout = await _interaction.GetLogoutContextAsync(command.LogoutId);

            await _persistedGrantService.RemoveAllGrantsAsync(command.Subject, logout?.ClientId);

            var aggregate = await _aggregateRepository.GetAsync <User, UserState>(command.Subject, cancellationToken);

            var expectedVersion = aggregate.Version;

            if (aggregate == null)
            {
                throw new ArgumentNullException(nameof(aggregate));
            }

            aggregate.Logout();

            await _aggregateRepository.SaveAsync(aggregate, command, expectedVersion, cancellationToken);
        }
        public async Task ProcessAsync(string aggregateId, Action <TAggregate> command, CancellationToken cancellationToken = default)
        {
            var aggregate = await _aggregateRepository.GetAsync(aggregateId, cancellationToken);

            command?.Invoke(aggregate);
            await _aggregateRepository.SaveAsync(aggregate, cancellationToken);
        }
        async Task IdempotentlyCreateAgg(string id, Action <OrganizationAggregate> usingThisMethod)
        {
            var agg = await AggRepository.GetAsync <OrganizationAggregate>(id);

            if (agg == null)
            {
                agg = new OrganizationAggregate(new OrganizationAggregateState());
            }
            var ov = agg.Version;

            usingThisMethod(agg);
            PublishedEvents = agg.PublishedEvents;
            if (ov != agg.Version)
            {
                await AggRepository.StoreAsync(agg);
            }
        }
Exemple #5
0
        public static async Task UpdateOutOfSession(string aggId, IAggregateRepository rep)
        {
            var agg = await rep.GetAsync <PersonAggregate>(aggId);

            agg.Rename(new RenamePerson()
            {
                Id = aggId, Name = "Renamed out of session"
            });
            await rep.StoreAsync(agg);
        }
        public async Task ExecuteAsync(VerifyUserCommand command, CancellationToken cancellationToken = default)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (cancellationToken.IsCancellationRequested)
            {
                _logger.LogInformation($"{nameof(RegisterUserCommandHandler)}.{nameof(ExecuteAsync)} was cancelled before execution");
                cancellationToken.ThrowIfCancellationRequested();
            }

            var user = await _userManager.FindByIdAsync(command.Subject);

            var confirmationResult = await _userManager.ConfirmEmailAsync(user, Encoding.UTF8.GetString(Convert.FromBase64String(command.Token)));

            if (!confirmationResult.Succeeded)
            {
                throw new EmailConfirmationException(confirmationResult.Errors.First().Description);
            }

            var addPasswordResult = await _userManager.AddPasswordAsync(user, command.Password);

            if (!addPasswordResult.Succeeded)
            {
                throw new PasswordCreationException(addPasswordResult.Errors.First().Description);
            }

            var aggregate = await _aggregateRepository.GetAsync <User, UserState>(command.Subject, cancellationToken);

            var expectedVersion = aggregate.Version;

            if (aggregate == null)
            {
                throw new ArgumentNullException(nameof(aggregate));
            }

            var streamId = StreamId.New();

            await _userManager.UpdateAsync(user);

            aggregate.Verify();

            await _signInManager.SignInAsync(user, false);

            aggregate.Login();

            await _aggregateRepository.SaveAsync(aggregate, command, expectedVersion, cancellationToken);
        }
Exemple #7
0
        public async Task ExecuteAsync(ForgotPasswordCommand command, CancellationToken cancellationToken = default)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (cancellationToken.IsCancellationRequested)
            {
                _logger.LogInformation($"{nameof(ForgotPasswordCommandHandler)}.{nameof(ExecuteAsync)} was cancelled before execution");
                cancellationToken.ThrowIfCancellationRequested();
            }

            var user = await _userManager.FindByIdAsync(command.Subject);

            if (user != null)
            {
                if (!user.EmailConfirmed)
                {
                    throw new UserNotVerifiedException();
                }

                if (user.IsArchived)
                {
                    throw new UserIsArchivedException();
                }

                var token = await _userManager.GeneratePasswordResetTokenAsync(user);

                await _userManager.UpdateAsync(user);

                var aggregate = await _aggregateRepository.GetAsync <User, UserState>(command.Subject, cancellationToken);

                var expectedVersion = aggregate.Version;

                if (aggregate == null)
                {
                    throw new ArgumentNullException(nameof(aggregate));
                }

                aggregate.RequestToken(token);

                await _aggregateRepository.SaveAsync(aggregate, command, expectedVersion, cancellationToken);
            }
            else
            {
                throw new UserDoesntExistException();
            }
        }
Exemple #8
0
        public async Task ExecuteAsync(LoginCommand command, CancellationToken cancellationToken = default)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (cancellationToken.IsCancellationRequested)
            {
                _logger.LogInformation($"{nameof(LoginCommandHandler)}.{nameof(ExecuteAsync)} was cancelled before execution");
                cancellationToken.ThrowIfCancellationRequested();
            }

            var user = await _userManager.FindByIdAsync(command.Subject);

            if (user == null)
            {
                throw new UserDoesntExistException();
            }

            if (await _userManager.IsLockedOutAsync(user))
            {
                throw new UserIsLockedOutException();
            }

            var aggregate = await _aggregateRepository.GetAsync <User, UserState>(command.Subject, cancellationToken);

            var expectedVersion = aggregate.Version;

            if (aggregate == null)
            {
                throw new ArgumentNullException(nameof(aggregate));
            }

            if (!user.EmailConfirmed)
            {
                var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                await _userManager.UpdateAsync(user);

                aggregate.RequestToken(token);
                await _aggregateRepository.SaveAsync(aggregate, command, expectedVersion, cancellationToken);

                throw new UserNotVerifiedException();
            }

            if (user.IsArchived)
            {
                throw new UserIsArchivedException();
            }

            var validLogin = await _userManager.CheckPasswordAsync(user, command.Password);

            if (!validLogin)
            {
                await _userManager.AccessFailedAsync(user);

                throw new IncorrectPasswordException();
            }

            aggregate.Login();

            await _signInManager.SignInAsync(user, false);

            await _userManager.ResetAccessFailedCountAsync(user);

            await _userManager.UpdateAsync(user);

            await _aggregateRepository.SaveAsync(aggregate, command, expectedVersion, cancellationToken);
        }
Exemple #9
0
 public async Task <IAggregateRoot> GetAsync(string aggregateRootId)
 {
     return(await _aggregateRepository.GetAsync(aggregateRootId).ConfigureAwait(false));
 }
Exemple #10
0
 public async Task <IAggregateRoot> GetAsync(string aggregateRootId)
 {
     return(await _aggregateRepository.GetAsync(aggregateRootId));
 }