Skip to content

Commit

Permalink
Merge pull request #22 from TEAM-ITERVIEW/#21
Browse files Browse the repository at this point in the history
[feat] 면접 옵션 선택 후 시작 API
  • Loading branch information
cha2y0ung authored Mar 27, 2024
2 parents 305fa60 + e20651b commit aad96bf
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 5 deletions.
9 changes: 5 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ model Subject {
}

model User {
id Int @id @default(autoincrement())
userName String
pictureURL String?
themeColor String? @db.Char(1)
id Int @id @default(autoincrement())
userName String
pictureURL String?
themeColor String? @db.Char(1)
refreshToken String?
}

model interviewQuestion {
Expand Down
1 change: 1 addition & 0 deletions src/controller/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as interviewController } from './interviewController';
24 changes: 24 additions & 0 deletions src/controller/interviewController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { startInterviewDTO } from '../interface/DTO';
import { Request, Response, NextFunction } from 'express';
import { message, statusCode } from '../module/constant';
import { success } from '../module/constant/utils';
import { interviewService } from '../service';

const startInterview = async (req: Request, res: Response, next: NextFunction) => {
const startInterviewDTO: startInterviewDTO = req.body;
const refreshToken = req.body;

try {
const data = await interviewService.startInterview(startInterviewDTO, refreshToken);

return res
.status(statusCode.CREATED)
.send(success(statusCode.CREATED, message.START_INTERVIEW_SUCCESS, data));
} catch (error) {
next(error);
}
};

export default {
startInterview,
};
6 changes: 6 additions & 0 deletions src/interface/DTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface startInterviewDTO {
subjectText: string;
questionNum: number;
onlyVoice: boolean;
startDateTime: string;
}
4 changes: 3 additions & 1 deletion src/module/constant/responseMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ export default {
NULL_VALUE: "필요한 값이 없습니다.",
OUT_OF_VALUE: "파라미터 값이 잘못되었습니다.",
NOT_FOUND: "잘못된 경로입니다.",
BAD_REQUEST: "잘못된 요청입니다.",
BAD_REQUEST: "잘못된 요청입니다.",

// 서버 내 오류
INTERNAL_SERVER_ERROR: "서버 내 오류",

START_INTERVIEW_SUCCESS: "인터뷰 시작 성공",
};
3 changes: 3 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Router } from "express";
import interviewRouter from './interviewRouter';

const router: Router = Router();

router.use('/interview', interviewRouter);

export default router;
22 changes: 22 additions & 0 deletions src/router/interviewRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Router } from 'express';
import { body, header, param } from 'express-validator';
import { interviewController } from '../controller';
import errorValidator from '../middleware/error/errorValidator';

const router: Router = Router();

router.post(
'/',
[
body('refreshToken').notEmpty(),
body('subjectText').notEmpty(),
body('questionNum').notEmpty(),
body('onlyVoice').notEmpty(),
body('startDateTime').notEmpty(),
],
errorValidator,
interviewController.startInterview,
);


export default router;
1 change: 1 addition & 0 deletions src/service/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as interviewService } from './interviewService';
75 changes: 75 additions & 0 deletions src/service/interviewService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { startInterviewDTO } from '../interface/DTO';
import { PrismaClient } from '@prisma/client';
import errorGenerator from '../middleware/error/errorGenerator';
import { message, statusCode } from '../module/constant';
const prisma = new PrismaClient();

const startInterview = async (startInterviewDTO: startInterviewDTO, refreshToken: string) => {
try {
const findUserId = await prisma.User.find({
where: {
refreshToken: refreshToken,
},
select: {
id: true,
},
});

const findSubjectId = await prisma.Subject.find({
where: {
subjectText: startInterviewDTO.subjectText,
},
select: {
id: true,
},
});

const countInterviewToday = await prisma.Subject.findMany({
include: {
_count: {
select: {startDateTime: startInterviewDTO.startDateTime}
}
}
});

const questionList = await prisma.Question.findMany({
where: {
subjectId: findSubjectId.id
},
select: {
id: true,
questionText: true,
},
});

const selectedQuestions = [];
const numQuestions = Math.min(questionList.length, startInterviewDTO.questionNum);
const shuffledQuestions = questionList.sort(() => Math.random() - 0.5);
for (let i = 0; i < numQuestions; i++) {
selectedQuestions.push(shuffledQuestions[i]);
};

const interview = await prisma.Interview.create({
data: {
userId: findUserId.id,
subjectId: findSubjectId.id,
questionNum: startInterviewDTO.questionNum,
subjectText: startInterviewDTO.subjectText,
title: null,
onlyVoice: startInterviewDTO.onlyVoice,
startDateTime: startInterviewDTO.startDateTime,
questionList: selectedQuestions.map(question => ({
id: question.id,
questionText: question.questionText
})),
},
});
return interview;
} catch (error) {
throw error;
}
};

export default {
startInterview,
};

0 comments on commit aad96bf

Please sign in to comment.