Skip to content
Snippets Groups Projects
Commit f05232c1 authored by Heine Mærde Brakstad's avatar Heine Mærde Brakstad
Browse files

Merge branch 'backend-frontend-sync' into 'main'

fixed frontend backend sync for adding questions to quiz

See merge request !30
parents 0b69c6df f98727f6
No related branches found
No related tags found
1 merge request!30fixed frontend backend sync for adding questions to quiz
Pipeline #269309 passed
......@@ -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;
}
......
<script>
import { apiClient } from "@/api.js";
import { typeEnums } from "@/data/type.js"
export default {
props: {
quizId: {
......@@ -12,31 +14,36 @@ 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');
},
......@@ -49,7 +56,6 @@ export default {
}
}
}
};
</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>
......
......@@ -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,7 +160,7 @@ 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">
......@@ -237,7 +236,7 @@ async function submitQuestion() {
-->
<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>
......
export const typeEnums = [
"MULTIPLE_CHOICE",
'TRUE_OR_FALSE',
'STRING'
];
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment