I use for this project nest and the nestjs/crud library. Unfortunately I can't override the createOneBase function so that it returns a person response I looked for a solution in the last posts of the same topic. I would like to change the answer when I create a user.
what I have:
{
"id": 12,
"username": "test",
"password": "tests",
"email": "test@foo.bar"
}
what I expect:
{
"id": 12,
"username": "test",
"password": "tests",
"email": "test@foo.bar",
"token": "TOKEN"
}
my controller:
import { Controller } from '@nestjs/common';
import { UsersService } from './users.service';
import { Crud, Override, ParsedRequest, ParsedBody, CrudRequest, CrudController } from '@nestjsx/crud';
import { User, UserDTO } from './user';
@Crud({
model: {
type: User,
},
})
@Controller('users')
export class UsersController {
constructor(public service: UsersService) {}
get base(): CrudController<User | UserDTO> {
return this;
}
@Override()
createOne(
@ParsedRequest() req: CrudRequest,
@ParsedBody() dto: User,
): Promise<User | UserDTO> {
const userDto: UserDTO = {
id: dto.id,
username: dto.username,
password: dto.password,
email: dto.email,
token: 'TOKEN',
};
return this.base.createOneBase(req, userDto);
}
}
my entities:
import { IsDefined, IsString, MinLength } from 'class-validator';
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
@IsDefined({ always: true })
@IsString({ always: true })
@MinLength(1, { always: true })
username: string;
@Column()
@IsDefined({ always: true })
@IsString({ always: true })
@MinLength(5, { always: true })
password: string;
@Column()
@IsDefined({ always: true })
@IsString({ always: true })
@MinLength(1, { always: true })
email: string;
}
export class UserDTO {
id: number;
username: string;
password: string;
email: string;
token: string;
access_token?: string;
}
so I don't understand what's wrong with it, thanks for your help
To override a Nestjs/Crud method use the decorator @Override(functionName)
Nestjs/Crud Doc
@Override('createOneBase')
createOne(
@ParsedRequest() req: CrudRequest,
@ParsedBody() dto: Hero)
{
return this.base.createOneBase(req, dto);
}