TIL

좌석 만들기 준비 - Nest.js 사용 2차원 배열 사용 준비

용찬 2023. 8. 22. 19:50

스터디 룸에서 해당하는 룸에 Type = 모양을 enum 타입으로 생성을 한 후
좌석을 만들때 그 룸에 해당하는 Type을 가져와서 해당하는 모양의 로직을 조건문으로 실행시켜 줄 예정.
좌석을 생성 해 줄 때 두 개의 수 (row, column)을 받는다.

정해진 타입의 값에 두개의 수 를 넣어 준 후 2차원 배열의 형태로 좌석의 구조를 생성.
만들어진 좌석에 Kind = 좌석의 종류 ( 1인석, 다인석, 회의실 및 좌석이 아님)를 추가해 주는 로직으로
스터디 룸 안의 좌석들의 형태와 각각의 좌석의 타입들을 기입 해 줄 예정.

로직을 구현함에 앞서 Entity파일과 dto파일 및 구조 세팅 中
컨트롤러 계층에서 Seat값에 해당하는 row와 column의 값을 가져오지 못함.
이 부분을 세팅 해주는 데에만 꼬박 하루를 보낸 것 같다..
Entity파일과 dto  파일에서 최종적으로 명시해 준 방법


import { IsNotEmpty, IsInt, Min } from 'class-validator';

export class SeatDto {
  @IsInt()
  @Min(0) // 음수로는 좌석을 생성 할 수 없음
  readonly low: number;

  @IsInt()
  @Min(0) // 음수로는 좌석을 생성 할 수 없음
  readonly column: number;
}

// low, column 이라는 값을 숫자 타입으로 명시 => SeatDto로 export

 

import {
    Column,
    Entity,
    PrimaryGeneratedColumn,
    OneToMany,
    OneToOne,
    Index,
    ManyToOne,
    CreateDateColumn,
    UpdateDateColumn,
    DeleteDateColumn
  
  } from 'typeorm';
  import { SeatDto } from '../dto/seat/seat-dto'
// SeatDto로 export 해온 걸 import 해와서
import { Room } from './room.entity'
export enum kindEnum {
  일인용 = 1,
  미팅룸 = 2,
  회의실 = 3,
  없음 = 4
}
  @Entity({ schema: 'apple', name: 'seat' })
export class Seat {
  @PrimaryGeneratedColumn()
  seatId: number;

  @ManyToOne(()=>Room, room=>room.seats)
  rooms:Room

  @Column('varchar')
  price: number;

  @Column({ type: 'enum', enum: kindEnum, default: kindEnum.일인용 })
  type: kindEnum;

  @Column({ type: 'json' })
  seat: SeatDto[]
// 위 코드에서 사용

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  @DeleteDateColumn()
  deletedAt: Date | null;
}



import {  IsNotEmpty, IsEnum, IsArray, ArrayMinSize, ArrayMaxSize, ValidateNested } from 'class-validator';
import { kindEnum } from 'src/entity/seat.entity';
import { Type } from 'class-transformer';
import { SeatDto } from './seat-dto'


export class createSeatDto {

  @IsEnum(kindEnum)
  @IsNotEmpty()
  readonly kind: kindEnum;

  @IsNotEmpty()
  readonly price: number;


  @IsArray()
  @ArrayMinSize(1, { message: '적어도 1개의 좌석이 필요합니다.' })
// 좌석은 음수 및 0의 값이 들어 갈 수 없어서 최소 값 1개로 명시
  @ArrayMaxSize(100, { message: '좌석은 최대 100개까지 가능합니다.' })
// 좌석의 최대 개수는 100개로 제한
  @ValidateNested({ each: true })
  @Type(() => SeatDto)
  readonly seat: SeatDto[];
}


위 코드에서 나와 있듯이 Seat.dto => entity.seat => create.seat.dto 순 대로 명시 해준 후 값을 가져옴.

import {
  Controller,
  Post,
  Get,
  Put,
  Delete,
  Body,
  Req,
  Res,
  Patch,
  Param,
} from '@nestjs/common';
import { SeatService } from './seat.service';
import { createSeatDto } from '../dto/seat/create-seat-dto';
import { updateSeatDto } from '../dto/seat/update-seat-dto';

@Controller('seat')
export class SeatController {
  constructor(private readonly seatService: SeatService) {}

  @Post('/roomId')
  async createSeat(
    @Param('roomId') roomId: number,
    @Body() data: createSeatDto, // 마지막으로 정해준 createSeatDto를 data로 명시.
  ) {
    return this.seatService.createSeat(roomId, data);
}

이러한 방식으로 마지막으로 create.seat.dto에 명시된 값들을 가져옴
아직은 Nest가 익숙하지도 않고 2차원 배열을 생성하는 개념을 잡는 것이 너무나도 어렵다.

현재 진행 상황 = 로직 작성을 위한 entity, dto 구조 완성

date. 2023-08-22