diff --git a/FullstackProsjekt/src/backend/main/java/edu/ntnu/idatt2105/controller/QuizController.java b/FullstackProsjekt/src/backend/main/java/edu/ntnu/idatt2105/controller/QuizController.java index 6935d07efd420bbb5a49437d4ed158515b9e0fa4..0477174f18efc1d2388284e721815bcefadd109b 100644 --- a/FullstackProsjekt/src/backend/main/java/edu/ntnu/idatt2105/controller/QuizController.java +++ b/FullstackProsjekt/src/backend/main/java/edu/ntnu/idatt2105/controller/QuizController.java @@ -22,6 +22,8 @@ import java.util.stream.Collectors; public class QuizController { private final QuizService quizService; + private static final Logger logger = LoggerFactory.getLogger(AuthenticationController.class); + @Autowired public QuizController(QuizService quizService) { @@ -41,9 +43,9 @@ public class QuizController { return quizService.updateQuiz(quizDTO); } - @PostMapping("/delete") - public void deleteQuiz(@RequestBody Map<String, Integer> payload) { - quizService.deleteQuiz(payload.get("id")); + @PostMapping("/delete/{quizId}") + public void deleteQuiz(@PathVariable Integer quizId) { + quizService.deleteQuiz(quizId); } @GetMapping("/quiz/{quizId}") @@ -82,9 +84,11 @@ public class QuizController { public QuizDTO convertToDTO(Quiz quiz) { QuizDTO quizDTO = new QuizDTO(); + quizDTO.setCreatorId(quiz.getCreator().getId()); quizDTO.setId(quiz.getId()); quizDTO.setTitle(quiz.getTitle()); quizDTO.setCategory(quiz.getCategory()); + quizDTO.setDifficulty(quiz.getDifficulty()); return quizDTO; } diff --git a/FullstackProsjekt/src/frontend/src/components/shared/NewQuestionModel.vue b/FullstackProsjekt/src/frontend/src/components/shared/NewQuestionModel.vue index b502ac6b73757b77db8798c8b99c021dd75cb282..2415f1bb7a6aca4f76a6d136756d27824c0ff8f1 100644 --- a/FullstackProsjekt/src/frontend/src/components/shared/NewQuestionModel.vue +++ b/FullstackProsjekt/src/frontend/src/components/shared/NewQuestionModel.vue @@ -1,5 +1,7 @@ <script> -import {apiClient} from "@/api.js"; +import { apiClient } from "@/api.js"; +import { typeEnums } from "@/data/type.js" + export default { props: { quizId: { @@ -12,44 +14,48 @@ export default { questionText: '', answers: [{ text: '', correct: false }], correctAnswerIndex: 0, - type: 'MC', + type: 'MULTIPLE_CHOICE', score: 0, correctAnswer: null, - errorMsg: '' + errorMsg: '', + selectedType: null, + questionTypes: typeEnums } }, methods: { - //TODO: error prevention/handling - async handleSubmit() { + async submitForm() { try { this.findCorrectAnswer(); - await apiClient.post('/questions', { + const data = { quizId: this.quizId, questionText: this.questionText, - type: this.type, + type: this.selectedType, answer: this.correctAnswer, - options: this.answers.map(answer => answer.text), //just the strings! - score: this.score - //add questionId to questions in editQuiz! - - }) + options: this.answers.map(answer => answer.text), + score: this.score, + }; + console.log(data); + await apiClient.post('/questions/save', data); } catch (error) { this.errorMsg = 'Error signing up'; } }, + async handleSubmit() { + await this.submitForm(); + this.$emit('close'); + }, closeModal() { this.$emit('close'); }, newAnswer() { this.answers.push({ text: '', correct: false }); }, - findCorrectAnswer(){ + findCorrectAnswer() { if (this.correctAnswerIndex !== null && this.answers[this.correctAnswerIndex]) { this.correctAnswer = this.answers[this.correctAnswerIndex].text; } } } - }; </script> @@ -57,15 +63,20 @@ export default { <div class="modal-overlay" @click="closeModal"> <div @click.stop class="modal-mask"> <div class="modal-container"> - <form @submit.prevent="handleSubmit"> + <form @submit.prevent="submitForm"> <div class="question-title"> <h3>Question:</h3> <input v-model="questionText" placeholder="Type your question here"> + <label>Score:</label> <input type="number" id="scoreInput" v-model="score"> + </div> <div class="modal-body"> + <select v-model="selectedType"> + <option v-for="type in questionTypes" :key="type" :value="type">{{type}}</option> + </select> <!-- <AnswerCard answer-id="answerCard" v-for="answer in answers" :key="answer.id" :answerId="answer.id" :answer="answer.answer" :correct="answer.correct"/> @@ -89,8 +100,8 @@ export default { </table> </div> <div class="modal-footer"> - <button class="edit-btn" @click="newAnswer">Add answer</button> - <button class="modal-default-button" @click="$emit('close')">SUBMIT</button> + <button class="edit-btn" @click.prevent="newAnswer">Add answer</button> + <button type="submit" class="modal-default-button" @click.prevent="handleSubmit">SUBMIT</button> </div> </form> </div> diff --git a/FullstackProsjekt/src/frontend/src/components/shared/create-quiz/EditQuizView.vue b/FullstackProsjekt/src/frontend/src/components/shared/create-quiz/EditQuizView.vue index 140a2887d5da9e606e55bf4393d7c3ef3d55047a..e441809449f5f69551d1362a603e4e9ee6ff003b 100644 --- a/FullstackProsjekt/src/frontend/src/components/shared/create-quiz/EditQuizView.vue +++ b/FullstackProsjekt/src/frontend/src/components/shared/create-quiz/EditQuizView.vue @@ -27,7 +27,6 @@ export default { creatorId: null, quizId: null, quizTitle: '', - questions: [], //question list is just a list of q-ids!! category: '', difficulty: '', errorMsg: '' @@ -43,11 +42,11 @@ export default { console.log('Fetching data for quiz: ', quizId); try { apiClient.get('/quiz/quiz/' + this.quizId).then(response => { + console.log(response) this.quizTitle = JSON.parse(response.data.title); - this.questions = JSON.parse(JSON.stringify(response.data.questions)); this.creatorId = JSON.parse(response.data.creatorId); - this.category = JSON.parse(response.data.category); - this.difficulty = JSON.parse(response.data.difficulty); + this.category = response.data.category; + this.difficulty = response.data.difficulty; }); } catch (error) { //TODO: proper error handling @@ -63,7 +62,7 @@ export default { //TODO: questions answers, +question count }, deleteQuiz() { - //API req, quizId + apiClient.post('/quiz/delete/' + this.quizId) } }, }; @@ -161,11 +160,31 @@ async function submitQuestion() { <QuestionCard v-for="question in questions" :question-id=question.id :key="question.id"/> </div> - <NewQuestionModel v-if="showNewQuestion" @close="hideNewQuestion" quiz-id="this.quizId"/> + <NewQuestionModel v-if="showNewQuestion" @close="hideNewQuestion" :quizId=this.quizId></NewQuestionModel> + <!-- + <div class="question-table"> + <table class="table"> + <thead> + <tr> + <th scope="col">#</th> + <th scope="col">Question</th> + <th scope="col">Action</th> + </tr> + </thead> + <tbody> + <tr> + <th scope="row">1</th> + <td>What is Vue?</td> + <td> + <button class="play-btn">View</button> + <button class="edit-btn">Edit</button> + <button class="delete-btn"> Delete</button> + </td> + </tr> <div class="footer"> <button @click="newQuestion" class="add-Btn"> Add Question </button> - <button class="delete-btn"> DELETE QUIZ </button> + <button @click="deleteQuiz" class="delete-btn"> DELETE QUIZ </button> <button class="save-Btn"> SAVE QUIZ </button> </div> </div> diff --git a/FullstackProsjekt/src/frontend/src/data/type.js b/FullstackProsjekt/src/frontend/src/data/type.js new file mode 100644 index 0000000000000000000000000000000000000000..8376fa6182ab5fb85029e906a7b2916720876856 --- /dev/null +++ b/FullstackProsjekt/src/frontend/src/data/type.js @@ -0,0 +1,5 @@ +export const typeEnums = [ + "MULTIPLE_CHOICE", + 'TRUE_OR_FALSE', + 'STRING' +]; \ No newline at end of file