Skip to content

Commit

Permalink
fix: implemented default return of the latest block for /block endpoi…
Browse files Browse the repository at this point in the history
…nt. (#248)

* fix: implemented default return of the latest block for /block endpoint. This will return the latest block if no block identifier was provided.

* fix: implemented default return of the latest block for /block endpoint. This will return the latest block if no block identifier was provided.

* chore: added test
  • Loading branch information
Kammerlo authored Oct 30, 2024
1 parent 79812f0 commit b67e354
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ public class LedgerBlockServiceImpl implements LedgerBlockService {
@Override
public Optional<Block> findBlock(Long blockNumber, String blockHash) {
log.debug("Query blockNumber: {} , blockHash: {}", blockNumber, blockHash);
if (blockHash == null && blockNumber != null) {
return blockRepository.findByNumber(blockNumber).map(this::toModelFrom);
} else if (blockHash != null && blockNumber == null) {
if (blockHash != null && blockNumber == null) {
return blockRepository.findByHash(blockHash).map(this::toModelFrom);
} else {
} else if (blockHash == null && blockNumber != null) {
return blockRepository.findByNumber(blockNumber).map(this::toModelFrom);
} else if (blockHash != null && blockNumber != null) {
return blockRepository
.findByNumberAndHash(blockNumber, blockHash)
.map(this::toModelFrom);
} else {
return blockRepository.findLatestBlock().map(this::toModelFrom);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ void findBlockIdentifier_Test_OK_null_blk() {
assertBlockIdentifierAndTx(block, tx);
}

@Test
void findBlock_Test_OK_null() {
//given
//when
Optional<Block> block = ledgerBlockService.findBlock(null, null);
//then
assertThat(block).isPresent();
}

@Test
void findBlock_Test_OK_empty() {
//given
Expand Down

0 comments on commit b67e354

Please sign in to comment.