Skip to content

Commit 6c44867

Browse files
committed
fix: add some checks on benchmark find one
1 parent a590bc9 commit 6c44867

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/benchmarks/benchmark.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export class BenchmarkController {
4242
@ApiOkResponse({ type: Benchmark, description: 'Requested benchmark' })
4343
@Get(':id')
4444
async getBenchmarkById(
45-
@Param() id: BenchmarkIdDto,
45+
@Param() benchmarkIdDto: BenchmarkIdDto,
4646
): Promise<Benchmark | undefined> {
47-
return this.benchmarkService.findOne(id);
47+
return this.benchmarkService.findOne(benchmarkIdDto.id);
4848
}
4949
}

src/benchmarks/benchmark.service.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { BadRequestException, Injectable } from '@nestjs/common';
22
import { Repository } from 'typeorm';
33
import { InjectRepository } from '@nestjs/typeorm';
4+
import { isUUID } from 'class-validator';
45
import { CreateBenchmarkDto } from './dto/create-benchmark.dto';
56
import { Benchmark } from './benchmark.entity';
67
import { User } from '../users/user.entity';
@@ -25,7 +26,16 @@ export class BenchmarkService {
2526
return this.benchmarkRepository.find({});
2627
}
2728

28-
async findOne(id: { id: string }): Promise<Benchmark | undefined> {
29-
return this.benchmarkRepository.findOne(id);
29+
async findOne(id: string): Promise<Benchmark | undefined> {
30+
if (!isUUID(id)) {
31+
throw new BadRequestException(`Invalid benchmark id: ${id}`);
32+
}
33+
const benchmark = await this.benchmarkRepository.findOne(id);
34+
35+
if (!benchmark) {
36+
throw new BadRequestException(`Could not find benchmark: ${id}`);
37+
}
38+
39+
return benchmark;
3040
}
3141
}

0 commit comments

Comments
 (0)