Skip to content

Commit

Permalink
feat: init 쿼리 추가
Browse files Browse the repository at this point in the history
- quizzes service 첫 동작시 데이터 삽입
- info level 로그 추가

[#115]
  • Loading branch information
LuizyHub authored and flydog98 committed Nov 23, 2023
1 parent 705d14f commit 1f64c52
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/backend/src/quizzes/quizzes.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { Category } from './entity/category.entity';
import { ContainersModule } from '../containers/containers.module';
import { SessionModule } from '../session/session.module';
import { Keyword } from './entity/keyword.entity';

@Module({
imports: [
TypeOrmModule.forFeature([Quiz, Category]),
TypeOrmModule.forFeature([Quiz, Category, Keyword]),
ContainersModule,
SessionModule,
],
Expand Down
81 changes: 79 additions & 2 deletions packages/backend/src/quizzes/quizzes.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
import { Quiz } from './entity/quiz.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
Expand All @@ -7,6 +7,10 @@ import { CategoryQuizzesDto, QuizzesDto } from './dto/quizzes.dto';
import { Category } from './entity/category.entity';
import { ContainersService } from 'src/containers/containers.service';
import { CommandResponseDto } from './dto/command-response.dto';
import fs from 'fs';
import * as Papa from 'papaparse';
import { Keyword } from './entity/keyword.entity';
import { Logger } from 'winston';

@Injectable()
export class QuizzesService {
Expand All @@ -15,8 +19,81 @@ export class QuizzesService {
private quizRepository: Repository<Quiz>,
@InjectRepository(Category)
private categoryRepository: Repository<Category>,
@InjectRepository(Keyword)
private keywordRepository: Repository<Keyword>,
private containerService: ContainersService,
) {}
@Inject('winston') private readonly logger: Logger,
) {
this.initQiuzzes();
}

private async initQiuzzes() {
this.logger.log('info', `read git-challenge-quiz.csv`);
const quizData = await this.readCsvFile('git-challenge-quiz.csv');

// Category와 Keyword를 먼저 생성
const categories = {};
const keywords = {};

for (const data of quizData) {
// Category 처리
if (!categories[data.category]) {
let category = new Category();
category.name = data.category;
this.logger.log('info', `add ${category.name} to categories`);
category = await this.categoryRepository.save(category);
categories[data.category] = category;
}

// Keyword 처리
const keywordList = data.keyword.split(',');
for (const kw of keywordList) {
const trimmedKw = kw.trim();
if (!keywords[trimmedKw]) {
let keyword = new Keyword();
keyword.keyword = trimmedKw;
this.logger.log('info', `add ${keyword.keyword} to keywords`);
keyword = await this.keywordRepository.save(keyword);
keywords[trimmedKw] = keyword;
}
}
}

// Quiz 생성
for (const data of quizData) {
const quiz = new Quiz();
quiz.title = data.title;
quiz.description = data.description;
quiz.category = categories[data.category];
quiz.id = data.id;

const keywordList = data.keyword.split(',').map((kw) => kw.trim());
quiz.keywords = keywordList.map((kw) => keywords[kw]);

this.logger.log('info', `add ${quiz.title} to quizzes`);
await this.quizRepository.save(quiz);
}
}

private readCsvFile(filePath: string): Promise<any[]> {
return new Promise((resolve, reject) => {
const file = fs.createReadStream(filePath);
const results = [];

Papa.parse(file, {
header: true,
step: (row) => {
results.push(row.data);
},
complete: () => {
resolve(results);
},
error: (err) => {
reject(err);
},
});
});
}
async getQuizById(id: number): Promise<QuizDto> {
const quiz = await this.quizRepository.findOne({
where: { id },
Expand Down

0 comments on commit 1f64c52

Please sign in to comment.