[TIL] [내일배움캠프 - Nest.js 개인과제] Nest.js + TypeORM | 공연 예매 사이트 만들기 - ERD, Entity 설계

sooyoung choi·2023년 12월 28일
0

내일배움캠프

목록 보기
8/19
post-thumbnail

1. 관계 설정해주기

user - payment < 1:N >
user - point < 1:N >
user - seat < 1:N >
payment - seat < 1:N >
point - seat < 1:1 >
performance - seat < 1:N >

2. entity 파일 작성하기

결제 엔티티

// payment entity file

import { User } from 'src/user/entities/user.entity';
import { Performance } from 'src/performance/entities/performance.entity';
import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { Seat } from 'src/seat/entities/seat.entity';

@Entity({
  name: 'payment',
})
export class Payment {
  @PrimaryGeneratedColumn({ name: 'payment_id' })
  id: number;

  @ManyToOne(() => User, (user) => user.id)
  user: User;

  @Column({ type: 'int', nullable: false })
  user_id: number;

  @ManyToOne(() => Performance, (performance) => performance.id)
  performance: Performance;

  @Column({ type: 'int', nullable: false })
  performance_id: number;

  @Column({ type: 'boolean', nullable: false })
  status: string;

  @Column({ type: 'int', nullable: false })
  total_price: string;

  @CreateDateColumn()
  created_at: Date;

  @UpdateDateColumn()
  updated_at: Date;

  @DeleteDateColumn()
  deleted_at?: Date;

  @OneToMany(() => Seat, (seat) => seat.id)
  seats: Seat[];
}

공연 엔티티

import { Seat } from 'src/seat/entities/seat.entity';
import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { Category } from '../types/performanceCategory.type';

@Entity({
  name: 'performance',
})
export class Performance {
  @PrimaryGeneratedColumn({ name: 'performance_id' })
  id: number;

  @Column({ type: 'varchar', nullable: false })
  title: string;

  @Column({ type: 'varchar', nullable: false })
  content: string;

  @Column({ type: 'varchar', nullable: false })
  location: string;

  // 배열로 받아야하는디
  @Column({ type: 'json', nullable: false })
  schedule: string[];

  @Column({ type: 'varchar', nullable: false })
  image: string;

  @Column({ type: 'enum', enum: Category, default: Category.Musical })
  category: Category;

  @CreateDateColumn()
  created_at: Date;

  @UpdateDateColumn()
  updated_at: Date;

  @DeleteDateColumn()
  deleted_at?: Date;

  @OneToMany(() => Seat, (seat) => seat.id)
  seat: Seat[];
}

포인트 엔티티

import { User } from 'src/user/entities/user.entity';
import {
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';

@Entity({
  name: 'point',
})
export class Point {
  @PrimaryGeneratedColumn({ name: 'point_id' })
  id: number;

  @Column({ type: 'int', nullable: false })
  income: number;

  @Column({ type: 'int', nullable: false })
  outcome: number;

  @Column({ type: 'int', nullable: false, default: 1000000 })
  total: number;

  @ManyToOne(() => User, (user) => user.id)
  @JoinColumn({ name: 'user_id' })
  user: User;
}

사용자 엔티티

import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  Index,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { Role } from '../types/userRole.type';
import { Payment } from 'src/payment/entities/payment.entity';
import { Point } from 'src/point/entities/point.entity';
import { Seat } from 'src/seat/entities/seat.entity';

@Index('email', ['email'], { unique: true })
@Entity({
  name: 'user',
})
export class User {
  @PrimaryGeneratedColumn({ name: 'user_id' })
  id: number;

  @Column({ type: 'varchar', unique: true, nullable: false })
  email: string;

  @Column({ type: 'varchar', nullable: false })
  name: string;

  @Column({ type: 'varchar', select: false, nullable: false })
  password: string;

  @Column({ type: 'enum', enum: Role, default: Role.User })
  role: Role;

  @CreateDateColumn()
  created_at: Date;

  @UpdateDateColumn()
  updated_at: Date;

  @DeleteDateColumn()
  deleted_at?: Date;

  @OneToMany(() => Payment, (payment) => payment.user_id)
  payments: Payment[];

  @OneToMany(() => Point, (point) => point.user)
  points: Point[];

  @OneToMany(() => Seat, (seat) => seat.id)
  seats: Seat[];
}

좌석 엔티티

import { Payment } from 'src/payment/entities/payment.entity';
import { Performance } from 'src/performance/entities/performance.entity';
import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  ManyToOne,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';

@Entity({
  name: 'seat',
})
export class Seat {
  @PrimaryGeneratedColumn({ name: 'seat_id' })
  id: number;

  @Column({ type: 'int', nullable: false, unique: true })
  seat_num: number;

  @Column({ type: 'varchar', nullable: false })
  grade: string;

  @Column({ type: 'boolean', nullable: false })
  status: boolean;

  @Column({ type: 'int', nullable: false })
  price: number;

  @Column({ type: 'int', nullable: false })
  performance_id: number;

  @CreateDateColumn()
  created_at: Date;

  @UpdateDateColumn()
  updated_at: Date;

  @DeleteDateColumn()
  deleted_at?: Date;

  @ManyToOne(() => Performance, (performance) => performance.id)
  performance: Performance;

  @ManyToOne(() => Payment, (payment) => payment.id)
  payment: Payment;
}

아직은 완벽하지 않은 엔티티들이다. 공연 스케줄을 따로 빼야할 지 고민이 되지만 일단은 계속해서 프로젝트를 진행하고 있다.
관계설정에서 며칠을 잡아먹어서 의욕이 떨어졌지만, 튜터님한테 가서 여쭤보니 사용자는 공연을 사는 것이 아닌 좌석을 사는 것 이라고 하셨다. 여기서 생각 전환이 돼서 지체되고 있었던 erd 설계를 그나마 진행하였고, 그에따른 엔티티 설계도 끝마쳤다.
계속해서 수정할 예정.

0개의 댓글