| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const sql = require('../../services/sql'); | 
					
						
							|  |  |  | const attributeService = require('../../services/attributes'); | 
					
						
							|  |  |  | const repository = require('../../services/repository'); | 
					
						
							|  |  |  | const Attribute = require('../../entities/attribute'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getNoteAttributes(req) { | 
					
						
							|  |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return await repository.getEntities("SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function updateNoteAttributes(req) { | 
					
						
							|  |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  |     const attributes = req.body; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const attribute of attributes) { | 
					
						
							|  |  |  |         let attributeEntity; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (attribute.attributeId) { | 
					
						
							|  |  |  |             attributeEntity = await repository.getAttribute(attribute.attributeId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             // if it was "created" and then immediatelly deleted, we just don't create it at all
 | 
					
						
							|  |  |  |             if (attribute.isDeleted) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             attributeEntity = new Attribute(); | 
					
						
							|  |  |  |             attributeEntity.noteId = noteId; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-05 20:08:56 +02:00
										 |  |  |         attributeEntity.type = attribute.type; | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  |         attributeEntity.name = attribute.name; | 
					
						
							|  |  |  |         attributeEntity.value = attribute.value; | 
					
						
							|  |  |  |         attributeEntity.position = attribute.position; | 
					
						
							| 
									
										
										
										
											2018-08-05 20:08:56 +02:00
										 |  |  |         attributeEntity.isInheritable = attribute.isInheritable; | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  |         attributeEntity.isDeleted = attribute.isDeleted; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await attributeEntity.save(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return await repository.getEntities("SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-03 11:11:57 +02:00
										 |  |  | async function getAttributeNames(req) { | 
					
						
							|  |  |  |     const type = req.query.type; | 
					
						
							|  |  |  |     const query = req.query.query; | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-03 11:11:57 +02:00
										 |  |  |     return attributeService.getAttributeNames(type, query); | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getValuesForAttribute(req) { | 
					
						
							|  |  |  |     const attributeName = req.params.attributeName; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return await sql.getColumn("SELECT DISTINCT value FROM attributes WHERE isDeleted = 0 AND name = ? AND value != '' ORDER BY value", [attributeName]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     getNoteAttributes, | 
					
						
							|  |  |  |     updateNoteAttributes, | 
					
						
							| 
									
										
										
										
											2018-08-03 11:11:57 +02:00
										 |  |  |     getAttributeNames, | 
					
						
							| 
									
										
										
										
											2018-08-02 22:48:21 +02:00
										 |  |  |     getValuesForAttribute | 
					
						
							|  |  |  | }; |