2020-05-19 09:05:04 +00:00
/ * *
* 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 = [ ] ;
2020-05-25 09:21:56 +00:00
// const sql = 'select _id, title, site, company, timestamp from jobs order by _id desc';
const sql = 'select * from jobsList' ;
2020-05-19 09:05:04 +00:00
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 ) => {
2020-05-25 13:51:52 +00:00
const sql = 'SELECT jobs.*, applied.a FROM jobs left join applied on applied.aid = jobs._id WHERE jobs._id == ?' ;
2020-05-19 09:05:04 +00:00
return new Promise ( ( resolve , reject ) => {
db . get ( sql , [ id ] , ( err , row ) => {
if ( err )
reject ( err ) ;
if ( ! err ) resolve ( row ) ;
} ) ;
} ) ;
} ;
2020-05-25 09:21:56 +00:00
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 ( { } ) ;
} ) ;
} ) ;
} ;
2020-05-19 09:05:04 +00:00
2020-05-25 13:51:52 +00:00
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 ( { } ) ;
} ) ;
} ) ;
} ;
2020-05-26 08:31:51 +00:00
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 ( { } ) ;
} ) ;
} ) ;
} ;
2020-05-19 09:05:04 +00:00
// select _id, title, site, company, timestamp from jobs order by _id desc;