Big Refactor of all SQL calls to comply with database changes

Added tag suggestions when entering tag field
Cleaned up animations to make them REAL smooth
This commit is contained in:
Max G committed 2019-07-30 20:27:26 +00:00
1 parent 0d86aa4ff9
commit b2dc6e5218
8 files changed
+111 -77

No files matched your search

+35 -13
View File
@@ -6,7 +6,7 @@ let Tag = module.exports = {}
Tag.removeTagFromNote = (userId, tagId) => {
return new Promise((resolve, reject) => {
db.promise()
.query(`DELETE FROM notes_tags WHERE id = ? AND user_id = ? LIMIT 1;`, [tagId, userId])
.query(`DELETE FROM note_tag WHERE id = ? AND user_id = ? LIMIT 1;`, [tagId, userId])
.then((rows, fields) => {
resolve(rows[0]) //Return new ID
})
@@ -50,7 +50,7 @@ Tag.associateWithNote = (userId, noteId, tagId) => {
//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])
.query(`SELECT * FROM note_tag WHERE note_id = ? AND tag_id = ? AND user_id = ?;`, [noteId, tagId, userId])
.then((rows, fields) => {
//If matching tag does not exist on note
@@ -58,7 +58,7 @@ Tag.associateWithNote = (userId, noteId, tagId) => {
//Add tag to note
db.promise()
.query(`INSERT INTO notes_tags (note_id, tag_id, user_id) VALUES (?,?,?);`, [noteId, tagId, userId])
.query(`INSERT INTO note_tag (note_id, tag_id, user_id) VALUES (?,?,?);`, [noteId, tagId, userId])
.then((rows, fields) => {
resolve(rows[0])
})
@@ -75,7 +75,7 @@ Tag.associateWithNote = (userId, noteId, tagId) => {
Tag.add = (tagText) => {
return new Promise((resolve, reject) => {
db.promise()
.query(`INSERT INTO tags (text, hash) VALUES (?,?);`, [tagText,0])
.query(`INSERT INTO tag (text, hash) VALUES (?,?);`, [tagText,0])
.then((rows, fields) => {
resolve(rows[0].insertId) //Return new ID
})
@@ -86,8 +86,8 @@ Tag.add = (tagText) => {
Tag.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)
.query(`SELECT note_tag.id, tag.text FROM note_tag
JOIN tag ON (tag.id = note_tag.tag_id)
WHERE user_id = ? AND note_id = ?;`, [userId, noteId])
.then((rows, fields) => {
resolve(rows[0]) //Return all tags found by query
@@ -115,7 +115,7 @@ Tag.string = (userId, noteId) => {
Tag.lookup = (tagText) => {
return new Promise((resolve, reject) => {
db.promise()
.query(`SELECT * FROM tags WHERE text = ?;`, [tagText])
.query(`SELECT * FROM tag WHERE text = ?;`, [tagText])
.then((rows, fields) => {
resolve(rows[0]) //Return all tags found by query
})
@@ -130,12 +130,12 @@ Tag.suggest = (userId, noteId, 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 = ?
.query(`SELECT text FROM note_tag
JOIN tag ON note_tag.tag_id = tag.id
WHERE note_tag.user_id = ?
AND tag.text LIKE ?
AND note_tag.tag_id NOT IN (
SELECT note_tag.tag_id FROM note_tag WHERE note_tag.note_id = ?
)
GROUP BY text
LIMIT 6;`, [userId, tagText, noteId])
@@ -144,4 +144,26 @@ Tag.suggest = (userId, noteId, tagText) => {
})
.catch(console.log)
})
}
//Suggest note tags - don't suggest tags already on note
Tag.latest = (userId, noteId) => {
return new Promise((resolve, reject) => {
db.promise()
.query(`SELECT tag.text FROM note_tag
JOIN tag ON note_tag.tag_id = tag.id
JOIN note ON note_tag.note_id = note.id
WHERE note_tag.user_id = ?
AND note_tag.tag_id NOT IN (
SELECT note_tag.tag_id FROM note_tag WHERE note_tag.note_id = ?
)
GROUP BY tag.text, note.updated
ORDER BY note.updated DESC
LIMIT 6;`, [userId, noteId])
.then((rows, fields) => {
resolve(rows[0]) //Return new ID
})
.catch(console.log)
})
}