This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add pipes for optional int, order by
- Loading branch information
1 parent
d6beaf8
commit b107497
Showing
4 changed files
with
72 additions
and
8 deletions.
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 |
---|---|---|
@@ -1,19 +1,27 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { Controller, Get, Param, ParseIntPipe, Query } from '@nestjs/common'; | ||
import { users } from '@prisma/client'; | ||
import { OmitSecrets } from 'src/modules/prisma/prisma.interface'; | ||
import { OptionalIntPipe } from 'src/pipes/optional-int.pipe'; | ||
import { OrderByPipe } from 'src/pipes/order-by.pipe'; | ||
import { UsersService } from './user.service'; | ||
|
||
@Controller('users') | ||
export class UserController { | ||
constructor(private usersService: UsersService) {} | ||
|
||
@Get() | ||
async getAll(): Promise<OmitSecrets<users>[]> { | ||
return this.usersService.users({}); | ||
async getAll( | ||
@Query('skip', OptionalIntPipe) skip?: number, | ||
@Query('take', OptionalIntPipe) take?: number, | ||
@Query('orderBy', OrderByPipe) orderBy?: Record<string, 'asc' | 'desc'>, | ||
): Promise<OmitSecrets<users>[]> { | ||
return this.usersService.users({ skip, take, orderBy }); | ||
} | ||
|
||
// @Get('post/:id') | ||
// async getPostById(@Param('id') id: string): Promise<PostModel> { | ||
// return this.postService.post({ id: Number(id) }); | ||
// } | ||
@Get(':id') | ||
async get( | ||
@Param('id', ParseIntPipe) id: number, | ||
): Promise<OmitSecrets<users>> { | ||
return this.usersService.user({ id: Number(id) }); | ||
} | ||
} |
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,22 @@ | ||
import { | ||
PipeTransform, | ||
Injectable, | ||
HttpException, | ||
HttpStatus, | ||
ArgumentMetadata, | ||
} from '@nestjs/common'; | ||
|
||
/** Convert a string like "1" to a number, but without NaN */ | ||
@Injectable() | ||
export class OptionalIntPipe implements PipeTransform { | ||
transform(value: string, metadata: ArgumentMetadata): number | undefined { | ||
if (value == null) return undefined; | ||
const num = Number(value); | ||
if (isNaN(num)) | ||
throw new HttpException( | ||
`"${metadata.data}" should be a number, provided "${value}"`, | ||
HttpStatus.UNPROCESSABLE_ENTITY, | ||
); | ||
return num; | ||
} | ||
} |
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,33 @@ | ||
import { | ||
PipeTransform, | ||
Injectable, | ||
HttpException, | ||
HttpStatus, | ||
ArgumentMetadata, | ||
} from '@nestjs/common'; | ||
|
||
/** Convert a string like "name asc, address desc" to { name: "asc", address: "desc" } */ | ||
@Injectable() | ||
export class OrderByPipe implements PipeTransform { | ||
transform( | ||
value: string, | ||
metadata: ArgumentMetadata, | ||
): Record<string, 'asc' | 'desc'> | undefined { | ||
if (value == null) return undefined; | ||
try { | ||
const rules = value.split(',').map(val => val.trim()); | ||
const orderBy: Record<string, 'asc' | 'desc'> = {}; | ||
rules.forEach(rule => { | ||
const [key, order] = rule.split(' '); | ||
if (!['asc', 'desc'].includes(order)) throw new Error(); | ||
rules[key] = order; | ||
}); | ||
return orderBy; | ||
} catch (_) { | ||
throw new HttpException( | ||
`"${metadata.data}" should be like "key1 asc, key2 desc", provided "${value}"`, | ||
HttpStatus.UNPROCESSABLE_ENTITY, | ||
); | ||
} | ||
} | ||
} |