| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | const sync_table = require('../../services/sync_table'); | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  | const options = require('../../services/options'); | 
					
						
							| 
									
										
										
										
											2017-11-04 23:46:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | router.get('', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							|  |  |  |     res.send(await getRecentNotes()); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 10:06:53 -05:00
										 |  |  | router.put('/:noteTreeId/:notePath', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							|  |  |  |     const noteTreeId = req.params.noteTreeId; | 
					
						
							| 
									
										
										
										
											2017-11-19 12:06:48 -05:00
										 |  |  |     const notePath = req.params.notePath; | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |     const sourceId = req.headers.source_id; | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |         await sql.replace('recent_notes', { | 
					
						
							| 
									
										
										
										
											2017-12-03 10:06:53 -05:00
										 |  |  |             note_tree_id: noteTreeId, | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:53 -05:00
										 |  |  |             note_path: notePath, | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |             date_accessed: utils.nowDate(), | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:53 -05:00
										 |  |  |             is_deleted: 0 | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-11-04 23:46:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await sync_table.addRecentNoteSync(noteTreeId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await options.setOption('start_note_path', notePath, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:53 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-05 00:16:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 23:46:50 -04:00
										 |  |  |     res.send(await getRecentNotes()); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getRecentNotes() { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     return await sql.getAll(`
 | 
					
						
							| 
									
										
										
										
											2017-12-18 22:37:31 -05:00
										 |  |  |       SELECT  | 
					
						
							|  |  |  |         recent_notes.*  | 
					
						
							|  |  |  |       FROM  | 
					
						
							|  |  |  |         recent_notes | 
					
						
							|  |  |  |         JOIN notes_tree USING(note_tree_id) | 
					
						
							|  |  |  |       WHERE | 
					
						
							|  |  |  |         recent_notes.is_deleted = 0 | 
					
						
							|  |  |  |         AND notes_tree.is_deleted = 0 | 
					
						
							|  |  |  |       ORDER BY  | 
					
						
							|  |  |  |         date_accessed DESC`);
 | 
					
						
							| 
									
										
										
										
											2017-11-04 23:46:50 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = router; |