2017-11-05 11:46:50 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const auth = require('../../services/auth');
|
|
|
|
const utils = require('../../services/utils');
|
2017-11-17 10:50:00 +08:00
|
|
|
const sync_table = require('../../services/sync_table');
|
2017-11-19 06:05:50 +08:00
|
|
|
const options = require('../../services/options');
|
2018-01-07 22:35:44 +08:00
|
|
|
const wrap = require('express-promise-wrap').wrap;
|
2017-11-05 11:46:50 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.get('', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-11-05 11:46:50 +08:00
|
|
|
res.send(await getRecentNotes());
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-11-05 11:46:50 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.put('/:noteTreeId/:notePath', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-12-03 23:06:53 +08:00
|
|
|
const noteTreeId = req.params.noteTreeId;
|
2017-11-20 01:06:48 +08:00
|
|
|
const notePath = req.params.notePath;
|
2018-01-29 10:57:46 +08:00
|
|
|
const sourceId = req.headers.source_id;
|
2017-11-19 06:05:50 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
await sql.replace('recent_notes', {
|
2018-01-29 08:30:14 +08:00
|
|
|
noteTreeId: noteTreeId,
|
|
|
|
notePath: notePath,
|
|
|
|
dateAccessed: utils.nowDate(),
|
|
|
|
isDeleted: 0
|
2017-11-24 13:09:53 +08:00
|
|
|
});
|
2017-11-05 11:46:50 +08:00
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addRecentNoteSync(noteTreeId, sourceId);
|
2017-11-19 06:05:50 +08:00
|
|
|
|
2018-01-29 09:52:05 +08:00
|
|
|
await options.setOption('start_note_path', notePath, sourceId);
|
2017-11-24 13:09:53 +08:00
|
|
|
});
|
2017-11-05 12:16:02 +08:00
|
|
|
|
2017-11-05 11:46:50 +08:00
|
|
|
res.send(await getRecentNotes());
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-11-05 11:46:50 +08:00
|
|
|
|
|
|
|
async function getRecentNotes() {
|
2018-01-30 06:41:59 +08:00
|
|
|
return await sql.getRows(`
|
2017-12-19 11:37:31 +08:00
|
|
|
SELECT
|
|
|
|
recent_notes.*
|
|
|
|
FROM
|
|
|
|
recent_notes
|
2018-01-29 08:38:05 +08:00
|
|
|
JOIN note_tree USING(noteTreeId)
|
2017-12-19 11:37:31 +08:00
|
|
|
WHERE
|
2018-01-29 08:30:14 +08:00
|
|
|
recent_notes.isDeleted = 0
|
2018-01-29 08:38:05 +08:00
|
|
|
AND note_tree.isDeleted = 0
|
2017-12-19 11:37:31 +08:00
|
|
|
ORDER BY
|
2018-01-29 08:30:14 +08:00
|
|
|
dateAccessed DESC`);
|
2017-11-05 11:46:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = router;
|