jobscraper/lib/dbmanager.js
Martin Donnelly 7c424a069e Added view
Added read marking
2020-05-25 10:21:56 +01:00

84 lines
1.8 KiB
JavaScript

/**
* 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 * FROM jobs WHERE _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({});
});
});
};
// select _id, title, site, company, timestamp from jobs order by _id desc;