From 62a0ac15845fe9857c76316a485b2fc77ab61623 Mon Sep 17 00:00:00 2001 From: Brian Faust Date: Fri, 29 Mar 2019 11:07:24 +0200 Subject: [PATCH 1/2] feat(core-database): add function to search a block by id or height --- .../core-api/v2/handlers/blocks.test.ts | 21 +++++++++++++++++++ .../core-api/src/versions/2/blocks/methods.ts | 2 +- .../blocks-business-repository.ts | 20 +++++++++++++----- .../blocks-business-repository.ts | 2 ++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/__tests__/integration/core-api/v2/handlers/blocks.test.ts b/__tests__/integration/core-api/v2/handlers/blocks.test.ts index 087b44a3f2..c27d955955 100644 --- a/__tests__/integration/core-api/v2/handlers/blocks.test.ts +++ b/__tests__/integration/core-api/v2/handlers/blocks.test.ts @@ -82,6 +82,27 @@ describe("API 2.0 - Blocks", () => { ); }); + describe("GET /blocks/:height", () => { + describe.each([["API-Version", "request"], ["Accept", "requestWithAcceptHeader"]])( + "using the %s header", + (header, request) => { + it("should GET a block by the given height", async () => { + const response = await utils[request]("GET", `blocks/${genesisBlock.height}`); + + expect(response).toBeSuccessfulResponse(); + expect(response.data.data).toBeObject(); + + const block = response.data.data; + utils.expectBlock(block, { + id: genesisBlock.id, + height: genesisBlock.height, + transactions: genesisBlock.numberOfTransactions, + }); + }); + }, + ); + }); + describe("GET /blocks/:id/transactions", () => { describe.each([["API-Version", "request"], ["Accept", "requestWithAcceptHeader"]])( 'using the "%s" header', diff --git a/packages/core-api/src/versions/2/blocks/methods.ts b/packages/core-api/src/versions/2/blocks/methods.ts index 19700d1cb9..cf5d092007 100644 --- a/packages/core-api/src/versions/2/blocks/methods.ts +++ b/packages/core-api/src/versions/2/blocks/methods.ts @@ -18,7 +18,7 @@ const index = async request => { }; const show = async request => { - const block = await blocksRepository.findById(request.params.id); + const block = await blocksRepository.findByIdOrHeight(request.params.id); if (!block) { return Boom.notFound("Block not found"); diff --git a/packages/core-database/src/repositories/blocks-business-repository.ts b/packages/core-database/src/repositories/blocks-business-repository.ts index 29f20d90c9..6b560c2197 100644 --- a/packages/core-database/src/repositories/blocks-business-repository.ts +++ b/packages/core-database/src/repositories/blocks-business-repository.ts @@ -10,24 +10,34 @@ export class BlocksBusinessRepository implements Database.IBlocksBusinessReposit } public async findAllByGenerator(generatorPublicKey: string, paginate: Database.SearchPaginate) { - return await this.findAll({ ...{ generatorPublicKey }, ...paginate }); + return this.findAll({ ...{ generatorPublicKey }, ...paginate }); } public async findLastByPublicKey(generatorPublicKey: string) { // we order by height,desc by default - return await this.findAll({ generatorPublicKey }); + return this.findAll({ generatorPublicKey }); } public async findByHeight(height: number) { - return await this.findAll({ ...{ height } }); + return this.findAll({ height }); } public async findById(id: string) { - return await this.databaseServiceProvider().connection.blocksRepository.findById(id); + return this.databaseServiceProvider().connection.blocksRepository.findById(id); + } + + public async findByIdOrHeight(idOrHeight) { + try { + const { rows } = await this.findByHeight(idOrHeight); + + return rows[0]; + } catch (error) { + return this.findById(idOrHeight); + } } public async search(params: Database.IParameters) { - return await this.databaseServiceProvider().connection.blocksRepository.search(this.parseSearchParams(params)); + return this.databaseServiceProvider().connection.blocksRepository.search(this.parseSearchParams(params)); } private parseSearchParams(params: Database.IParameters): Database.SearchParameters { diff --git a/packages/core-interfaces/src/core-database/business-repository/blocks-business-repository.ts b/packages/core-interfaces/src/core-database/business-repository/blocks-business-repository.ts index 9fca9787a3..45442bf81c 100644 --- a/packages/core-interfaces/src/core-database/business-repository/blocks-business-repository.ts +++ b/packages/core-interfaces/src/core-database/business-repository/blocks-business-repository.ts @@ -10,6 +10,8 @@ export interface IBlocksBusinessRepository { findByHeight(height: number): Promise; + findByIdOrHeight(idOrHeight): Promise; + findAllByGenerator(generatorPublicKey: string, paginate: SearchPaginate); findLastByPublicKey(generatorPublicKey: string): Promise; From 4cde3f0083e73c16e6cc80f6b848e857d0caf9c7 Mon Sep 17 00:00:00 2001 From: supaiku <1311798+supaiku0@users.noreply.github.com> Date: Fri, 29 Mar 2019 14:36:21 +0200 Subject: [PATCH 2/2] Update packages/core-database/src/repositories/blocks-business-repository.ts Co-Authored-By: faustbrian --- .../src/repositories/blocks-business-repository.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core-database/src/repositories/blocks-business-repository.ts b/packages/core-database/src/repositories/blocks-business-repository.ts index 6b560c2197..11898c836f 100644 --- a/packages/core-database/src/repositories/blocks-business-repository.ts +++ b/packages/core-database/src/repositories/blocks-business-repository.ts @@ -10,7 +10,7 @@ export class BlocksBusinessRepository implements Database.IBlocksBusinessReposit } public async findAllByGenerator(generatorPublicKey: string, paginate: Database.SearchPaginate) { - return this.findAll({ ...{ generatorPublicKey }, ...paginate }); + return this.findAll({ generatorPublicKey, ...paginate }); } public async findLastByPublicKey(generatorPublicKey: string) {