Skip to content
Permalink
e504f7a13e
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
134 lines (124 sloc) 3.85 KB
'use strict'
const sqlite = require('sqlite-async')
/**
* Class representing images in the database.
*/
class Image {
/**Constructs database with the appropriate table for article images.
* @constructor
* @param {string} [dbName=':memory:'] - database for the website
*/
constructor(dbName = ':memory:') {
return (async() => {
this.db = await sqlite.open(dbName)
let sql = `CREATE TABLE IF NOT EXISTS images (
id INTEGER PRIMARY KEY AUTOINCREMENT,
image_path TEXT,
image_description TEXT);`
await this.db.run(sql)
sql = 'INSERT INTO images (image_path) values ("public/avatars/avatar.png")'
await this.db.run(sql)
return this
})()
}
/**
* Adds filename of selected file to the database.
* @param {string} filename
* @param {string} [description]
* @returns {Promise<integer>} id of image file
*/
async add(filename, description) {
try{
if(filename.length === 0) throw new Error('filename is missing')
let sql = `SELECT COUNT(id) as records from images WHERE image_path = '${filename}';`
const data = await this.db.get(sql)
if(data.records !== 0) throw new Error('filename is already in use')
if(description === undefined) sql = `INSERT INTO images (image_path) values ('${filename}');`
else sql = `INSERT INTO images (image_path, image_description) values ('${filename}', '${description}');`
await this.db.run(sql)
sql = 'SELECT last_insert_rowid() as id'
const result = await this.db.get(sql)
return result['id']
} catch(err) {
throw err
}
}
/**
* Retrieves details of an image
* @param {Integer} key - id of the image
* @returns {Promise<imageData>}
*/
async get(key) {
try{
if(key === undefined) throw new Error('key is not defined')
let sql = `SELECT COUNT(id) as records FROM images WHERE id = ${key};`
let data = await this.db.get(sql)
if(data.records === 0) throw new Error('file does not exist')
sql = `SELECT image_path, image_description
FROM images WHERE id = ${key};`
data = await this.db.get(sql)
return data
} catch(err) {
throw err
}
}
/**
* Retrieves details of all stored images
* @returns {Promise<imageData[]>} - array of image data
*/
async getAll() {
const sql = `SELECT image_path as path,
image_description as description
FROM images;`
const data = await this.db.all(sql)
return data
}
/**
* Rename an existing file
* @param {string} targetFile - full path of file
* @param {string} newName - new filename
* @returns {Promise<true>} confirmation for file being renamed
*/
async rename(targetFile, newName) {
try{
if(targetFile.length === 0) throw new Error('file not selected')
if(newName.length === 0) throw new Error('new name not stated')
let sql = `SELECT COUNT(id) as records FROM images WHERE image_path = '${targetFile}';`
const data = await this.db.get(sql)
if(data.records === 0) throw new Error('file is not found')
sql = `UPDATE images SET image_path = '${newName}' WHERE image_path = '${targetFile}';`
await this.db.run(sql)
return true
} catch(err) {
throw err
}
}
/**
* Delete an exisitng image file based on image id
* @param {integer} key
* @returns {Promise<true>} confirmation for image deleted
*/
async delete(key) {
try{
if(key === undefined) throw new Error('key is not defined')
let sql = `SELECT COUNT(id) as records FROM images WHERE id = ${key};`
const data = await this.db.get(sql)
if(data.records === 0) throw new Error('file is not found')
sql = `DELETE FROM images WHERE id = ${key}`
await this.db.run(sql)
return true
} catch(err) {
throw err
}
}
/**
* Count the sum of images
* @returns {Promise<integer>} number representing amount of stored images
*/
async count() {
const sql = 'SELECT COUNT(id) as records FROM images'
const data = await this.db.get(sql)
return data.records
}
}
module.exports = Image