diff --git a/seasoned_api/src/seasoned/stray.js b/seasoned_api/src/seasoned/stray.js index ce2218f..f33e2ac 100644 --- a/seasoned_api/src/seasoned/stray.js +++ b/seasoned_api/src/seasoned/stray.js @@ -1,7 +1,7 @@ class Stray { - constructor(id) { - this.id = id; - } + constructor(id) { + this.id = id; + } } -module.exports = Stray; \ No newline at end of file +module.exports = Stray; diff --git a/seasoned_api/src/seasoned/strayRepository.js b/seasoned_api/src/seasoned/strayRepository.js index 7d439b0..05ae66f 100644 --- a/seasoned_api/src/seasoned/strayRepository.js +++ b/seasoned_api/src/seasoned/strayRepository.js @@ -1,67 +1,62 @@ const assert = require('assert'); const Stray = require('src/seasoned/stray'); const establishedDatabase = require('src/database/database'); -var pythonShell = require('python-shell'); - -function foo(e) { - throw('Foooo'); -} +const pythonShell = require('python-shell'); class StrayRepository { + constructor(database) { + this.database = database || establishedDatabase; + this.queries = { + read: 'SELECT * FROM stray_eps WHERE id = ?', + readAll: 'SELECT id, name, season, episode, verified FROM stray_eps', + readAllFiltered: 'SELECT id, name, season, episode, verified FROM stray_eps WHERE verified = ', + checkVerified: 'SELECT id FROM stray_eps WHERE verified = 0 AND id = ?', + verify: 'UPDATE stray_eps SET verified = 1 WHERE id = ?', + }; + } - constructor(database) { - this.database = database || establishedDatabase; - this.queries = { - 'read': 'SELECT * FROM stray_eps WHERE id = ?', - 'readAll': 'SELECT id, name, season, episode, verified FROM stray_eps', - 'readAllFiltered': 'SELECT id, name, season, episode, verified FROM stray_eps WHERE verified = ', - 'checkVerified': 'SELECT id FROM stray_eps WHERE verified = 0 AND id = ?', - 'verify': 'UPDATE stray_eps SET verified = 1 WHERE id = ?', - }; - } + read(strayId) { + return this.database.get(this.queries.read, strayId).then((row) => { + assert.notEqual(row, undefined, `Could not find list with id ${strayId}.`); + return row; + }); + } - read(strayId) { - return this.database.get(this.queries.read, strayId).then((row) => { - assert.notEqual(row, undefined, `Could not find list with id ${strayId}.`); - return row; - }) - } + readAll(verified = null) { + let dbSearchQuery = this.queries.readAll; + if (verified != null) { + dbSearchQuery = this.queries.readAllFiltered + verified.toString(); + } + return this.database.all(dbSearchQuery).then(rows => + rows.map((row) => { + const stray = new Stray(row.id); + stray.name = row.name; + stray.season = row.season; + stray.episode = row.episode; + stray.verified = row.verified; + return stray; + })); + } - readAll(verified = null, page = 1) { - var dbSearchQuery = this.queries.readAll; - if (verified != null) { - dbSearchQuery = this.queries.readAllFiltered + verified.toString(); - } - return this.database.all(dbSearchQuery).then(rows => - rows.map((row) => { - const stray = new Stray(row.id); - stray.name = row.name; - stray.season = row.season; - stray.episode = row.episode; - stray.verified = row.verified; - return stray; - })) - } + verifyStray(strayId) { + return this.database.get(this.queries.checkVerified, strayId).then((row) => { + assert.notEqual(row, undefined, `Stray '${strayId}' already verified.`); - verifyStray(strayId) { - return this.database.get(this.queries.checkVerified, strayId).then((row) => { - assert.notEqual(row, undefined, `Stray '${strayId}' already verified.`); + const options = { + pythonPath: '/usr/bin/python3', + args: [strayId], + }; - var options = { - pythonPath: '/usr/bin/python3', - args: [strayId] - } + pythonShell.run('../app/moveSeasoned.py', options, (err, results) => { + if (err) throw err; + // TODO Add error handling!! StrayRepository.ERROR + // results is an array consisting of messages collected during execution + console.log('results: %j', results); + }); - pythonShell.run('../app/moveSeasoned.py', options, function (err, results) { - if (err) throw err; - // TODO Add error handling!! StrayRepository.ERROR - // results is an array consisting of messages collected during execution - console.log('results: %j', results); - }); - - return this.database.run(this.queries.verify, strayId); - }) - } + return this.database.run(this.queries.verify, strayId); + }); + } } module.exports = StrayRepository;