| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const express = require('express'); | 
					
						
							|  |  |  | const router = express.Router(); | 
					
						
							|  |  |  | const sql = require('../../services/sql'); | 
					
						
							|  |  |  | const auth = require('../../services/auth'); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  | const sync_table = require('../../services/sync_table'); | 
					
						
							|  |  |  | const utils = require('../../services/utils'); | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | const wrap = require('express-promise-wrap').wrap; | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  | const labels = require('../../services/labels'); | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  | router.get('/notes/:noteId/labels', auth.checkApiAuth, wrap(async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     res.send(await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId])); | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  | router.put('/notes/:noteId/labels', auth.checkApiAuth, wrap(async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     const labels = req.body; | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |     const now = utils.nowDate(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |         for (const attr of labels) { | 
					
						
							|  |  |  |             if (attr.labelId) { | 
					
						
							|  |  |  |                 await sql.execute("UPDATE labels SET name = ?, value = ?, dateModified = ?, isDeleted = ?, position = ? WHERE labelId = ?", | 
					
						
							|  |  |  |                     [attr.name, attr.value, now, attr.isDeleted, attr.position, attr.labelId]); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							| 
									
										
										
										
											2018-02-06 23:09:19 -05:00
										 |  |  |                 // if it was "created" and then immediatelly deleted, we just don't create it at all
 | 
					
						
							|  |  |  |                 if (attr.isDeleted) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |                 attr.labelId = utils.newLabelId(); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |                 await sql.insert("labels", { | 
					
						
							|  |  |  |                     labelId: attr.labelId, | 
					
						
							| 
									
										
										
										
											2018-02-10 08:37:14 -05:00
										 |  |  |                     noteId: noteId, | 
					
						
							|  |  |  |                     name: attr.name, | 
					
						
							|  |  |  |                     value: attr.value, | 
					
						
							|  |  |  |                     position: attr.position, | 
					
						
							|  |  |  |                     dateCreated: now, | 
					
						
							|  |  |  |                     dateModified: now, | 
					
						
							|  |  |  |                     isDeleted: false | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |                 }); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             await sync_table.addLabelSync(attr.labelId); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     res.send(await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId])); | 
					
						
							| 
									
										
										
										
											2018-01-11 21:40:09 -05:00
										 |  |  | })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  | router.get('/labels/names', auth.checkApiAuth, wrap(async (req, res, next) => { | 
					
						
							|  |  |  |     const names = await sql.getColumn("SELECT DISTINCT name FROM labels WHERE isDeleted = 0"); | 
					
						
							| 
									
										
										
										
											2018-02-04 19:27:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     for (const attr of labels.BUILTIN_LABELS) { | 
					
						
							| 
									
										
										
										
											2018-02-04 19:27:27 -05:00
										 |  |  |         if (!names.includes(attr)) { | 
					
						
							|  |  |  |             names.push(attr); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     names.sort(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.send(names); | 
					
						
							|  |  |  | })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  | router.get('/labels/values/:labelName', auth.checkApiAuth, wrap(async (req, res, next) => { | 
					
						
							|  |  |  |     const labelName = req.params.labelName; | 
					
						
							| 
									
										
										
										
											2018-02-04 19:43:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     const values = await sql.getColumn("SELECT DISTINCT value FROM labels WHERE isDeleted = 0 AND name = ? AND value != '' ORDER BY value", [labelName]); | 
					
						
							| 
									
										
										
										
											2018-02-04 19:43:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send(values); | 
					
						
							|  |  |  | })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | module.exports = router; |