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/core/models/question.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
37 lines (32 sloc)
893 Bytes
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 Database = require('sqlite-async') | |
const dbName = 'server.db' | |
module.exports = class Question { | |
async getAllQuestions(query) { | |
try { | |
let sql = 'SELECT id, title, question FROM Questions;' | |
console.log(query.search) | |
if(query !== undefined && query.search !== undefined) { | |
sql = `SELECT id, title, question FROM Questions | |
WHERE upper(title) LIKE "%${query.search}%";` | |
} | |
const db = await Database.open(dbName) | |
const data = await db.all(sql) | |
await db.close() | |
console.log(data) | |
return data | |
} catch(err) { | |
return err.message | |
} | |
} | |
async insertQuestion(request) { | |
console.log(request.body) | |
const body = request.body | |
const sql = `INSERT INTO Questions(title, question) | |
VALUES("${body.title}", "${body.question}");` | |
console.log(sql) | |
const db = await Database.open(dbName) | |
await db.run(sql) | |
await db.close() | |
} | |
} |