exports.delete = function (db, req, res) {
exports.parseReceivedData(req, function (work) {
db.query(
'delete from work where id = ?',
[work.id],
function (err) {
if (err) throw err ;
exports.show( db, res ) ;
}
) ;
} ) ;
} ;
6. 更新MySQL数据
为了实现更新工作记录的逻辑,将它标记为已归档,把下面的代码添加到timetrack.js中。
exports.archive = function (db, req, res) {
exports.parseReceivedData( req, function (work) {
db.query(
'update work set archived = 1 where id = ?',
[work.id],
function (err) {
if (err) throw err ;
exports.show( db, res ) ;
}
) ;
} ) ;
} ;
exports.show = function (db, res, showArchived) {
console.log( 'in show function' ) ;
var query = 'select * from work ' +
'where archived = ? ' +
'order by date desc ' ;
var archiveValue = (showArchived) ? 1 : 0 ;
console.log( 'archiveValue:' + archiveValue ) ;
db.query(
query,
[archiveValue],
function (err, rows) {
console.log( rows ) ;
if (err) throw err ;
html = (showArchived)
? ''
: 'Archived Work
' ;
html += exports.workHitlistHtml( rows ) ;
html += exports.workFormHtml() ;
exports.sendHtml(res, html) ;
}
) ;
} ;
exports.showArchived = function (db, res) {
exports.show(db, res, true) ;
}
8. 渲染MySQL记录
将下面代码清单中的代码添加到timetrack.js中。它会将工作记录渲染为HTML。
exports.workHitlistHtml = function (rows) {
var html = '' ;
for( var i in rows ) {
html += '' ;
html += ''
html += ''
html += ''
if ( !rows[i].archived ) {
html += ''
}
html += '' ;
}
html += '
' + rows[i].date + '
' + rows[i].hours + '
' + rows[i].description + '
' + exports.workArchiveForm( rows[ i ].id ) + '