boilerplate-nestjs-api-crud/src/users/entities/user.entity.ts
2023-04-17 10:20:39 +08:00

55 lines
1.1 KiB
TypeScript

import { ApiProperty } from '@nestjs/swagger';
import { CreateUserDto } from '../dto/create-user.dto';
import { UpdateUserDto } from '../dto/update-user.dto';
// See: https://cimpleo.com/blog/nestjs-modules-and-swagger-practices/
// export interface IUser {
// id: number;
// name: string;
// }
// export class User implements IUser {
export class User {
@ApiProperty({
type: 'number',
name: 'id',
description: 'id of user',
required: false,
})
id: number;
@ApiProperty({
type: 'string',
name: 'name',
description: 'name of user',
required: false,
})
name: string;
protected constructor() {
this.id = -1;
this.name = '';
}
// Factory methods
public static create(id = 0, name = ''): User {
const user = new User();
user.id = id;
user.name = name;
return user;
}
public static createFromCreateUserDto(
userDto: CreateUserDto | UpdateUserDto,
id?: number,
): User {
const user = new User();
if (id) {
user.id = id;
}
user.name = userDto.name;
return user;
}
}