Skip to content

Commit

Permalink
Refactor: Entity 속성에 assertion 추가, createPost에 userId 형 변환 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JJieunn committed Oct 25, 2022
1 parent 9fa3e06 commit 047d919
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
14 changes: 7 additions & 7 deletions src/entities/post.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@ import { Users } from "./user.entity";
@Entity('posts')
export class Posts {
@PrimaryGeneratedColumn()
id?: number
id!: number

@Column("varchar", { length: 30 })
title?: string
title!: string

@Column("varchar", { length: 3000 })
content?: string
content!: string

@CreateDateColumn({
type: 'timestamp',
default: () => 'CURRENT_TIMESTAMP(6)',
})
created_at?: Date;
created_at!: Date;

@UpdateDateColumn({
type: 'timestamp',
default: () => 'CURRENT_TIMESTAMP(6)',
onUpdate: 'CURRENT_TIMESTAMP(6)',
})
updated_at?: Date;
updated_at!: Date;

@Column("int")
user_id?: number;
user_id!: number;

@ManyToOne(() => Users, (user) => user.posts )
@JoinColumn({ name: "user_id", referencedColumnName: 'id' })
user?: Users;
user!: Users;
}
12 changes: 6 additions & 6 deletions src/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ export class Users {
id?: number;

@Column("varchar", { length: 15 })
nickname?: string;
nickname!: string;

@Column("varchar", { length: 30 })
email?: string;
email!: string;

@Column("varchar", { length: 1500 })
password?: string;
password!: string;

@CreateDateColumn({
type: 'timestamp',
default: () => 'CURRENT_TIMESTAMP(6)',
})
created_at?: Date;
created_at!: Date;

@UpdateDateColumn({
type: 'timestamp',
default: () => 'CURRENT_TIMESTAMP(6)',
onUpdate: 'CURRENT_TIMESTAMP(6)',
})
updated_at?: Date;
updated_at!: Date;

@OneToMany(() => Posts, (post) => post.user)
posts?: Posts[];
posts!: Posts[];
}
3 changes: 2 additions & 1 deletion src/models/postsDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ const isPostExistByUserId = async (userId: string | string[] | undefined, postId


const createPost = async (userId: string | string[] | undefined, postData: CreatePostDTO): Promise<object> => {
const userNumId = Number(userId);
return await myDataSource
.createQueryBuilder()
.insert()
.into(Posts)
.values({
user_id: userId,
user_id: userNumId,
title: postData.title,
content: postData.content
})
Expand Down

0 comments on commit 047d919

Please sign in to comment.