Skip to content
Permalink
fa85284f92
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 (56 sloc) 2.12 KB
'use strict'
//const bcrypt = require('bcrypt-promise')
//const fs = require('fs-extra')
//const mime = require('mime-types')
const sqlite = require('sqlite-async')
//const saltRounds = 10
module.exports = class Forum {
constructor(dbName = ':memory:') {
return (async() => {
this.db = await sqlite.open(dbName)
// we need this table to store the forums details
const sql = `CREATE TABLE IF NOT EXISTS forums
(id INTEGER PRIMARY KEY AUTOINCREMENT, forum_name TEXT, forum_description TEXT, forum_message TEXT,
created_date date default CURRENT_DATE, created_time date default CURRENT_TIME,
forum_link TEXT, user_id INTEGER, user_name TEXT, profile TEXT,
FOREIGN KEY (user_id) REFERENCES user (id), FOREIGN KEY (user_name) REFERENCES user (user));`
await this.db.run(sql)
return this
})()
}
async registerForum(forum, selectedUserData) {
try {
if (forum.forum_name.length === 0) throw new Error('missing forum name')
if (forum.forum_description.length === 0) throw new Error('missing forum description')
if (forum.forum_message.length === 0) throw new Error('missing initial message')
const sql = `INSERT INTO forums(forum_name, forum_description, forum_message, created_date, created_time,
forum_link, user_id, user_name, profile) VALUES("${forum.forum_name}","${forum.forum_description}",
"${forum.forum_message}", (DATE('now')), (TIME('now')),
"${forum.forum_links}", "${selectedUserData.id}", "${selectedUserData.user}",
"${selectedUserData.profile}");`
const data = await this.db.run(sql)
return data
} catch (err) {
throw err
}
}
async loadForumsData() {
const query = 'SELECT * FROM forums'
const data = await this.db.all(query)
return data
}
async loadForumData(forum) {
const userSql = `SELECT * FROM forums WHERE id = "${forum.forum_id}";`
const data = await this.db.get(userSql)
return data
}
async searchForumsData(searchText) {
try {
const query = `SELECT * FROM forums WHERE forum_name LIKE "%${searchText}%"`
const data = await this.db.all(query)
return data
} finally {
console.log('done')
}
}
}