-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from klayrHQ/search-api
search module
- Loading branch information
Showing
11 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,4 @@ pids | |
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json | ||
|
||
prisma/db | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ApiResponseOptions } from '@nestjs/swagger'; | ||
|
||
export class SearchValidators { | ||
name: string; | ||
address: string; | ||
publicKey: string; | ||
rank: number; | ||
} | ||
|
||
export class SearchBlocks { | ||
height: number; | ||
id: string; | ||
} | ||
|
||
export class SearchTransactions { | ||
id: string; | ||
sender: string; | ||
} | ||
|
||
export class GetSearchResponse { | ||
validators: SearchValidators[]; | ||
blocks: SearchBlocks[]; | ||
transactions: SearchTransactions[]; | ||
} | ||
|
||
export const getSearchResponse: ApiResponseOptions = { | ||
status: 200, | ||
description: 'The search results have been successfully fetched.', | ||
type: GetSearchResponse, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { IsString } from 'class-validator'; | ||
|
||
export class SearchQueryDto { | ||
/** | ||
* The search query to search in blocks, transactions and validators | ||
*/ | ||
@IsString() | ||
search: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Controller, Query, Get, ValidationPipe, UsePipes } from '@nestjs/common'; | ||
import { BlockRepoService } from 'src/block/block-repo.service'; | ||
import { TransactionRepoService } from 'src/transaction/transaction-repo.service'; | ||
import { ValidatorRepoService } from 'src/validator/validator.repo-service'; | ||
import { SearchQueryDto } from './dto/get-search.dto'; | ||
import { ApiResponse } from '@nestjs/swagger'; | ||
import { GetSearchResponse, getSearchResponse } from './dto/get-search-res.dto'; | ||
import { getKlayr32AddressFromAddress } from 'src/utils/helpers'; | ||
import { HEX_ADDRESS_LENGTH } from 'src/utils/constants'; | ||
|
||
@Controller('search') | ||
export class SearchController { | ||
constructor( | ||
private readonly blockRepoService: BlockRepoService, | ||
private readonly validatorRepoService: ValidatorRepoService, | ||
private readonly transactionRepoService: TransactionRepoService, | ||
) {} | ||
|
||
@Get() | ||
@UsePipes(new ValidationPipe({ transform: true, whitelist: true, forbidNonWhitelisted: true })) | ||
@ApiResponse(getSearchResponse) | ||
async getSearchResults(@Query() query: SearchQueryDto): Promise<GetSearchResponse> { | ||
let { search } = query; | ||
|
||
if (search.length === HEX_ADDRESS_LENGTH) { | ||
search = getKlayr32AddressFromAddress(search); | ||
} | ||
|
||
const [blocks, validators, transactions] = await Promise.all([ | ||
this.blockRepoService.searchBlocks(search), | ||
this.validatorRepoService.searchValidators(search), | ||
this.transactionRepoService.searchTransactions(search), | ||
]); | ||
|
||
const formattedBlocks = blocks.map((block) => ({ | ||
height: block.height, | ||
id: block.id, | ||
})); | ||
|
||
const formattedValidators = validators.map((validator) => ({ | ||
name: validator.account.name, | ||
address: validator.address, | ||
publicKey: validator.account.publicKey, | ||
rank: validator.rank, | ||
})); | ||
|
||
const formattedTransactions = transactions.map((transaction) => ({ | ||
id: transaction.id, | ||
sender: transaction.sender.address, | ||
})); | ||
|
||
return { | ||
validators: formattedValidators, | ||
blocks: formattedBlocks, | ||
transactions: formattedTransactions, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { SearchController } from './search.controller'; | ||
import { BlockModule } from 'src/block/block.module'; | ||
import { ValidatorModule } from 'src/validator/validator.module'; | ||
import { TransactionModule } from 'src/transaction/transaction.module'; | ||
|
||
@Module({ | ||
imports: [BlockModule, ValidatorModule, TransactionModule], | ||
controllers: [SearchController], | ||
}) | ||
export class SearchModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters