Documentation endpoint user
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
import { NestFactory } from '@nestjs/core'
|
||||
import { AppModule } from './app.module'
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
|
||||
import { ValidationPipe } from '@nestjs/common'
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule)
|
||||
app.useGlobalPipes(new ValidationPipe())
|
||||
const config = new DocumentBuilder()
|
||||
.setTitle('Traintrape-moi')
|
||||
.setDescription('API permettant de stocker les données de Traintrape-moi')
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { User } from "@prisma/client";
|
||||
import { ApiProperty } from "@nestjs/swagger"
|
||||
import { User } from "@prisma/client"
|
||||
|
||||
export class UserEntity implements User {
|
||||
@ApiProperty({description: "Identifiant unique"})
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Controller, Get, Param } from '@nestjs/common'
|
||||
import { Controller, Get, NotFoundException, Param, ParseIntPipe } from '@nestjs/common'
|
||||
import { UsersService } from './users.service'
|
||||
import { ApiOkResponse } from '@nestjs/swagger'
|
||||
import { ApiNotFoundResponse, ApiOkResponse } from '@nestjs/swagger'
|
||||
import { UserEntity } from './entities/user.entity'
|
||||
|
||||
@Controller('users')
|
||||
@ -9,13 +9,17 @@ export class UsersController {
|
||||
|
||||
@Get()
|
||||
@ApiOkResponse({ type: UserEntity, isArray: true })
|
||||
findAll() {
|
||||
async findAll() {
|
||||
return this.usersService.findAll()
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOkResponse({ type: UserEntity })
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.usersService.findOne(+id)
|
||||
@ApiNotFoundResponse({ description: "Utilisateur⋅rice non trouvé⋅e" })
|
||||
async findOne(@Param('id', ParseIntPipe) id: number) {
|
||||
const user = await this.usersService.findOne(id)
|
||||
if (!user)
|
||||
throw new NotFoundException(`L'utilisateur⋅rice avec l'identifiant ${id} n'existe pas`)
|
||||
return user
|
||||
}
|
||||
}
|
||||
|
@ -5,11 +5,11 @@ import { PrismaService } from 'src/prisma/prisma.service'
|
||||
export class UsersService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
findAll() {
|
||||
return this.prisma.user.findMany()
|
||||
async findAll() {
|
||||
return await this.prisma.user.findMany()
|
||||
}
|
||||
|
||||
findOne(id: number) {
|
||||
return this.prisma.user.findUnique({ where: { id } })
|
||||
async findOne(id: number) {
|
||||
return await this.prisma.user.findUnique({ where: { id } })
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user