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?
WebsiteProject/modules/article.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
129 lines (117 sloc)
3.93 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 Database = require('sqlite-async') | |
class Article { | |
constructor(dbName = ':memory:') { | |
return (async() => { | |
this.db = await Database.open(dbName) | |
const sql = `CREATE TABLE IF NOT EXISTS Articles ( | |
"article_id" INTEGER PRIMARY KEY AUTOINCREMENT, | |
"title" TEXT NOT NULL, | |
"summary" TEXT, | |
"content" NVARCHAR NOT NULL, | |
"written_date" DATE NOT NULL, | |
"author" TEXT, | |
"image_url" TEXT, | |
"tag" TEXT, | |
"released" INTEGER);` | |
await this.db.run(sql) | |
return this | |
})() | |
} | |
async upload(title, summary, content, tag, username) { | |
try{ | |
if(title === null) throw new Error('Please enter a title for your article') | |
else if(summary === null) throw new Error('Please enter a summary for your article') | |
else if(content === null) throw new Error('Where is the content? Please Enter some text in the content box') | |
const date = await new Date() | |
const fullDate = `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}` | |
let sql = `INSERT INTO Articles(title, summary, content, written_date, tag, released, author) | |
VALUES("${title}", "${summary}", "${content}", "${fullDate}", "${tag}", 0, "${username}")` | |
await this.db.run(sql) | |
sql = 'SELECT last_insert_rowid() as id' | |
const result = await this.db.get(sql) | |
sql = `UPDATE Articles SET image_url = "articleImages/${result.id}.png" WHERE article_id = ${result.id};` | |
await this.db.run(sql) | |
return result.id | |
} catch(err) { | |
throw err | |
} | |
} | |
async reupload(id, title, summary, content, tag) { | |
try{ | |
if(title === null) throw new Error('Please enter a title for your article') | |
else if(summary === null) throw new Error('Please enter a summary for your article') | |
else if(content === null) throw new Error('Where is the content? Please Enter some text in the content box') | |
const date = await new Date() | |
const fullDate = `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}` | |
let sql = `UPDATE Articles SET title = "${title}", summary = "${summary}", content = "${content}", | |
written_date = "${fullDate}", tag = "${tag}", released = 0 | |
WHERE article_id = ${id};` | |
await this.db.run(sql) | |
sql = `UPDATE Articles SET image_url = "articleImages/${id}.png" WHERE article_id = ${id};` | |
await this.db.run(sql) | |
} catch(err) { | |
throw err | |
} | |
} | |
async release(id) { | |
try { | |
const query = `UPDATE Articles SET released = 1 WHERE article_id = ${id};` | |
await this.db.run(query) | |
} catch(err) { | |
throw err | |
} | |
} | |
async get(id) { | |
try{ | |
const data = await this.db.get(`SELECT * FROM Articles WHERE article_id = '${id}';`) | |
if(data === undefined) throw new Error('This article does not exist') | |
return data | |
} catch(err) { | |
throw err | |
} | |
} | |
async getByTag(tag) { | |
try { | |
const data = await this.db.all(`SELECT * FROM Articles WHERE tag = '${tag}';`) | |
Array.prototype.reverse.call(data) | |
return data | |
} catch(err) { | |
throw err | |
} | |
} | |
async getAll() { | |
try{ | |
const data = await this.db.all('SELECT * FROM Articles;') | |
Array.prototype.reverse.call(data) | |
return data | |
} catch(err) { | |
throw err | |
} | |
} | |
async getUnreleased() { | |
try{ | |
const data = await this.db.all('SELECT * FROM Articles WHERE released = 0;') | |
return data | |
} catch(err) { | |
throw err | |
} | |
} | |
async search(searchQuery) { | |
try { | |
let sql = 'SELECT * FROM Articles WHERE released = 1;' | |
if(searchQuery !== undefined && searchQuery.q !== undefined) { | |
sql = `SELECT * FROM Articles | |
WHERE upper(title) LIKE upper("%${searchQuery.q}%") AND released = 1 | |
OR upper(summary) LIKE upper("%${searchQuery.q}%") AND released = 1 | |
OR written_date LIKE "%${searchQuery.q}%" AND released = 1 | |
OR upper(author) LIKE upper("%${searchQuery.q}%") AND released = 1;` | |
} | |
const data = await this.db.all(sql) | |
return data | |
} catch(err) { | |
throw err | |
} | |
} | |
} | |
module.exports = Article |