| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | const express = require('express'); | 
					
						
							|  |  |  | const router = express.Router(); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | const auth = require('../../services/auth'); | 
					
						
							| 
									
										
										
										
											2017-10-15 19:47:05 -04:00
										 |  |  | const sql = require('../../services/sql'); | 
					
						
							| 
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 |  |  | const options = require('../../services/options'); | 
					
						
							| 
									
										
										
										
											2017-10-15 19:47:05 -04:00
										 |  |  | const utils = require('../../services/utils'); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | const notes = require('../../services/notes'); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-15 16:32:49 -04:00
										 |  |  | router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     let noteId = req.params.noteId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-03 20:50:48 -04:00
										 |  |  |     await options.setOption('start_node', noteId); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let detail = await sql.getSingleResult("select * from notes where note_id = ?", [noteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |     if (detail.note_clone_id) { | 
					
						
							|  |  |  |         noteId = detail.note_clone_id; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |         detail = sql.getSingleResult("select * from notes where note_id = ?", [noteId]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.send({ | 
					
						
							| 
									
										
										
										
											2017-11-04 21:02:56 -04:00
										 |  |  |         detail: detail, | 
					
						
							|  |  |  |         images: await sql.getResults("select * from images where note_id = ? order by note_offset", [noteId]), | 
					
						
							|  |  |  |         loadTime: utils.nowTimestamp() | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | router.post('/:parentNoteId/children', async (req, res, next) => { | 
					
						
							|  |  |  |     let parentNoteId = req.params.parentNoteId; | 
					
						
							|  |  |  |     const browserId = utils.browserId(req); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     const note = req.body; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     const noteId = await notes.createNewNote(parentNoteId, note, browserId); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     res.send({ | 
					
						
							|  |  |  |         'note_id': noteId | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | router.put('/:noteId', async (req, res, next) => { | 
					
						
							|  |  |  |     const newNote = req.body; | 
					
						
							|  |  |  |     let noteId = req.params.noteId; | 
					
						
							|  |  |  |     const browserId = utils.browserId(req); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await notes.updateNote(noteId, newNote, browserId); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | router.delete('/:noteId', async (req, res, next) => { | 
					
						
							|  |  |  |     const browserId = utils.browserId(req); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |         await notes.deleteNote(req.params.noteId, browserId); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     res.send({}); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | router.get('/', async (req, res, next) => { | 
					
						
							|  |  |  |     const search = '%' + req.query.search + '%'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const result = await sql.getResults("select note_id from notes where note_title like ? or note_text like ?", [search, search]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const noteIdList = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const res of result) { | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |         noteIdList.push(res.note_id); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.send(noteIdList); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = router; |