import { BadRequestException , Body , Controller , Get , Param , ParseIntPipe , Post , Query , Req , Res , UseGuards } from '@nestjs/common' ; import type { Request , Response } from 'express' ; import { Roles , RolesGuard , Logger , ConfigService } from '../../common' ; import type { Sampletable1 } from '../../entity/sampledb1' ; import { FoobarService } from '../../shared/foobar' ; import { SampleDto } from '../dto' ; import { DatabaseService } from '../providers' ; /**
 * route /test/sample/*
 */ @ UseGuards ( RolesGuard ) @ Controller ( 'sample' ) export class SampleController { constructor ( private readonly logger , private config , private dbquery , private foobarService ) { this . logger . setContext ( SampleController . name ) ; } @ Get ( ) public sample ( ) { this . logger . log ( 'this is sample' ) ; return { hello : this . config . get ( 'hello' ) , foo : this . config . get ( 'foo' ) , } ; } @ Get ( 'hello' ) public hello ( @ Req ( ) req , @ Res ( ) res ) { res . json ( { message : req . originalUrl , } ) ; } @ Get ( 'hello/query' ) public helloQuery ( @ Query ( 'name' ) name ) { if ( ! name ) { throw new BadRequestException ( 'InvalidParameter' ) ; } return `helloQuery: ${ name }` ; } @ Get ( 'hello/param/:name' ) public helloParam ( @ Param ( 'name' ) name ) { return `helloParam: ${ name }` ; } @ Get ( 'hello/number/:foo' ) public helloNumber ( @ Param ( 'foo' ) foo , @ Query ( 'bar' ) bar , @ Query ( 'blah' , ParseIntPipe ) blah ) { return { foo , bar , blah } ; } @ Post ( 'hello/body' ) public helloBody ( @ Body ( ) param ) { return `helloBody: ${ JSON . stringify ( param ) }` ; } @ Get ( 'database' ) public async database ( ) { return this . dbquery . sample1 ( ) ; } @ Get ( 'foobars' ) public async foobars ( ) { return this . foobarService . getFoobars ( ) ; } @ Roles ( 'admin' ) @ Get ( 'admin' ) public admin ( ) { return 'Need admin role' ; } }