Skip to content
Permalink
48e450480a
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
63 lines (58 sloc) 2.12 KB
const sqlite = require('sqlite-async')
const config = require('../config.json')
module.exports = class Question {
//CONTRUCTOR FOR THE DB
constructor(dbName = ':memory:'){
return (async() => {
this.db = await sqlite.open(dbName)
//CHECK IF TABLE EXISTS ELSE CREATE
const sql = 'CREATE TABLE IF NOT EXISTS available_questions (\
id INTEGER PRIMARY KEY AUTOINCREMENT,\
question TEXT, possible_answer1 TEXT, possible_answer2 TEXT, possible_answer3 TEXT, correct_answer TEXT\
);'
await this.db.run(sql)
return this
})()
}
//INSERT QUESTIONS IN DB - BACKOFFICE
async questionz(questions) {
try{
console.log('Question Added')
const sql = `INSERT INTO available_questions(question, possible_answer1, possible_answer2, possible_answer3, correct_answer)\
VALUES("${questions.question}","${questions.possible_answer1}","${questions.possible_answer2}","${questions.possible_answer3}","${questions.correct_answer}")`
console.log(sql)
await this.db.run(sql)
} catch(err) {
console.log(err.message)
throw err
}
}
//SELECT MOST QUESTIONS IN DB
async all(){
try {
console.log('ALL questions')
const sql = 'SELECT id, question, possible_answer1, possible_answer2, possible_answer3, correct_answer FROM available_questions'
console.log(sql)
const questiont = await this.db.all(sql)
console.log(questiont)
return questiont
} catch(err) {
console.log(err.message)
throw err
}
}
//GETS THE ID
async getById(id){
try{
console.log('BY ID')
console.log(id)
const sql =`SELECT * FROM available_questions WHERE id=${id}`
console.log(sql)
const question = await this.db.get(sql)
return question
} catch(err) {
console.log(err.message)
throw err
}
}
}