Permalink
Cannot retrieve contributors at this time
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?
dasilv32/modules/answers.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
49 lines (43 sloc)
1.25 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sqlite = require('sqlite-async') | |
const config = require('../config.json') | |
module.exports = class Answer { | |
//CONSTRUCTOR FOR THE DB | |
constructor(dbName = ':memory:'){ | |
return (async() => { | |
this.db = await sqlite.open(dbName) | |
const sql = 'CREATE TABLE IF NOT EXISTS user_answer (\ | |
id INTEGER PRIMARY KEY AUTOINCREMENT,\ | |
answer TEXT\ | |
);' | |
await this.db.run(sql) | |
return this | |
})() | |
} | |
//INSERT ANSWERS IN DB | |
async quiz(answers) { | |
try{ | |
console.log('ADD answer') | |
const sql = `INSERT INTO user_answer(answer)\ | |
VALUES("${answers.answer}")` | |
console.log(sql) | |
await this.db.run(sql) | |
} catch(err){ | |
console.log(err.message) | |
throw err | |
} | |
} | |
//SELECT ALL ANSWERS IN DB | |
async all() { | |
try{ | |
console.log('ALL answers') | |
const sql = 'SELECT id, answer FROM user_answer' | |
console.log(sql) | |
const answert = await this.db.all(sql) | |
console.log(answert) | |
return answert | |
} catch(err){ | |
console.log(err.message) | |
throw err | |
} | |
} | |
} |