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?
Forum-Website/modules/update.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
73 lines (66 sloc)
2.49 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 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 Update { | |
constructor(dbName = ':memory:') { | |
return (async() => { | |
this.db = await sqlite.open(dbName) | |
// we need this table to store the user accounts. | |
const sql = `CREATE TABLE IF NOT EXISTS messages | |
(message_id INTEGER PRIMARY KEY AUTOINCREMENT, | |
created_date date default CURRENT_DATE, | |
created_time date default CURRENT_TIME, | |
content TEXT, visibility TEXT, | |
user_id INTEGER, user_name TEXT, profile TEXT, forum_id INTEGER, | |
FOREIGN KEY (forum_id) REFERENCES forums (id), | |
FOREIGN KEY (user_id) REFERENCES user (id), | |
FOREIGN KEY (user_name) REFERENCES user (user));` | |
await this.db.run(sql) | |
return this | |
})() | |
} | |
async createMessage(message, selectedUserData) { | |
if (message.content.length === 0) throw new Error('empty comment') | |
const sql = `INSERT INTO messages(created_date, created_time, content, visibility, | |
user_id, user_name, profile, forum_id) VALUES((DATE('now')), (TIME('now')), | |
"${message.content}","true","${selectedUserData.id}", "${selectedUserData.user}", | |
"${selectedUserData.profile}","${message.forum_id}");` | |
await this.db.run(sql) | |
return true | |
} | |
/*Moved to forum.js | |
async loadForumData(forum) { | |
const userSql = `SELECT * FROM forums WHERE id = "${forum.forum_id}";` | |
const data = await this.db.get(userSql) | |
return data | |
}*/ | |
async loadMessageData(forum) { | |
const userSql = `SELECT * FROM messages WHERE forum_id = "${forum.forum_id}" AND visibility = "true";` | |
const data = await this.db.all(userSql) | |
return data | |
} | |
async updateMessage(message, selectedUserData) { | |
//console.log(message, selectedUserData, 'ss') | |
if (selectedUserData.user !== 'Admin') throw new Error('Only Admin User can alter comments') | |
if (message.visibility === 'null') { | |
//console.log('Userrr') | |
message.visibility = null | |
} | |
const sql = `Update messages set visibility=${message.visibility} where forum_id="${message.forum_id}" | |
AND message_id="${message.message_id}"` | |
await this.db.run(sql) | |
return true | |
} | |
async searchMessagesData(searchText) { | |
try { | |
const query = `SELECT * FROM messages WHERE content LIKE "%${searchText}%"` | |
const data = await this.db.all(query) | |
return data | |
} finally { | |
console.log('done') | |
} | |
} | |
} | |