forked from dpmurphy11/trivia-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildQuizes.js
159 lines (136 loc) · 4.93 KB
/
buildQuizes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const axios = require('axios');
const { Categories, Types, Difficulties, Quiz, QuizQuestion, QuizAnswers } = require('./models');
const quizAPIURL = 'https://opentdb.com/api.php?amount=10';
// get categories
getCategories = async () => {
const catData = await Categories.findAll({
attributes: {
exclude: ['category_name', 'active']
},
where: {
active: true,
},
});
return catData.map(({ id, api_id }) => ({
id: id,
api_id: api_id,
}));
};
// get types
getTypes = async () => {
const typesData = await Types.findAll({
attributes: {
exclude: ['display_name', 'active']
},
where: {
active: true,
},
});
return typesData.map(({ id, type_name }) => ({
id: id,
name: type_name,
}));
};
// get difficulties
getDifficulties = async () => {
const diffsData = await Difficulties.findAll({
attributes: {
exclude: ['display_name', 'active']
},
where: {
active: true,
},
});
return diffsData.map(({ id, difficulty_name }) => ({
id: id,
name: difficulty_name,
}));
};
saveQuestion = async (quizId, question_text) => {
// console.log(question_text);
const newQuestion = await QuizQuestion.create({
quiz_id: quizId,
question_text: question_text,
});
// console.log(newQuestion.id);
return newQuestion.id;
}
saveAnswer = async (questionId, answer_text, correct) => {
// console.log(answer_text);
const newAnswer = await QuizAnswers.create({
question_id: questionId,
quiz_answer: answer_text,
correct_yn: correct,
});
return newAnswer.id;
}
// call api and save quiz, questions, and answers
fetchAndSaveQuizes = async (category, difficulty, type) => {
try {
const response = await axios.get(`${quizAPIURL}&category=${category.api_id}&type=${type.name}&difficulty=${difficulty.name}`);
const results = response.data.results;
if (response.data.response_code == 0) {
const newQuiz = await Quiz.create({
category_id: category.id,
type_id: type.id,
difficulty_id: difficulty.id,
});
const promises = results.map(async (result, i) => {
const newQuestionId = await saveQuestion(newQuiz.id, result.question);
// save correct answer
await saveAnswer(newQuestionId, result.correct_answer, true);
// save incorrect answers
for (x = 0; x < result.incorrect_answers.length; x++) {
saveAnswer(newQuestionId, result.incorrect_answers[x], false);
}
});
await Promise.all(promises);
// for (i = 0; i < response.data.results.length; i++) {
// console.log(response.data.results[i].question.trim().length);
// console.log(response.data.results[i].correct_answer.trim().length);
// console.log(response.data.results[i].incorrect_answers.length);
// if (response.data.results[i].question.trim().length && response.data.results[i].correct_answer.trim().length && response.data.results[i].incorrect_answers.length) {
// save quiz
// const newQuiz = await Quiz.create({
// category_id: category.id,
// type_id: type.id,
// difficulty_id: difficulty.id,
// });
// const newQuestionId = await saveQuestion(newQuiz.id, response.data.results[i].question);
// // save correct answer
// saveAnswer(newQuestionId, response.data.results[i].correct_answer, true);
// // save incorrect answers
// for (x = 0; x < response.data.results[i].incorrect_answers.length; x++) {
// saveAnswer(newQuestionId, response.data.results[i].incorrect_answers[x], false);
// }
// }
// }
}
} catch (error) {
console.error(error);
}
};
const buildQuizes = async () => {
try {
// get categories
const cats = await getCategories();
// get types
const types = await getTypes();
// get difficulties
const diffs = await getDifficulties();
// call api for each permutation
for (a = 0; a < cats.length; a++) {
// cats.forEach(cat => {
for (b = 0; b < diffs.length; b++) {
// diffs.forEach(diff => {
for (c = 0; c < types.length; c++) {
// types.forEach(type => {
fetchAndSaveQuizes(cats[a], diffs[b], types[c]);
}
}
}
} catch (error) {
console.error(`buildQuizes: ${error}`);
}
}
buildQuizes();