-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquestion.js
68 lines (67 loc) · 2.06 KB
/
question.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
const db = require('./db');
const knex = require('../knex/knex.js');
const Answer = require('./answer.js');
module.exports = {
get: (id) => {
},
saveAnswers(answers, question_id) {
throw "Undefined saveAnswers method for question type";
},
update: async (question) => {
return await knex.transaction(function(trx) {
knex.update({
question: question.question,
time_limit: question.time_limit,
question_order: question.question_order,
question_type: question.question_type,
})
.table('questions')
.where({'questions.question_id': question.question_id})
.returning('*')
.transacting(trx)
.then(function(update) {
question.question_id = update[0].question_id;
question.title = update[0].title;
return module.exports.saveAnswers(question.answers, update[0].question_id);
})
.then(trx.commit)
.catch(trx.rollback);
})
.then(function(answers) {
question.answers = answers;
return question;
})
.catch(function(error) {
console.error('ugh2', error);
});
},
create: async (question, quiz_id) => {
return await knex.transaction(function(trx) {
knex.insert({
question: question.question,
time_limit: question.time_limit,
question_order: question.question_order,
quiz_id: quiz_id,
question_type: question.question_type,
})
.into('questions')
.returning('*')
.transacting(trx)
.then(function(insert) {
question.question_id = insert[0].question_id;
question.title = insert[0].title;
return module.exports.saveAnswers(question.answers, insert[0].question_id);
})
.then(trx.commit)
.catch(trx.rollback);
})
.then(function(answers) {
question.answers = answers;
return question;
})
.catch(function(error) {
// no inserts
console.error('ugh2', error);
});
},
}