Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/src/question-list/dto/create-question-list.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface CreateQuestionListDto {
title: string;
contents: string[];
categoryNames: string[];
isPublic: boolean;
userId: number;
Expand Down
8 changes: 0 additions & 8 deletions backend/src/question-list/dto/question-list.dto.ts

This file was deleted.

5 changes: 0 additions & 5 deletions backend/src/question-list/dto/question.dto.ts

This file was deleted.

15 changes: 2 additions & 13 deletions backend/src/question-list/question-list.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,18 @@ export class QuestionListController {
// 질문지 DTO 준비
const createQuestionListDto: CreateQuestionListDto = {
title,
contents,
categoryNames,
isPublic,
userId: token.userId,
};

// 질문지 생성
const createdQuestionList =
const { createdQuestionList, createdQuestions } =
await this.questionListService.createQuestionList(
createQuestionListDto
);

// 질문 DTO 준비
const createQuestionDto: CreateQuestionDto = {
contents,
questionListId: createdQuestionList.id,
};

// 질문 생성
const createdQuestions =
await this.questionListService.createQuestions(
createQuestionDto
);

return res.send({
success: true,
message: "Question list created successfully.",
Expand Down
1 change: 1 addition & 0 deletions backend/src/question-list/question-list.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import { QuestionListRepository } from "./question-list.repository";
@Module({
controllers: [QuestionListController],
providers: [QuestionListService, QuestionListRepository],
exports: [QuestionListRepository],
})
export class QuestionListModule {}
14 changes: 1 addition & 13 deletions backend/src/question-list/question-list.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { Injectable } from "@nestjs/common";
import { DataSource, In } from "typeorm";
import { QuestionList } from "./question-list.entity";
import { Question } from "./question.entity";
import { QuestionDto } from "./dto/question.dto";
import { Category } from "./category.entity";
import { QuestionListDto } from "./dto/question-list.dto";
import { User } from "../user/user.entity";

@Injectable()
Expand Down Expand Up @@ -49,16 +47,6 @@ export class QuestionListRepository {
: [];
}

createQuestionList(questionListDto: QuestionListDto) {
return this.dataSource
.getRepository(QuestionList)
.save(questionListDto);
}

async createQuestions(questionDtos: QuestionDto[]) {
return this.dataSource.getRepository(Question).save(questionDtos);
}

async findCategoriesByNames(categoryNames: string[]) {
return this.dataSource.getRepository(Category).find({
where: {
Expand All @@ -75,7 +63,7 @@ export class QuestionListRepository {

getContentsByQuestionListId(questionListId: number) {
return this.dataSource.getRepository(Question).find({
where: { questionListId: questionListId },
where: { questionListId },
});
}

Expand Down
79 changes: 49 additions & 30 deletions backend/src/question-list/question-list.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Injectable } from "@nestjs/common";
import { QuestionListRepository } from "./question-list.repository";
import { CreateQuestionListDto } from "./dto/create-question-list.dto";
import { CreateQuestionDto } from "./dto/create-question.dto";
import { QuestionDto } from "./dto/question.dto";
import { QuestionListDto } from "./dto/question-list.dto";
import { GetAllQuestionListDto } from "./dto/get-all-question-list.dto";
import { QuestionListContentsDto } from "./dto/question-list-contents.dto";
import { MyQuestionListDto } from "./dto/my-question-list.dto";
import { DataSource } from "typeorm";
import { QuestionList } from "./question-list.entity";
import { Question } from "./question.entity";

@Injectable()
export class QuestionListService {
constructor(
private readonly dataSource: DataSource,
private readonly questionListRepository: QuestionListRepository
) {}

Expand Down Expand Up @@ -85,40 +86,45 @@ export class QuestionListService {

// 질문 생성 메서드
async createQuestionList(createQuestionListDto: CreateQuestionListDto) {
const { title, categoryNames, isPublic, userId } =
const { title, contents, categoryNames, isPublic, userId } =
createQuestionListDto;
const categories =
await this.questionListRepository.findCategoriesByNames(
categoryNames
);

if (categories.length !== categoryNames.length) {
throw new Error("Some category names were not found.");
}
const categories = await this.findCategoriesByNames(categoryNames);

const questionListDto: QuestionListDto = {
title,
categories,
isPublic,
userId,
};
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.startTransaction();

return this.questionListRepository.createQuestionList(questionListDto);
}
try {
const questionListDto = new QuestionList();
questionListDto.title = title;
questionListDto.categories = categories;
questionListDto.isPublic = isPublic;
questionListDto.userId = userId;

async createQuestions(createQuestionDto: CreateQuestionDto) {
const { contents, questionListId } = createQuestionDto;
const questionDtos = contents.map((content, index) => {
const question: QuestionDto = {
content,
index,
questionListId,
};
const createdQuestionList =
await queryRunner.manager.save(questionListDto);

const questions = contents.map((content, index) => {
const question = new Question();
question.content = content;
question.index = index;
question.questionList = createdQuestionList;

return question;
});

return question;
});
const createdQuestions =
await queryRunner.manager.save(questions);

return await this.questionListRepository.createQuestions(questionDtos);
await queryRunner.commitTransaction();

return { createdQuestionList, createdQuestions };
} catch (error) {
await queryRunner.rollbackTransaction();
throw new Error(error.message);
} finally {
await queryRunner.release();
}
}

async getQuestionListContents(questionListId: number) {
Expand Down Expand Up @@ -182,4 +188,17 @@ export class QuestionListService {
}
return myQuestionLists;
}

async findCategoriesByNames(categoryNames: string[]) {
const categories =
await this.questionListRepository.findCategoriesByNames(
categoryNames
);

if (categories.length !== categoryNames.length) {
throw new Error("Some category names were not found.");
}

return categories;
}
}
2 changes: 1 addition & 1 deletion backend/src/question-list/question.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { QuestionList } from "./question-list.entity";

@Entity()
export class Question {
private static CONTENT_MAX_LEN = 20;
private static CONTENT_MAX_LEN = 200;

@PrimaryGeneratedColumn()
id: number;
Expand Down
1 change: 1 addition & 0 deletions backend/src/room/dto/create-room.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface CreateRoomDto {
nickname: string;
socketId: string;
maxParticipants?: number;
questionListId: number;
}
4 changes: 3 additions & 1 deletion backend/src/room/room.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ export class RoomGateway implements OnGatewayConnection, OnGatewayDisconnect {

@SubscribeMessage(EVENT_NAME.CREATE_ROOM)
async handleCreateRoom(client: Socket, data: any) {
const { title, nickname, status, maxParticipants } = data; // unknown 으로 받고, Dto와 Pipe로 검증받기
const { title, nickname, status, maxParticipants, questionListId } =
data; // unknown 으로 받고, Dto와 Pipe로 검증받기
try {
const roomData = await this.roomService.createRoom({
title,
status,
socketId: client.id,
nickname,
maxParticipants,
questionListId,
});

client.join(roomData.roomId);
Expand Down
1 change: 1 addition & 0 deletions backend/src/room/room.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Room {
maxParticipants: number;
createdAt: number;
host: string;
questionListId;
}

export interface MemberConnection {
Expand Down
2 changes: 2 additions & 0 deletions backend/src/room/room.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { RoomGateway } from "./room.gateway";
import { RedisService } from "../redis/redis.service";
import { RoomRepository } from "./room.repository";
import { RoomController } from "./room.controller";
import { QuestionListModule } from "../question-list/question-list.module";

@Module({
imports: [QuestionListModule],
providers: [RoomService, RoomGateway, RedisService, RoomRepository],
controllers: [RoomController],
})
Expand Down
4 changes: 3 additions & 1 deletion backend/src/room/room.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export class RoomRepository {
}

async createRoom(dto: CreateRoomDto) {
const { title, socketId, maxParticipants, status } = dto;
const { title, socketId, maxParticipants, status, questionListId } =
dto;
const roomId = generateRoomId();

await this.redisService.set(
Expand All @@ -128,6 +129,7 @@ export class RoomRepository {
host: socketId,
maxParticipants,
status,
questionListId,
} as Room,
6 * HOUR
);
Expand Down
1 change: 1 addition & 0 deletions backend/src/room/room.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe("RoomService", () => {
title: "Test Room",
socketId: "socket-123",
nickname: "User1",
questionListId: 1,
};
const mockRoomId = "room-123";

Expand Down
25 changes: 23 additions & 2 deletions backend/src/room/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Injectable } from "@nestjs/common";
import { RoomRepository } from "./room.repository";
import { CreateRoomDto } from "./dto/create-room.dto";
import { MemberConnection } from "./room.model";
import { QuestionListRepository } from "../question-list/question-list.repository";
import { InjectRepository } from "@nestjs/typeorm";
import { QuestionList } from "../question-list/question-list.entity";

/**
* 비즈니스 로직 처리를 좀 더 하게 하기 위한 클래스로 설정
Expand All @@ -12,7 +15,10 @@ import { MemberConnection } from "./room.model";
export class RoomService {
private static MAX_MEMBERS = 5;

constructor(private readonly roomRepository: RoomRepository) {}
constructor(
private readonly roomRepository: RoomRepository,
private readonly questionListRepository: QuestionListRepository
) {}

async getPublicRoom() {
const rooms = await this.roomRepository.getAllRoom();
Expand All @@ -28,15 +34,29 @@ export class RoomService {
}

async createRoom(dto: CreateRoomDto) {
const { title, status, maxParticipants, socketId, nickname } = dto;
const {
title,
status,
maxParticipants,
socketId,
nickname,
questionListId,
} = dto;
const roomId = await this.roomRepository.createRoom({
title,
status: status ?? "PUBLIC",
maxParticipants: maxParticipants ?? RoomService.MAX_MEMBERS,
socketId,
nickname: nickname ?? "Master",
questionListId,
});

await this.roomRepository.addUser(roomId, dto.socketId, dto.nickname);
const questionListContents =
await this.questionListRepository.getContentsByQuestionListId(
questionListId
);

return {
roomId,
roomMetadata: {
Expand All @@ -45,6 +65,7 @@ export class RoomService {
maxParticipants: maxParticipants ?? RoomService.MAX_MEMBERS,
host: socketId,
nickname: nickname ?? "Master",
questionListContents,
},
};
}
Expand Down