Пример #1
0
        public async Task Execute_Should_ReturnError()
        {
            var request = _fixture.Create <UserAdd>();
            var root    = Substitute.For <IUserAggregationRoot>();

            _store.Create()
            .Returns(root);

            var fail = _fixture.Create <ErrorResult>();

            root.Create(request.Email, request.FirstName, request.LastNames, request.BirthDate)
            .Returns(fail);

            var result = await _operation.ExecuteAsync(request, CancellationToken.None);

            result.IsSuccess.Should().BeFalse();
            result.Value.Should().BeNull();
            result.Should().Be(fail);

            _store
            .Received(1)
            .Create();

            root
            .Received(1)
            .Create(request.Email, request.FirstName, request.LastNames, request.BirthDate);

            await _repository
            .DidNotReceive()
            .EmailExistAsync(request.Email, Arg.Any <CancellationToken>());

            _mapper
            .DidNotReceive()
            .Map(Arg.Any <Domain.Common.User>());

            await _store
            .DidNotReceive()
            .SaveAsync(Arg.Any <IUserAggregationRoot>(), Arg.Any <CancellationToken>());
        }
Пример #2
0
        public async ValueTask <Result> ExecuteAsync(UserAdd operation, CancellationToken cancellation = default)
        {
            var scope = _logger.BeginScope("Creating user.");

            try
            {
                var root = _store.Create();

                if (root.Create(operation.Email, operation.FirstName,
                                operation.LastNames, operation.BirthDate) is ErrorResult error)
                {
                    _logger.LogInformation("Error [ErrorCode: {errorCode}]", error.ErrorCode);
                    return(error);
                }

                if (await _repository.EmailExistAsync(operation.Email, cancellation))
                {
                    _logger.LogInformation("Email already exist");
                    return(DomainError.UserError.EmailAlreadyExist);
                }

                await _store.SaveAsync(root, cancellation);

                _logger.LogInformation("User created: [UserId: {userId}]", root.State.Id);
                return(Result.Ok(_mapper.Map((Domain.Common.User)root.State)));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Exception: ");
                return(Result.Fail(e));
            }
            finally
            {
                scope.Dispose();
            }
        }