Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
176 additions
and
29 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
const sqlite = require('sqlite-async') | ||
const atob = require('atob') | ||
|
||
module.exports = class List { | ||
|
||
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 lists\ | ||
(id INTEGER PRIMARY KEY AUTOINCREMENT,\ | ||
userid TEXT, listname TEXT, description TEXT,\ | ||
date TEXT DEFAULT CURRENT_TIMESTAMP);' | ||
await this.db.run(sql) | ||
return this | ||
})() | ||
} | ||
|
||
async add(token, listname, description) { | ||
try { | ||
console.log(token) | ||
const hash = token.substring(6) //removes the Basic_ bit | ||
console.log(hash) | ||
const str = atob(hash) | ||
console.log(str) | ||
const username = str.split(':')[0] | ||
console.log(username) | ||
const sql = `INSERT INTO lists(userid, listname, description)\ | ||
VALUES("${username}", "${listname}", "${description}")` | ||
console.log(sql) | ||
const recordID = await this.db.run(sql) | ||
console.log(recordID.lastID) | ||
return recordID.lastID | ||
// return 42 | ||
} catch(err) { | ||
console.log(err.message) | ||
throw err | ||
} | ||
} | ||
|
||
async getLists(token) { | ||
try { | ||
const hash = token.substring(6) //removes the Basic_ bit | ||
const str = atob(hash) | ||
const username = str.split(':')[0] | ||
const sql = `SELECT * FROM lists WHERE userid="${username}" ORDER BY date DESC;` | ||
console.log(sql) | ||
const lists = await this.db.all(sql) | ||
console.log(lists) | ||
return lists | ||
} catch(err) { | ||
console.log(err.message) | ||
throw err | ||
} | ||
} | ||
|
||
async tearDown() { | ||
await this.db.close() | ||
} | ||
} |
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
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
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
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
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