You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.4 KiB
124 lines
3.4 KiB
import sqlite3 from 'sqlite3' |
|
import ErrorType from '../error/types.error.js' |
|
import serverConfig from '../configuration/server.config.js' |
|
|
|
sqlite3.verbose() |
|
|
|
const tableName = 'Histories' |
|
|
|
class History { |
|
constructor(options = {}) { |
|
if (!options.name || !options.fullDate || !options.itemID || !options.itemName) { |
|
throw new Error('History has to have a name, a fullDate, an itemID and an itemName') |
|
} |
|
this.id = options.id |
|
this.name = options.name |
|
this.fullDate = options.fullDate |
|
this.itemID = options.itemID |
|
this.itemName = options.itemName |
|
this.class = options.class |
|
this.response = options.response |
|
this.votes = options.votes |
|
this.instance = options.instance |
|
this.boss = options.boss |
|
this.equipLoc = options.equipLoc |
|
this.note = options.note |
|
} |
|
|
|
static findAll() { |
|
return new Promise((resolve, reject) => { |
|
const db = openDB() |
|
db.serialize(() => { |
|
db.all(`SELECT * FROM ${tableName} order by fullDate desc`, (err, histories) => { |
|
if (err) reject(new Error(ErrorType.TECHNICAL_UNKNOWN)) |
|
else { |
|
const array = []; |
|
for (const history of histories) { |
|
array.push(createHistoryFromDB(history)) |
|
} |
|
resolve(array); |
|
} |
|
}) |
|
closeDB(db) |
|
}) |
|
}) |
|
} |
|
|
|
static delete(id) { |
|
return new Promise((resolve, reject) => { |
|
const db = openDB(); |
|
db.serialize(() => { |
|
db.run(`DELETE FROM ${tableName} WHERE id = '${id}'`, (err, history) => { |
|
if (err) reject(new Error(ErrorType.TECHNICAL_UNKNOWN)) |
|
else resolve(history) |
|
}) |
|
}) |
|
closeDB(db) |
|
}) |
|
} |
|
|
|
async insert() { |
|
return new Promise((resolve, reject) => { |
|
const values = `'${this.name}', ${this.fullDate}, '${this.itemID}', "${this.itemName}", '${this.class}', '${this.response}', '${this.votes}', "${this.instance}", "${this.boss}", "${this.equipLoc}", "${this.note}"`; |
|
const db = openDB(); |
|
db.serialize(() => { |
|
db.run(`INSERT INTO ${tableName} (name, fullDate, itemID, itemName, class, response, votes, instance, boss, equipLoc, note) VALUES (${values})`, err => { |
|
if (err) reject(new Error(ErrorType.TECHNICAL_UNKNOWN)) |
|
else resolve() |
|
}) |
|
}) |
|
closeDB(db) |
|
}) |
|
} |
|
|
|
toJson() { |
|
return { |
|
id: this.id, |
|
name: this.name, |
|
fullDate: this.fullDate, |
|
itemID: this.itemID, |
|
itemName: this.itemName, |
|
class: this.class, |
|
response: this.response, |
|
votes: this.votes, |
|
instance: this.instance, |
|
boss: this.boss, |
|
equipLoc: this.equipLoc, |
|
note: this.note |
|
} |
|
} |
|
} |
|
|
|
export default History |
|
|
|
function openDB() { |
|
const env = process.env.NODE_ENV || 'development' |
|
const db = new sqlite3.Database(serverConfig.database[env].path, err => { |
|
if (err) throw new Error(err) |
|
}) |
|
db.configure('busyTimeout', 30000) |
|
return db |
|
} |
|
|
|
function closeDB(db) { |
|
db.close(err => { |
|
if (err) throw new Error(err) |
|
}) |
|
} |
|
|
|
function createHistoryFromDB(dbHistory) { |
|
return new History({ |
|
id: dbHistory.id, |
|
name: dbHistory.name, |
|
fullDate: dbHistory.fullDate, |
|
itemID: dbHistory.itemID, |
|
itemName: dbHistory.itemName, |
|
class: dbHistory.class, |
|
response: dbHistory.response, |
|
votes: dbHistory.votes, |
|
instance: dbHistory.instance, |
|
boss: dbHistory.boss, |
|
equipLoc: dbHistory.equipLoc, |
|
note: dbHistory.note |
|
}) |
|
}
|
|
|