示例#1
0
        public async Task Execute_Should_ReturnUserNotFound()
        {
            var address = _fixture.Create <AddressAdd>();

            _store.GetAsync(address.UserId, Arg.Any <CancellationToken>())
            .Returns(Task.FromResult((IUserAggregationRoot)null));

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

            result.IsSuccess.Should().BeFalse();
            result.Value.Should().BeNull();
            result.ErrorCode.Should().NotBeNullOrEmpty();
            result.Description.Should().NotBeNullOrEmpty();
            result.Should().Be(DomainError.UserError.UserNotFound);

            await _store
            .Received(1)
            .GetAsync(address.UserId, Arg.Any <CancellationToken>());

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

            await _store
            .DidNotReceive()
            .SaveAsync(Arg.Any <IUserAggregationRoot>(), Arg.Any <CancellationToken>());
        }
示例#2
0
        public async ValueTask <Result> ExecuteAsync(UserUpdate operation, CancellationToken cancellation = default)
        {
            var scope = _logger.BeginScope("Updating user. [UserId: {userId}]", operation.Id);

            try
            {
                var root = await _store.GetAsync(operation.Id, cancellation);

                if (root == null)
                {
                    _logger.LogInformation("User not found");
                    return(DomainError.UserError.UserNotFound);
                }

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

                await _store.SaveAsync(root, cancellation);

                _logger.LogInformation("User updated with success");
                return(Result.Ok(_mapper.Map((Domain.Common.User)root.State)));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Exception: ", operation.Id);
                return(Result.Fail(e));
            }
            finally
            {
                scope.Dispose();
            }
        }
示例#3
0
        public async ValueTask <Result> ExecuteAsync(AddressGet operation, CancellationToken cancellation = default)
        {
            var scope = _logger.BeginScope("Get Address. [UserId: {userId}]", operation.UserId);

            try
            {
                var root = await _store.GetAsync(operation.UserId, cancellation);

                if (root == null)
                {
                    _logger.LogInformation("User not found");
                    return(DomainError.UserError.UserNotFound);
                }

                _logger.LogInformation("Address get with success");
                return(Result.Ok(root.State.Addresses.Select(x => _mapper.Map(x))));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Exception: ");
                return(Result.Fail(e));
            }
            finally
            {
                scope.Dispose();
            }
        }
示例#4
0
        public async ValueTask <Result> ExecuteAsync(AddressRemove operation, CancellationToken cancellation = default)
        {
            var scope = _logger.BeginScope("Remove Address. [UserId: {0}]", operation.UserId);

            try
            {
                var root = await _store.GetAsync(operation.UserId, cancellation);

                if (root == null)
                {
                    _logger.LogInformation("User not found");
                    return(DomainError.UserError.UserNotFound);
                }

                if (root.RemoveAddress(operation.Id) is ErrorResult error)
                {
                    _logger.LogInformation("Error [ErrorCode: {0}]", error.ErrorCode);
                    return(error);
                }

                await _store.SaveAsync(root, cancellation);

                _logger.LogInformation("Address removed with success");
                return(Result.Ok());
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Exception: ");
                return(Result.Fail(e));
            }
            finally
            {
                scope.Dispose();
            }
        }
示例#5
0
        public async ValueTask <Result> ExecuteAsync(AddressAdd operation, CancellationToken cancellation = default)
        {
            var scope = _logger.BeginScope("Get Address. [UserId: {userId}]", operation.UserId);

            try
            {
                var root = await _store.GetAsync(operation.UserId, cancellation);

                if (root == null)
                {
                    _logger.LogInformation("User not found");
                    return(DomainError.UserError.UserNotFound);
                }

                if (root.AddAddress(operation.Line, operation.Number, operation.PostCode) is ErrorResult error)
                {
                    _logger.LogInformation("Error. [ErrorCode: {errorCode}].", error.ErrorCode);
                    return(error);
                }

                await _store.SaveAsync(root, cancellation);

                _logger.LogInformation("Address added with success");
                var address = root.State.Addresses.Last();

                return(Result.Ok(_mapper.Map(address)));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Exception: ");
                return(Result.Fail(e));
            }
            finally
            {
                scope.Dispose();
            }
        }