Commit 4ad237e6 authored by Yoon, Daeki's avatar Yoon, Daeki 😅
Browse files

quiz 문제 추가

parent e7c36007
...@@ -30,7 +30,7 @@ function MainRouter() { ...@@ -30,7 +30,7 @@ function MainRouter() {
<Route path="/quiz/by/:userId"> <Route path="/quiz/by/:userId">
<Quizzes /> <Quizzes />
</Route> </Route>
<Route path="/quiz/problem/new"> <Route path="/quiz/problem/new/:quizId">
<NewProblem /> <NewProblem />
</Route> </Route>
<Route path="/quiz/problem/edit/:problemId"> <Route path="/quiz/problem/edit/:problemId">
......
...@@ -2,11 +2,18 @@ import React, { useState } from "react"; ...@@ -2,11 +2,18 @@ import React, { useState } from "react";
import Button from "react-bootstrap/Button"; import Button from "react-bootstrap/Button";
import Form from "react-bootstrap/Form"; import Form from "react-bootstrap/Form";
import Col from "react-bootstrap/Col"; import Col from "react-bootstrap/Col";
import { useParams } from "react-router-dom"; import { Link, useParams } from "react-router-dom";
import { createProblem } from "./api-quiz";
import authHelpers from "../auth/auth-helpers";
import Modal from "react-bootstrap/esm/Modal";
function NewProblem({ addProblem }) { function NewProblem({ addProblem }) {
const { quizId } = useParams();
const [answers, setAnswers] = useState([""]); const [answers, setAnswers] = useState([""]);
const [question, setQuestion] = useState(""); const [question, setQuestion] = useState("");
const [show, setShow] = useState(false)
const jwt = authHelpers.isAuthenticated();
const addAnswer = () => { const addAnswer = () => {
setAnswers([...answers, ""]); setAnswers([...answers, ""]);
...@@ -29,9 +36,25 @@ function NewProblem({ addProblem }) { ...@@ -29,9 +36,25 @@ function NewProblem({ addProblem }) {
setQuestion(event.target.value); setQuestion(event.target.value);
}; };
const clickAdd = (event) => { const clickAddProblem = (event) => {
event.preventDefault(); event.preventDefault();
addProblem({ question, answers }); const problem = {
question,
answers,
quiz: quizId,
};
createProblem({ userId: jwt.user._id }, { t: jwt.token }, problem).then(
(data) => {
if (data.error) {
console.log(data.error);
} else {
setQuestion(data.question);
setAnswers(data.answers);
setShow(true)
}
}
);
// addProblem({ question, answers });
}; };
return ( return (
...@@ -68,8 +91,19 @@ function NewProblem({ addProblem }) { ...@@ -68,8 +91,19 @@ function NewProblem({ addProblem }) {
</Form.Row> </Form.Row>
); );
})} })}
<Button onClick={clickAdd}>문제 추가</Button> <Button onClick={clickAddProblem}>문제 추가</Button>
</Form> </Form>
<Modal show={show}>
<Modal.Header>New Problem</Modal.Header>
<Modal.Body>
Problem successfully created.
</Modal.Body>
<Modal.Footer>
<Link to={`/quiz/${quizId}`}>
<Button>Go to quiz</Button>
</Link>
</Modal.Footer>
</Modal>
</div> </div>
); );
} }
......
...@@ -33,6 +33,23 @@ const read = async (params, credentials, signal) => { ...@@ -33,6 +33,23 @@ const read = async (params, credentials, signal) => {
} }
} }
const createProblem = async (params, credentials, problem) => {
try {
let response = await fetch('/api/quiz/problem/' + params.userId, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + credentials.t,
},
body: JSON.stringify(problem),
})
return await response.json()
} catch (error) {
console.log(error)
}
}
const readProblem = async (params, credentials, signal) => { const readProblem = async (params, credentials, signal) => {
try { try {
let response = await fetch('/api/quiz/problem/by/' + params.problemId, { let response = await fetch('/api/quiz/problem/by/' + params.problemId, {
...@@ -102,6 +119,7 @@ const listByUserId = async (params, credentials, signal) => { ...@@ -102,6 +119,7 @@ const listByUserId = async (params, credentials, signal) => {
export { export {
create, create,
read, read,
createProblem,
readProblem, readProblem,
updateProblem, updateProblem,
removeProblem, removeProblem,
......
...@@ -36,6 +36,27 @@ const create = async (req, res) => { ...@@ -36,6 +36,27 @@ const create = async (req, res) => {
} }
} }
const createProblem = async (req, res) => {
try {
const problem = new Problem(req.body)
problem.author = req.profile._id
await problem.save()
// push the problem to the quiz
if (problem.quiz) {
const quiz = await Quiz.findById(problem.quiz)
quiz.problems.push(problem)
await quiz.save()
}
res.json(problem)
} catch (error) {
return res.status(400).json({
error: dbErrorHandler.getErrorMessage(error)
})
}
}
const isAuthor = (req, res, next) => { const isAuthor = (req, res, next) => {
const isAuthor = req.auth && req.quiz && req.auth._id == req.quiz.author._id const isAuthor = req.auth && req.quiz && req.auth._id == req.quiz.author._id
if (!isAuthor) { if (!isAuthor) {
...@@ -152,6 +173,7 @@ const problemById = async (req, res, next, id) => { ...@@ -152,6 +173,7 @@ const problemById = async (req, res, next, id) => {
export default { export default {
create, create,
createProblem,
read, read,
readProblem, readProblem,
updateProblem, updateProblem,
......
...@@ -6,19 +6,19 @@ import quizCtrl from './quiz.controller.js' ...@@ -6,19 +6,19 @@ import quizCtrl from './quiz.controller.js'
const router = express.Router() const router = express.Router()
router.route('/api/quiz/:userId') router.route('/api/quiz/:userId')
.post(authCtrl.requireSignin, authCtrl.hasAuthorization, userCtrl.isInstructor, quizCtrl.create) .post(authCtrl.requireSignin, authCtrl.hasAuthorization, userCtrl.isInstructor, quizCtrl.create) // 퀴즈 생성
.get(authCtrl.requireSignin, authCtrl.hasAuthorization, quizCtrl.listByUserId) .get(authCtrl.requireSignin, authCtrl.hasAuthorization, quizCtrl.listByUserId) // 모든 퀴즈 반환
router.route('/api/quiz/by/:quizId') router.route('/api/quiz/by/:quizId')
.get(authCtrl.requireSignin, quizCtrl.isAuthor, quizCtrl.read) .get(authCtrl.requireSignin, quizCtrl.isAuthor, quizCtrl.read) // 퀴즈 하나 반환
router.route('/api/quiz/problem/:userId') router.route('/api/quiz/problem/:userId')
.post(authCtrl.requireSignin, userCtrl.isInstructor) .post(authCtrl.requireSignin, userCtrl.isInstructor, quizCtrl.createProblem) // 문제 생성
router.route('/api/quiz/problem/by/:problemId') router.route('/api/quiz/problem/by/:problemId')
.get(authCtrl.requireSignin, quizCtrl.isProblemAuthor, quizCtrl.readProblem) .get(authCtrl.requireSignin, quizCtrl.isProblemAuthor, quizCtrl.readProblem) // 문제 반환
.put(authCtrl.requireSignin, quizCtrl.isProblemAuthor, quizCtrl.updateProblem) .put(authCtrl.requireSignin, quizCtrl.isProblemAuthor, quizCtrl.updateProblem) // 문제 수정
.delete(authCtrl.requireSignin, quizCtrl.isProblemAuthor, quizCtrl.removeProblem) .delete(authCtrl.requireSignin, quizCtrl.isProblemAuthor, quizCtrl.removeProblem) // 문제 삭제
router.param('userId', userCtrl.userById) router.param('userId', userCtrl.userById)
router.param('quizId', quizCtrl.quizById) router.param('quizId', quizCtrl.quizById)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment