[Nestjs][TIL][개인과제] Nestjs + TypeORM | 공연 예매 사이트 만들기 - 좌석 예매 가능 정보 반환하기

Trippy·2024년 1월 3일
1

Nest.js

목록 보기
9/15

공연 예매 좌석 확인 api

예매 등록까지 완료 한 상태에서 예매 가능한 좌석들을 GET할 api를 구현하려고 한다.
seatNumgrade price를 배열에 담아서 보여주는 형식으로 만들어주기 위해서 미리 메모장에 손 코딩을 해 본다.

a공연A스케줄을 예매 하고자 할 때는 scheduleId를 이용하여 해당 스케줄을 찾은 다음, 스케줄에 해당하는 예매되어 있는 좌석들을 불러와 예매가 되었는지 비교해주는 방식으로 예매 가능한 좌석을 만들어주는 로직을 구현하고자 생각했었다.

//performance.service.ts

// 해당 공연 특정 시간대 좌석 조회
async findOneSeats(performanceId: number, user: any, scheduleId: any) {
	// performance 테이블에서 공연의 가격을 가져오기 위함.
    const performance = await this.performanceRepository
      .createQueryBuilder('performance')
      .where({ performanceId })
      .getOne();
	
  	// schedule 테이블에서 각 등급별 제한 인원수를 가져오기 위함.
    const limits = await this.scheduleRepository.findOne({
      where: { scheduleId },
      select: ['standardLimit', 'royalLimit', 'vipLimit'],
    });

  	// seat 테이블에서 예매 된 좌석들을 불러오기 위함.
    const existSeats = await this.seatRepository.find({
      where: { schedules: scheduleId },
    });

다음으로는 예매된 좌석의 grade에 맞게 나눠서 빈 배열에 담아두도록 한다.

	// 예매된 좌석 배열 초기화
    let bookedStandardSeatArr: Array<number> = [];
    let bookedRoyalSeatArr: Array<number> = [];
    let bookedVipSeatArr: Array<number> = [];

    // 좌석db에 존재하는 seatNum을 각 grade에 맞게 새로운 배열로 반환
    for (const existseat of existSeats) {
      if (existseat.grade === 'STANDARD') {
        bookedStandardSeatArr.push(existseat.seatNum);
      } else if (existseat.grade === 'ROYAL') {
        bookedRoyalSeatArr.push(existseat.seatNum);
      } else if (existseat.grade === 'VIP') {
        bookedVipSeatArr.push(existseat.seatNum);
      }
    }
// 예매 가능한 좌석 배열 초기화
    let possibleStandard: Array<any> = [];
    let possibleRoyal: Array<any> = [];
    let possibleVip: Array<any> = [];

    // 예매 가능 좌석 생성 class
    class SeatObject {
      grade: string;
      seatNum: number;
      price: number;

      constructor(grade: string, seatNum: number, price: number) {
        this.grade = grade;
        this.seatNum = seatNum;
        this.price = price;
      }
    }
	// STANDARD 예매 가능 좌석 생성
    for (let i = 1; i <= limits.standardLimit; i++) {
      const standard = new SeatObject('STANDARD', i, performance.price);
      possibleStandard.push(standard);
    }
    const filteredStandardSeats = possibleStandard.filter(
      (seat) => !bookedStandardSeatArr.includes(seat.seatNum),
    );
    // ROYAL 예매 가능 좌석 생성
    for (let i = 1; i <= limits.royalLimit; i++) {
      const royal = new SeatObject('ROYAL', i, performance.price * 1.5);
      possibleRoyal.push(royal);
    }
    const filteredRoyalSeats = possibleRoyal.filter(
      (seat) => !bookedRoyalSeatArr.includes(seat.seatNum),
    );
    // VIP 예매 가능 좌석 생성
    for (let i = 1; i <= limits.vipLimit; i++) {
      const vip = new SeatObject('VIP', i, performance.price * 1.75);
      possibleVip.push(vip);
    }
    const filteredVipSeats = possibleVip.filter(
      (seat) => !bookedVipSeatArr.includes(seat.seatNum),
    );

결과

profile
감금 당하고 개발만 하고 싶어요

0개의 댓글