Exemple #1
0
        public async Task ExecuteAsync(UploadDocumentCommand command, CancellationToken cancellationToken = default)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

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

            using (var stream = command.File.OpenReadStream())
            {
                await _fileClient.UploadAsync(
                    fileName : $"{command.Subject}{Path.GetExtension(command.File.FileName)}",
                    userId : command.Actor,
                    stream : stream,
                    cancellationToken : cancellationToken
                    );
            }

            var aggregate = _aggregateFactory.FromHistory <Document, DocumentState>(Enumerable.Empty <IEvent>());

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

            aggregate.Upload(
                subject: command.Subject,
                user: command.Actor,
                translationType: command.TranslationType,
                fileName: command.File.FileName,
                translationSubject: command.TranslationSubject
                );

            await _aggregateRepository.SaveAsync(aggregate, command, 0, cancellationToken);
        }
        public async Task ExecuteAsync(RegisterUserCommand 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.FindByEmailAsync(command.Email);

            if (user != null)
            {
                if (user.EmailConfirmed)
                {
                    throw new EmailInUseException();
                }
                else
                {
                    await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    throw new UserNotVerifiedException();
                }
            }

            user = new SIOUser
            {
                Id        = command.Subject,
                Email     = command.Email,
                FirstName = command.FirstName,
                LastName  = command.LastName,
                UserName  = command.Email,
            };

            var userResult = await _userManager.CreateAsync(user);

            if (!userResult.Succeeded)
            {
                throw new UserCreationException();
            }

            var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            await _userManager.UpdateAsync(user);

            var aggregate = _aggregateFactory.FromHistory <User, UserState>(Enumerable.Empty <IEvent>());

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

            aggregate.Register(
                subject: user.Id,
                email: user.Email,
                firstName: user.FirstName,
                lastName: user.LastName,
                activationToken: token
                );

            await _aggregateRepository.SaveAsync(aggregate, command, cancellationToken : cancellationToken);
        }