일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- TypeORMconfig
- 좌석만들기
- cachememory
- 좌석내가격
- devcamp
- Live server
- 캐시메모리저장후DB저장
- Til
- Nest
- get
- nestwinston
- 카카오페이테스트결제
- 캐시메모리DB저장
- 테스트결제
- joi vs classvalidation
- 포트원
- NEST좌석
- NEST좌석생성
- Nest.js
- TypeORMconfig.ts
- 네스트로거
- 로그파일저장
- 내일배움캠프
- Class Validation
- javascript
- TYPESCRIPT좌석생성
- ormconfig.ts
- 자바스크립트
- CRUD
- 좌석생성
- Today
- Total
배씨의 개발일지
좌석만들기 시작 - 2차원 배열 반환 값 받아오기 본문
좌석 생성 시
{
"kind": 1, // 좌석의 종류
"price": 100, // 좌석의 종류
"row": 10,
"column": 10
}
이런 식으로 JSON @BODY 형태로 요청을 주고
row와 column에 해당하는 값들을 받아와 room에서 받아온 형태를 기반으로 실행 하는 로직 작성
async createSeat(roomId: number, data: createSeatDto) {
const roomType = await this.roomRepository.findOne({
where: { roomId, deletedAt: null },
select: ['type'],
});
if (!roomType || !roomType.type) {
throw new NotFoundException('Room type not found');
}
let seats: Seat[][];
if (roomType.type === 1) { // 룸 타입의 값이 1일 때 실행되는 로직은 원을 생성하는 로직
seats = await this.createCircle(data.row, data.column);
} else if (roomType.type === 2) { // 룸 타입의 값이 2일 때 실행되는 로직은 반원을 생성하는 로직
seats = await this.createEllipse(data.row, data.column);
} else if (roomType.type === 3) { // 룸 타입의 값이 3일 때 실행되는 로직은 사각형을 생성하는 로직
seats = await this.createSquare(data.row, data.column);
} else {
throw new Error('Unsupported room type');
}
return seats;
}
룸의 타입이 2 일 때 실행되는 로직 ( 원 모양의 좌석 배열을 만들어주는 로직 )
private async createCircle(rows: number, columns: number): Promise<Seat[][]> {
const seats: Seat[][] = [];
// 중심 좌표 계산
const centerX = Math.floor(columns / 2);
const centerY = Math.floor(rows / 2);
// 반지름 계산 (원의 지름은 좌석 행 또는 열 중 작은 값의 절반)
const radius = Math.min(columns, rows) / 2;
// 원형 좌석 생성 로직
for (let y = 0; y < rows; y++) {
const row: Seat[] = [];
for (let x = 0; x < columns; x++) {
// 현재 좌표와 중심 간의 거리 계산
const distance = Math.sqrt((x - centerX) ** 2 + (y - centerY) ** 2);
// 거리가 반지름 이내인 경우 원 안에 있는 좌석으로 표시
const isInsideCircle = distance <= radius;
row.push([x, y, isInsideCircle]); // 좌석 정보를 배열에 추가
}
seats.push(row);
}
console.log(seats)
return seats;
}
이렇게 해서 row, column = 5 인 원을 생성하는 요청 썬더클라이언트로 보낼 시
[ [ [ 0, 0, false ], [ 1, 0, false ], [ 2, 0, false ], [ 3, 0, true ], [ 4, 0, false ], [ 5, 0, false ] ], [ [ 0, 1, false ], [ 1, 1, true ], [ 2, 1, true ], [ 3, 1, true ], [ 4, 1, true ], [ 5, 1, true ] ], [ [ 0, 2, false ], [ 1, 2, true ], [ 2, 2, true ], [ 3, 2, true ], [ 4, 2, true ], [ 5, 2, true ] ], [ [ 0, 3, true ], [ 1, 3, true ], [ 2, 3, true ], [ 3, 3, true ], [ 4, 3, true ], [ 5, 3, true ] ], [ [ 0, 4, false ], [ 1, 4, true ], [ 2, 4, true ], [ 3, 4, true ], [ 4, 4, true ], [ 5, 4, true ] ], [ [ 0, 5, false ], [ 1, 5, true ], [ 2, 5, true ], [ 3, 5, true ], [ 4, 5, true ], [ 5, 5, true ] ] ]
이런식으로 값을 반환 하는데
여기서 false인 값을 0, true인 값을 1로 바꾸면
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0]
이러한 모양의 5x5의 배열이 만들어진다.
앞으로 남은 부분은
0인 부분은 존재하지 않는 좌석.
1인 부분은 존재하는 좌석.
0인 부분은 예약을 할 수 없고 1인 부분만 예약이 가능하게끔 로직을 만들 예정이다.
'TIL' 카테고리의 다른 글
NEST+TYPEORM 에서 트랜잭션 사용 (0) | 2023.08.28 |
---|---|
좌석 만들기 - Mysql DB에 2차원 배열 저장하기 (0) | 2023.08.24 |
좌석 만들기 준비 - Nest.js 사용 2차원 배열 사용 준비 (0) | 2023.08.22 |
NEST.JS JWT와 GUARD 사용하기 (0) | 2023.08.21 |
Primary key, Foreign Key (0) | 2023.08.21 |