Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core-database): add function to search a block by id or height #2337

Merged
merged 2 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions __tests__/integration/core-api/v2/handlers/blocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion packages/core-api/src/versions/2/blocks/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface IBlocksBusinessRepository {

findByHeight(height: number): Promise<any>;

findByIdOrHeight(idOrHeight): Promise<any>;

findAllByGenerator(generatorPublicKey: string, paginate: SearchPaginate);

findLastByPublicKey(generatorPublicKey: string): Promise<any>;
Expand Down