πŸ“¦ cityzenKIM / toy_project_board

πŸ“„ Posts.ts Β· 89 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { Users } from './Users';
import { Comments } from './Comments';
import { IsNotEmpty, IsString } from 'class-validator';
import { PostViews } from './PostViews';
import { ApiProperty } from '@nestjs/swagger';

export enum PostCategory {
  NOTICE = 'NOTICE',
  QA = 'QA',
  ONE_ON_ONE = 'ONE_ON_ONE',
}

@Index(['deletedAt'])
@Entity({ schema: 'shop', name: 'post' })
export class Posts {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id' })
  id: number;

  @ApiProperty({
    example: 'κΈ€ 제λͺ©μž…λ‹ˆλ‹€.',
    required: true,
  })
  @IsString()
  @IsNotEmpty()
  @Column('varchar', { name: 'title', length: 30 })
  title: string;

  @ApiProperty({
    example: 'κΈ€ λ‚΄μš©μž…λ‹ˆλ‹€.',
    required: true,
  })
  @IsString()
  @IsNotEmpty()
  @Column('text', { name: 'content' })
  content: string;

  @IsString()
  @Column('varchar', { name: 'imgUrl', nullable: true })
  imgUrl: string;

  @ApiProperty({
    example: ' NOTICE or QA or ONE_ON_ONE',
    required: true,
  })
  @Column({
    type: 'enum',
    enum: PostCategory,
    default: PostCategory.NOTICE,
  })
  category: PostCategory;

  @Column('int', { name: 'views', default: 1 })
  views: number;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  @DeleteDateColumn({ nullable: true })
  deletedAt?: Date;

  @OneToMany(() => Comments, (comments) => comments.post)
  comments: Comments[];

  @OneToMany(() => PostViews, (postViews) => postViews.post)
  postViews: PostViews[];

  @ManyToOne(() => Users, (users) => users.Posts, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinColumn([{ name: 'userId', referencedColumnName: 'id' }])
  user: Users;
}