Skip to content

Commit

Permalink
Merge pull request #2 from Horizontal-org/gus/add-validation-to-endpo…
Browse files Browse the repository at this point in the history
…ints

add validations to delete and list
  • Loading branch information
juandans01 authored Dec 15, 2022
2 parents 814a538 + 23083f3 commit 9b6b868
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
11 changes: 7 additions & 4 deletions src/modules/question/controllers/delete.question.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Controller, Delete, Get, Inject, Param, ParseArrayPipe, ParseIntPipe, Query } from '@nestjs/common';
import { Delete, Param, ParseIntPipe } from '@nestjs/common';
import { DeleteQuestionDto } from '../dto/delete.question.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { AuthController } from 'src/utils/decorators/auth-controller.decorator';
import { Repository } from 'typeorm';
Expand All @@ -12,9 +13,11 @@ export class DeleteQuestionController {
) {}

@Delete(':id')
async handler(@Param('id') id: string) {

await this.questionRepository.delete(id)
async handler(
@Param('id', ParseIntPipe)
id: DeleteQuestionDto,
) {
await this.questionRepository.delete(id);

return true;
}
Expand Down
31 changes: 19 additions & 12 deletions src/modules/question/controllers/list.question.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Inject, ParseArrayPipe, ParseIntPipe, Query } from '@nestjs/common';
import { Controller, Get, ParseArrayPipe, Query } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Question } from '../domain';
Expand All @@ -11,20 +11,27 @@ export class ListQuestionController {
) {}

@Get('')
async handler(@Query('apps', new ParseArrayPipe({ optional: true })) apps = []) {

async handler(
@Query(
'apps',
new ParseArrayPipe({ items: Number, separator: ',', optional: true }),
)
apps = [],
) {
const query = this.questionRepository
.createQueryBuilder('question')
.leftJoinAndSelect("question.apps", "apps")
.leftJoinAndSelect("question.explanations", "explanations")
.take(10)

// console.log("🚀 ~ file: list.question.controller.ts:23 ~ ListQuestionController ~ handler ~ apps", apps)
// if (apps.length > 0) {
// query.where('apps.id IN(:...ids)', {ids: apps})
// }
const response = await query.getMany()
.leftJoinAndSelect('question.apps', 'apps')
.leftJoinAndSelect('question.explanations', 'explanations')
.take(10);

console.log(
'🚀 ~ file: list.question.controller.ts:23 ~ ListQuestionController ~ handler ~ apps',
apps,
);
if (apps.length > 0) {
query.where('apps.id IN(:...ids)', { ids: apps });
}
const response = await query.getMany();

return response;
}
Expand Down
6 changes: 6 additions & 0 deletions src/modules/question/dto/delete.question.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IsNumberString } from 'class-validator';

export class DeleteQuestionDto {
@IsNumberString()
id: number;
}

0 comments on commit 9b6b868

Please sign in to comment.