Notes/src/routes/api/recent_notes.js

52 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-11-04 23:46:50 -04: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');
const sync_table = require('../../services/sync_table');
const options = require('../../services/options');
const wrap = require('express-promise-wrap').wrap;
2017-11-04 23:46:50 -04:00
router.get('', auth.checkApiAuth, wrap(async (req, res, next) => {
2017-11-04 23:46:50 -04:00
res.send(await getRecentNotes());
}));
2017-11-04 23:46:50 -04:00
2018-03-24 21:39:15 -04:00
router.put('/:branchId/:notePath', auth.checkApiAuth, wrap(async (req, res, next) => {
const branchId = req.params.branchId;
2017-11-19 12:06:48 -05:00
const notePath = req.params.notePath;
2018-01-28 21:57:46 -05:00
const sourceId = req.headers.source_id;
await sql.doInTransaction(async () => {
await sql.replace('recent_notes', {
2018-03-24 21:39:15 -04:00
branchId: branchId,
2018-01-28 19:30:14 -05:00
notePath: notePath,
dateAccessed: utils.nowDate(),
isDeleted: 0
});
2017-11-04 23:46:50 -04:00
2018-03-24 21:39:15 -04:00
await sync_table.addRecentNoteSync(branchId, sourceId);
2018-01-28 20:52:05 -05:00
await options.setOption('start_note_path', notePath, sourceId);
});
2017-11-05 00:16:02 -04:00
2017-11-04 23:46:50 -04:00
res.send(await getRecentNotes());
}));
2017-11-04 23:46:50 -04:00
async function getRecentNotes() {
return await sql.getRows(`
SELECT
recent_notes.*
FROM
recent_notes
2018-03-24 21:39:15 -04:00
JOIN branches USING(branchId)
WHERE
2018-01-28 19:30:14 -05:00
recent_notes.isDeleted = 0
2018-03-24 21:39:15 -04:00
AND branches.isDeleted = 0
ORDER BY
dateAccessed DESC
LIMIT 200`);
2017-11-04 23:46:50 -04:00
}
module.exports = router;