Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1f0f1e2
Merge pull request #109 from boostcampwm-2024/dev
ShipFriend0516 Nov 12, 2024
2574086
Merge pull request #123 from boostcampwm-2024/dev
ShipFriend0516 Nov 15, 2024
65e9b80
chore: `docker-compose.yml` 추가
blu3fishez Nov 21, 2024
d66d7bb
feat: 방 생성할 때 question contents도 함께 보내주기
twalla26 Nov 21, 2024
f4eb2f9
chore: `.gitignore` 파일 수정
blu3fishez Nov 21, 2024
67e03b1
fix: 질문 contents 길이 20 -> 200 으로 늘리기
twalla26 Nov 21, 2024
7ddea28
fix: 열려있는 세션이 없을 경우에 대한 예외처리
blu3fishez Nov 21, 2024
db51171
fix: 스터디세션 레포지토리 버그 수정
blu3fishez Nov 21, 2024
fb54ef0
feat: 질문과 질문지 생성 기능을 트랜젝션으로 묶기
twalla26 Nov 21, 2024
ee8b85e
feat: 세션 리스트 GET API 데이터 수정
blu3fishez Nov 21, 2024
8c1bbb6
Merge pull request #190 from boostcampwm-2024/chore/docker-compose
blu3fishez Nov 21, 2024
f4353ba
chore: 필요없는 코드 삭제
twalla26 Nov 21, 2024
c2d3916
Merge pull request #195 from boostcampwm-2024/fix/study-session
ShipFriend0516 Nov 21, 2024
716ee13
Merge pull request #196 from twalla26/feature/question-list
ShipFriend0516 Nov 21, 2024
6325019
Merge pull request #198 from boostcampwm-2024/dev-fe
ShipFriend0516 Nov 21, 2024
e8d6381
feat: 카테고리 시드 데이터 추가
twalla26 Nov 21, 2024
763cb87
Merge pull request #200 from twalla26/feature/question-list
twalla26 Nov 21, 2024
4e98435
Merge pull request #199 from boostcampwm-2024/dev
ShipFriend0516 Nov 21, 2024
e02d134
refactor: useSession에서 미디어스트림 정리, 피어 커넥션 정리 분리
yiseungyun Nov 23, 2024
06413bc
refactor: useReaction 훅 분리 및 sessionId 없으면 early return하도록 수정
yiseungyun Nov 23, 2024
21f6c11
style: 코드 format 변경
yiseungyun Nov 23, 2024
a2c8e66
refactor: 리액션 유지 시간 매직넘버로 변경
yiseungyun Nov 23, 2024
eb4576e
refactor: useSession에서 소켓 이벤트 코드 분리
yiseungyun Nov 25, 2024
836cb24
refactor: useSession의 소켓, 닉네임 검사 부분 함수 분리
yiseungyun Nov 25, 2024
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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ node_modules

# husky 관련
.husky/_
.husky/.gitignore
.husky/.gitignore

# docker-compose 관련 파일

mysql-data
redis-data
34 changes: 34 additions & 0 deletions backend/src/config/typeorm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,40 @@ export const createDataSource = async (): Promise<DataSource> => {
transactionalDataSource = addTransactionalDataSource(
new DataSource(typeOrmConfig)
);

await transactionalDataSource.initialize();
await seedDatabase(transactionalDataSource);
}
return transactionalDataSource;
};

const seedDatabase = async (dataSource: DataSource) => {
const categoryRepository = dataSource.getRepository(Category);

const categories = [
"자료구조",
"운영체제",
"데이터베이스",
"컴퓨터구조",
"네트워크",
"백엔드",
"프론트엔드",
"알고리즘",
"보안",
];

// 이미 데이터가 있을 경우 시딩하지 않음
const existingCount = await categoryRepository.count();
if (existingCount > 0) {
return;
}

// 카테고리 데이터 삽입
const categoryEntities = categories.map((name) => {
const category = new Category();
category.name = name;
return category;
});

await categoryRepository.save(categoryEntities);
};
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
9 changes: 4 additions & 5 deletions backend/src/room/room.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ export class RoomRepository {

async getAllRoom(): Promise<Record<string, Room>> {
const redisMap = await this.redisService.getMap("room:*");
console.log(redisMap);

if (!redisMap) return {};

return Object.entries(redisMap).reduce(
return Object.entries(redisMap ?? {}).reduce(
(acc, [roomId, room]) => {
acc[roomId.split(":")[1]] = room as Room;
return acc;
Expand Down Expand Up @@ -117,7 +114,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 +126,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
Loading