public async Task SaveAsync(IUserAggregationRoot aggregate, CancellationToken cancellation = default) { foreach (var phone in _phones) { await _repository.RemoveAsync(phone, cancellation); } foreach (var address in _addresses) { await _repository.RemoveAsync(address, cancellation); } await _repository.SaveAsync((Common.User) aggregate.State, cancellation) .ConfigureAwait(false); }
public async ValueTask <Result> ExecuteAsync(AddressAdd operation, CancellationToken cancellation = default) { var scope = _logger.BeginScope("Get Address. [UserId: {0}]", operation.UserId); try { IUserAggregationRoot 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: {0}].", 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(); } }