Skip to content

Commit

Permalink
Merge pull request #10 from mju-likelion/feature/room-create-method
Browse files Browse the repository at this point in the history
방 생성 로직 수정
  • Loading branch information
ChoiWonYu authored Aug 7, 2023
2 parents 66231bd + 300cb30 commit 8fcb0f8
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Warnings:
- The `dates` column on the `Room` table would be dropped and recreated. This will lead to data loss if there is data in the column.
*/
-- AlterTable
ALTER TABLE "Room" DROP COLUMN "dates",
ADD COLUMN "dates" TEXT[];
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ datasource db {
// 정의 완료 후 'yarn prisma migrate dev' 명령 입력하셔서 migrate 진행하시면 됩니다.
model Room {
code String @id
dates String
dates String[]
dateOnly Boolean
startTime String?
endTime String?
Expand Down
6 changes: 4 additions & 2 deletions src/api/rooms/dto/create-room.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { IsBoolean, IsOptional, Matches } from 'class-validator';

export class CreateRoomDto {
@Matches(/^(\d{4}-\d{2}-\d{2},?)+$/)
dates: string;
@Matches(/^(\d{4}-\d{2}-\d{2})$/, {
each: true,
})
dates: string[];

@IsOptional()
@IsBoolean()
Expand Down
89 changes: 55 additions & 34 deletions src/api/rooms/rooms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,55 @@ export class RoomsService {
* @see {@link https://www.notion.so/likelion-11th/6-6-8fdfd4c7268e4f70bd232dcee5078aab?pvs=4#ca12b4cd60904410bbb83549e748f1cd | Notion}
*/
async create(createRoomDto: CreateRoomDto): Promise<Room> {
this.validateDates(createRoomDto);

return await this.prismaService.room.create({
data: {
code: nanoid(),
...createRoomDto,
dateOnly: createRoomDto.dateOnly || false,
},
});
}

async findOne(code: string): Promise<Room> {
const room = this.prismaService.room.findUnique({
where: { code },
});

if (!room) {
throw new NotFoundException(`Room with code ${code} not found`);
}

return room;
}

getRoomSummary(code: string) {
return `This action returns a ${code} room summary`;
}

convertStringtoDate(stringDate: string): Date {
return new Date(stringDate);
}

validateDates(createRoomDto: CreateRoomDto) {
const errors = [];

const dates = createRoomDto.dates.split(',');
const dates = createRoomDto.dates;
const firstDate = this.convertStringtoDate(dates[0]);
const now = new Date();
const utc = now.getTime() + now.getTimezoneOffset() * 60 * 1000;
const koreaTimeDiff = 9 * 60 * 60 * 1000;
const nowKoreanDate = new Date(utc + koreaTimeDiff);

const maxDate = new Date(
nowKoreanDate.setMonth(nowKoreanDate.getMonth() + 6)
);
const maxDateString = `${maxDate.getFullYear()}-${(
'0' +
(maxDate.getMonth() + 1)
).slice(-2)}-${('0' + maxDate.getDate()).slice(-2)}`;

if (dates.length < 1 || dates.length > 60) {
errors.push('dates must be between 1 and 60');
}
Expand All @@ -38,15 +84,14 @@ export class RoomsService {
errors.push('dates must be sorted');
}

const today = new Date(
new Date().toLocaleString('ko-KR', { timeZone: 'Asia/Seoul' })
);
const lastDate = new Date(today.setMonth(today.getMonth() + 6));
const lastDateString = `${lastDate.getFullYear()}-${(
'0' +
(lastDate.getMonth() + 1)
).slice(-2)}-${('0' + lastDate.getDate()).slice(-2)}`;
if (sortedDates.at(-1) > lastDateString) {
if (
firstDate.getMonth() < nowKoreanDate.getMonth() ||
firstDate.getDate() < nowKoreanDate.getDate()
) {
errors.push('first date must be today no matter how early it is.');
}

if (sortedDates.at(-1) > maxDateString) {
errors.push('dates must be within 6 months');
}

Expand Down Expand Up @@ -77,29 +122,5 @@ export class RoomsService {
if (errors.length > 0) {
throw new BadRequestException(errors);
}

return this.prismaService.room.create({
data: {
...createRoomDto,
code: nanoid(),
dateOnly: createRoomDto.dateOnly || false,
},
});
}

async findOne(code: string): Promise<Room> {
const room = this.prismaService.room.findUnique({
where: { code },
});

if (!room) {
throw new NotFoundException(`Room with code ${code} not found`);
}

return room;
}

getRoomSummary(code: string) {
return `This action returns a ${code} room summary`;
}
}

0 comments on commit 8fcb0f8

Please sign in to comment.