Skip to content

Commit

Permalink
Merge pull request #45 from TEAM-ITERVIEW/#44
Browse files Browse the repository at this point in the history
[feat] 학습 노트 조회 api
  • Loading branch information
cha2y0ung authored Apr 3, 2024
2 parents 11b19ff + 1445faf commit cd817a4
Show file tree
Hide file tree
Showing 14 changed files with 651 additions and 875 deletions.
439 changes: 426 additions & 13 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
},
"dependencies": {
"@prisma/client": "^5.11.0",
"axios": "^1.6.8",
"dotenv": "^16.4.5",
"express": "^4.19.1",
"environ": "^3.0.3",
"express": "^4.19.2",
"express-validator": "^7.0.1",
"openai": "^4.32.0",
"prisma": "^5.11.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ model Question {
subjectId Int @unique
questionText String?
sampleAnswer String?
pin Boolean
again Boolean
}

model Subject {
Expand All @@ -74,6 +76,4 @@ model interviewQuestion {
questionId Int @unique
userId Int @unique
subjectId Int @unique
pin Boolean?
again Boolean?
}
3 changes: 2 additions & 1 deletion src/controller/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as interviewController } from './interviewController';
export { default as userController } from './userController';
export { default as historyController } from './historyController';
export { default as questionController } from './questionController';
export { default as questionController } from './questionController';
export { default as studyNoteController } from './studyNoteController';
21 changes: 21 additions & 0 deletions src/controller/studyNoteController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Request, Response, NextFunction } from 'express';
import { message, statusCode } from '../module/constant';
import { success } from '../module/constant/utils';
import { studyNoteService } from '../service';

const getStudyNote = async (req: Request, res: Response, next: NextFunction) => {
const refreshToken = req.body;
const {sortNum} = req.params;
try {
const data = await studyNoteService.getStudyNote(refreshToken, +sortNum);
return res
.status(statusCode.CREATED)
.send(success(statusCode.CREATED, message.GET_STUDYNOTE_SUCCESS, data));
} catch (error) {
next(error);
}
};

export default {
getStudyNote,
};
42 changes: 40 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
import express, { NextFunction, Request, Response } from 'express';
//import { Configuration, OpenAIApi } from 'openai';
import Configuration, { OpenAI } from 'openai';
import OpenAIApi from 'openai';
import config from './config';
import router from './router';
import * as dotenv from 'dotenv'

dotenv.config()
const axios = require('axios')
const app = express(); // express 객체 받아옴

app.use(express.json());

app.use(express.urlencoded({ extended: true }));
app.use('/api', router); // use -> 모든 요청
app.use('/api', router);

const openai = new OpenAI({
organization: "org-5IqT7dxuJeAfuyJEJJhg8VXq",
apiKey: process.env.OPEN_API_KEY
});

export const scoreAnswer = async (apitext: string) => {
try {
const completion = await openai.completions.create({
model: "gpt-3.5-turbo-instruct",
prompt: apitext,
max_tokens: 200
});
const result = completion.choices[0].text;
return result;
} catch(error) {
throw error;
}
};

app.post('/api/chat', async (req, res) => {
try {
const { message } = req.body
const completion = await openai.completions.create({
model: "gpt-3.5-turbo-instruct",
prompt: message,
max_tokens:20
});
res.json({ response: completion.choices[0].text })
} catch (error) {
res.json({error})
}
})

//Error Handler
interface ErrorType {
Expand Down
1 change: 1 addition & 0 deletions src/module/constant/responseMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export default {
DELETE_INTERVIEW_SUCCESS: "면접 기록 삭제 성공",
ADD_PIN_SUCCESS: "질문 스크랩 성공",
DELETE_PIN_SUCCESS: "질문 스크랩 취소 성공",
GET_STUDYNOTE_SUCCESS: "학습 노트 조회 성공",
};
2 changes: 2 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import interviewRouter from './interviewRouter';
import userRouter from "./userRouter";
import historyRouter from "./historyRouter";
import questionRouter from "./questionRouter";
import studyNoteRouter from "./studyNoteRouter";

const router: Router = Router();

router.use('/interview', interviewRouter);
router.use('/accounts', userRouter);
router.use('/history', historyRouter);
router.use('/question', questionRouter);
router.use('/studyNote', studyNoteRouter);

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

const router: Router = Router();

router.get(
'/:sortNum',
[
param('sortNum').notEmpty(),
body('refreshToken').notEmpty(),
],
errorValidator,
studyNoteController.getStudyNote,
);

export default router;
3 changes: 2 additions & 1 deletion src/service/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as interviewService } from './interviewService';
export { default as userService } from './userService';
export { default as historyService } from './historyService';
export { default as questionService } from './questionService';
export { default as questionService } from './questionService';
export { default as studyNoteService } from './studyNoteService';
6 changes: 5 additions & 1 deletion src/service/interviewService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PrismaClient } from '@prisma/client';
import errorGenerator from '../middleware/error/errorGenerator';
import { message, statusCode } from '../module/constant';
import { count } from 'console';
import { scoreAnswer } from '../index'
const prisma = new PrismaClient();

const startInterview = async (startInterviewDTO: startInterviewDTO, refreshToken: string) => {
Expand Down Expand Up @@ -128,13 +129,16 @@ const makeFeedback = async (makeFeedbackDTO: makeFeedbackDTO, interviewQuestionI
time: makeFeedbackDTO.time,
}
});

const feedbackText = await scoreAnswer(makeFeedbackDTO.text);

const feedback = await prisma.feedback.create({
data: {
interviewQuestionId: interviewQuestionId,
answerId: answer.id,
interviewId: answer.interviewId,
score: null,
feedbackText: null,
feedbackText: feedbackText,
}
})

Expand Down
24 changes: 20 additions & 4 deletions src/service/questionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ const prisma = new PrismaClient();

const addPin = async (interveiwQuestionId: number) => {
try {
const pin = await prisma.interviewQuestion.update({
const findQuestionId = await prisma.interviewQuestion.findFirst({
where: {
id: interveiwQuestionId
id: interveiwQuestionId,
},
select: {
questionId: true
}
})
const pin = await prisma.question.update({
where: {
id: findQuestionId?.questionId
},
data: {
pin: true,
Expand All @@ -21,9 +29,17 @@ const addPin = async (interveiwQuestionId: number) => {

const deletePin = async (interveiwQuestionId: number) => {
try {
const pin = await prisma.interviewQuestion.update({
const findQuestionId = await prisma.interviewQuestion.findFirst({
where: {
id: interveiwQuestionId,
},
select: {
questionId: true
}
})
const pin = await prisma.question.update({
where: {
id: interveiwQuestionId
id: findQuestionId?.questionId
},
data: {
pin: false,
Expand Down
108 changes: 108 additions & 0 deletions src/service/studyNoteService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { PrismaClient } from '@prisma/client';
import errorGenerator from '../middleware/error/errorGenerator';
import { message, statusCode } from '../module/constant';
const prisma = new PrismaClient();

const getStudyNote = async (refreshToken: string, sortNum: number) => {
try {
const getUser= await prisma.user.findFirst({
where: {
refreshToken: refreshToken
},
select: {
id: true
}
});

const interviews = await prisma.interview.findMany({
where: {
userId: getUser!.id,
},
select: {
id: true
}
})

const questionList = [];
for (const interview of interviews) {
const interviewQuestions = await prisma.interviewQuestion.findMany({
where: {
interviewId: interview.id
},
select: {
questionId: true,
interviewId: true,
}
});
for (const interviewQuestion of interviewQuestions) {
const question = await prisma.question.findFirst({
where: {
id: interviewQuestion.questionId
},
select: {
id: true,
subjectId: true,
questionText: true,
pin: true,
again: true
}
})
const getAnswer = await prisma.answer.findFirst({
where: {
questionId: interviewQuestion.questionId
},
select: {
id: true
}
})
const getFeedback = await prisma.feedback.findFirst({
where: {
answerId: getAnswer!.id
},
select: {
score: true,
}
})
const getTitle = await prisma.interview.findFirst({
where:{
id: interviewQuestion.interviewId
},
select: {
title: true,
}
})
const getSubjectText = await prisma.subject.findFirst({
where: {
id: question?.subjectId
},
select: {
subjectText: true
}
})
const result = ({
data: {
id: question!.id,
subjectText: getSubjectText!.subjectText,
title: getTitle?.title,
again: question!.again,
questionText: question!.questionText,
score: getFeedback?.score,
pin: question!.pin
}
});
questionList.push(result)
}
}
return {
data: {
questionList
}
}
} catch(error) {
throw error;
}
};

export default {
getStudyNote,
};
Loading

0 comments on commit cd817a4

Please sign in to comment.