/** * Created by WebStorm. * User: martin * Date: 16/04/2020 * Time: 10:00 */ const db = require('./connect'); function prepareData(_obj) { const newObj = Object.assign({}, _obj); newObj.isEasyApply = (_obj.isEasyApply) ? 1 : 0; return newObj; } exports.insertOne = (data) => { const sql = 'INSERT INTO jobs VALUES (?,?,?,?,?,?,?,?,?,?,?,?)'; const workObj = prepareData(data); return new Promise((resolve, reject) => { db.run(sql, [null, workObj.title, workObj.site, workObj.url, workObj.id, workObj.summary, workObj.company, workObj.location, workObj.postDate, workObj.salary, workObj.isEasyApply, workObj.timestamp], function(err) { if (err) reject(err); resolve({ 'msg':'Row inserted', '_id': this.lastID }); }); }); }; exports.getList = () => { const outgoing = []; // const sql = 'select _id, title, site, company, timestamp from jobs order by _id desc'; const sql = 'select * from jobsList'; return new Promise((resolve, reject) => { db.all(sql, [], (err, rows) => { if (err) reject(err); rows.forEach((row) => { outgoing.push(row); }); resolve(outgoing) ; }); }); }; exports.getOne = (id) => { const sql = 'SELECT jobs.*, applied.a FROM jobs left join applied on applied.aid = jobs._id WHERE jobs._id == ?'; return new Promise((resolve, reject) => { db.get(sql, [id], (err, row) => { if (err) reject(err); if (!err) resolve(row); }); }); }; exports.touchOne = (data) => { const sql = 'INSERT INTO read VALUES (?,?,?)'; const { rid, d } = data; return new Promise((resolve, reject) => { db.run(sql, [null, rid, d], function(err) { if (err && err.errno !== 19) { console.log(err); reject(err); } resolve({}); }); }); }; exports.appliedOne = (data) => { const sql = 'INSERT INTO applied VALUES (?,?,?)'; const { aid, a } = data; return new Promise((resolve, reject) => { db.run(sql, [null, aid, a], function(err) { if (err && err.errno !== 19) { console.log(err); reject(err); } resolve({}); }); }); }; exports.markAllRead = () => { const sql = 'insert into read (rid, d) select jobs._id as rid, strftime(\'%s\',\'now\') as d from jobs left join read on read.rid = jobs._id where read.d is null order by jobs._id desc;'; return new Promise((resolve, reject) => { db.run(sql, [], function(err) { if (err && err.errno !== 19) { console.log(err); reject(err); } resolve({}); }); }); }; // select _id, title, site, company, timestamp from jobs order by _id desc;