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?
FAQ-1/core/models/answer.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
40 lines (34 sloc)
1.09 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
'use strict' | |
const sqlite = require('sqlite-async') | |
const table = require('../dbTables') | |
module.exports = class Answer { | |
constructor(database = ':memory:') { | |
return (async() => { | |
this.db = await sqlite.open(database) | |
await this.db.run(table.createAnswersTable()) | |
return this | |
})() | |
} | |
async createAnswer(request, date) { | |
try { | |
if (request.body.body === '') throw new Error('Answer cannot be empty!') | |
const sql = `INSERT INTO answers(body, date, user_id, question_id) | |
VALUES("${request.body.body}", "${date}", "${request.session.user.id}", | |
"${request.parameters.question_id}");` | |
this.db.run(sql) | |
return true | |
} catch (err) { | |
throw err | |
} | |
} | |
async getAnswersByQuestion(id) { | |
const sql = `SELECT answers.*, users.name AS user_name FROM answers | |
INNER JOIN users ON users.id = answers.user_id WHERE question_id = "${id}";` | |
const answers = await this.db.all(sql) | |
return answers | |
} | |
async __testData() { | |
await this.db.run(table.createUsersTable()) | |
await this.db.run('INSERT INTO users(name, username, password) VALUES("Wallef", "username", "password");') | |
} | |
} |