2019-07-19 13:51:57 -07:00
|
|
|
let db = require('@config/database')
|
|
|
|
|
|
|
|
let Tags = module.exports = {}
|
|
|
|
|
|
|
|
|
|
|
|
Tags.removeTagFromNote = (userId, tagId) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.promise()
|
|
|
|
.query(`DELETE FROM notes_tags WHERE id = ? AND user_id = ? LIMIT 1;`, [tagId, userId])
|
|
|
|
.then((rows, fields) => {
|
|
|
|
resolve(rows[0]) //Return new ID
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
Tags.addToNote = (userId, noteId, tagText) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
//Lookup tag
|
|
|
|
Tags.lookup(tagText)
|
|
|
|
.then( lookup => {
|
|
|
|
|
|
|
|
//Tag does not exist, insert new tag, then associate it with a note
|
|
|
|
if(lookup.length == 0){
|
|
|
|
//Insert new tag
|
|
|
|
Tags.add(tagText)
|
|
|
|
.then( newTagId => {
|
|
|
|
Tags.associateWithNote(userId, noteId, newTagId)
|
|
|
|
.then( result => {
|
|
|
|
resolve(result)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//Tag already exists, associate it with a note
|
|
|
|
if(lookup.length > 0){
|
|
|
|
Tags.associateWithNote(userId, noteId, lookup[0].id)
|
|
|
|
.then( result => {
|
|
|
|
resolve(result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
Tags.associateWithNote = (userId, noteId, tagId) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
//Check if tag already exists on note before adding note
|
|
|
|
db.promise()
|
|
|
|
.query(`SELECT * FROM notes_tags WHERE note_id = ? AND tag_id = ? AND user_id = ?;`, [noteId, tagId, userId])
|
|
|
|
.then((rows, fields) => {
|
|
|
|
|
|
|
|
//If matching tag does not exist on note
|
|
|
|
if(rows[0].length == 0){
|
|
|
|
|
|
|
|
//Add tag to note
|
|
|
|
db.promise()
|
|
|
|
.query(`INSERT INTO notes_tags (note_id, tag_id, user_id) VALUES (?,?,?);`, [noteId, tagId, userId])
|
|
|
|
.then((rows, fields) => {
|
|
|
|
resolve(rows[0])
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
} else {
|
|
|
|
|
|
|
|
reject('Error: Tag already exists on note.')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
Tags.add = (tagText) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.promise()
|
|
|
|
.query(`INSERT INTO tags (text, hash) VALUES (?,?);`, [tagText,0])
|
|
|
|
.then((rows, fields) => {
|
|
|
|
resolve(rows[0].insertId) //Return new ID
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
Tags.get = (userId, noteId) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.promise()
|
|
|
|
.query(`SELECT notes_tags.id, tags.text FROM notes_tags
|
|
|
|
JOIN tags ON (tags.id = notes_tags.tag_id)
|
|
|
|
WHERE user_id = ? AND note_id = ?;`, [userId, noteId])
|
|
|
|
.then((rows, fields) => {
|
|
|
|
resolve(rows[0]) //Return all tags found by query
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:06:50 -07:00
|
|
|
Tags.string = (userId, noteId) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Tags.get(userId, noteId).then(tagArray => {
|
|
|
|
|
|
|
|
let tagString = ''
|
|
|
|
tagArray.forEach( (tag, i) => {
|
|
|
|
if(i > 0){ tagString += ',' }
|
|
|
|
tagString += tag.text
|
|
|
|
})
|
|
|
|
//Output comma delimited list of tag strings
|
|
|
|
resolve(tagString)
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-19 13:51:57 -07:00
|
|
|
Tags.lookup = (tagText) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.promise()
|
|
|
|
.query(`SELECT * FROM tags WHERE text = ?;`, [tagText])
|
|
|
|
.then((rows, fields) => {
|
|
|
|
resolve(rows[0]) //Return all tags found by query
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
2019-07-21 09:28:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//Suggest note tags - don't suggest tags already on note
|
|
|
|
Tags.suggest = (userId, noteId, tagText) => {
|
|
|
|
|
|
|
|
tagText += '%'
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.promise()
|
|
|
|
.query(`SELECT text FROM notes_tags
|
|
|
|
JOIN tags ON notes_tags.tag_id = tags.id
|
|
|
|
WHERE notes_tags.user_id = ?
|
|
|
|
AND tags.text LIKE ?
|
|
|
|
AND notes_tags.tag_id NOT IN (
|
|
|
|
SELECT notes_tags.tag_id FROM notes_tags WHERE notes_tags.note_id = ?
|
|
|
|
)
|
|
|
|
GROUP BY text
|
|
|
|
LIMIT 6;`, [userId, tagText, noteId])
|
|
|
|
.then((rows, fields) => {
|
|
|
|
resolve(rows[0]) //Return new ID
|
|
|
|
})
|
|
|
|
.catch(console.log)
|
|
|
|
})
|
2019-07-19 13:51:57 -07:00
|
|
|
}
|