Skip to content

Commit

Permalink
simplify, clean-up logic
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-aurele-besner committed Jan 6, 2025
1 parent 4d0e981 commit f482945
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 81 deletions.
4 changes: 1 addition & 3 deletions indexers/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import { ApiDailyUsage } from './entities/users/api-daily-usage.entity';
import { ApiKey } from './entities/users/api-key.entity';
import { ApiMonthlyUsage } from './entities/users/api-monthly-usage.entity';
import { Profile } from './entities/users/profile.entity';
import { BlocksService } from './services/blocks.service';
import { ExtrinsicsService } from './services/extrinsics.service';

@Module({
imports: [
Expand Down Expand Up @@ -59,6 +57,6 @@ import { ExtrinsicsService } from './services/extrinsics.service';
ExtrinsicsController,
AccountsController,
],
providers: [AppService, BlocksService, ExtrinsicsService, ApiKeyStrategy],
providers: [AppService, ApiKeyStrategy],
})
export class AppModule {}
2 changes: 1 addition & 1 deletion indexers/api/src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
return 'Hello Autonomys World!';
}
}
16 changes: 0 additions & 16 deletions indexers/api/src/config/typeorm.config.ts

This file was deleted.

4 changes: 2 additions & 2 deletions indexers/api/src/controllers/accounts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ export class AccountsController {
@ApiResponse({
status: 200,
description: 'Account details retrieved successfully',
type: Accounts,
})
@ApiResponse({ status: 404, description: 'Account not found' })
async getAccountById(@Param('accountId') accountId: string) {
const account = await this.accountRepository.findOne({
where: { id: accountId },
});

if (!account) {
if (!account)
throw new NotFoundException(`Account with ID ${accountId} not found`);
}

return account;
}
Expand Down
20 changes: 14 additions & 6 deletions indexers/api/src/controllers/blocks.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Controller, Get, NotFoundException, Param } from '@nestjs/common';
import { ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Blocks } from '../entities/consensus/blocks.entity';
import { BlocksService } from '../services/blocks.service';

@ApiTags('Blocks')
@Controller('blocks')
export class BlocksController {
constructor(private readonly blocksService: BlocksService) {}
constructor(
@InjectRepository(Blocks)
private blocksRepository: Repository<Blocks>,
) {}

@Get('latest')
@ApiOperation({
Expand All @@ -20,7 +24,10 @@ export class BlocksController {
})
@ApiResponse({ status: 404, description: 'No blocks found' })
async getLatestBlock(): Promise<Blocks> {
return this.blocksService.findLatest();
return this.blocksRepository
.createQueryBuilder('blocks')
.orderBy('blocks.height', 'DESC')
.getOne();
}

@Get(':height')
Expand All @@ -36,11 +43,12 @@ export class BlocksController {
})
@ApiResponse({ status: 404, description: 'Block not found' })
async getBlockByHeight(@Param('height') height: string) {
const block = await this.blocksService.findByHeight(Number(height));
const block = await this.blocksRepository.findOne({
where: { height: Number(height) },
});

if (!block) {
if (!block)
throw new NotFoundException(`Block with height ${height} not found`);
}

return block;
}
Expand Down
25 changes: 21 additions & 4 deletions indexers/api/src/controllers/extrinsics.controller.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import { Controller, Get, Param, UseGuards } from '@nestjs/common';
import {
Controller,
Get,
NotFoundException,
Param,
UseGuards,
} from '@nestjs/common';
import {
ApiOperation,
ApiResponse,
ApiSecurity,
ApiTags,
} from '@nestjs/swagger';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ApiKeyGuard } from '../auth/api-key.guard';
import { Extrinsics } from '../entities/consensus/extrinsics.entity';
import { ExtrinsicsService } from '../services/extrinsics.service';

@ApiTags('Extrinsics')
@Controller('extrinsics')
@UseGuards(ApiKeyGuard)
@ApiSecurity('X-API-KEY')
export class ExtrinsicsController {
constructor(private readonly extrinsicsService: ExtrinsicsService) {}
constructor(
@InjectRepository(Extrinsics)
private extrinsicsRepository: Repository<Extrinsics>,
) {}

@Get(':hash')
@ApiOperation({ summary: 'Get extrinsic by hash' })
Expand All @@ -25,6 +35,13 @@ export class ExtrinsicsController {
})
@ApiResponse({ status: 404, description: 'Extrinsic not found' })
async findByHash(@Param('hash') hash: string): Promise<Extrinsics> {
return this.extrinsicsService.findByHash(hash);
const extrinsic = await this.extrinsicsRepository.findOne({
where: { hash },
});

if (!extrinsic)
throw new NotFoundException(`Extrinsic with hash ${hash} not found`);

return extrinsic;
}
}
25 changes: 0 additions & 25 deletions indexers/api/src/services/blocks.service.ts

This file was deleted.

24 changes: 0 additions & 24 deletions indexers/api/src/services/extrinsics.service.ts

This file was deleted.

0 comments on commit f482945

Please sign in to comment.