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

+27 -38
View File
@@ -24,7 +24,7 @@ Note.create = (userId, noteText) => {
const created = Math.round((+new Date)/1000)
db.promise()
.query('INSERT INTO notes (user, text, created) VALUES (?,?,?)', [userId, noteText, created])
.query('INSERT INTO note (user_id, text, created) VALUES (?,?,?)', [userId, noteText, created])
.then((rows, fields) => {
resolve(rows[0].insertId) //Only return the new note ID when creating a new note
})
@@ -38,7 +38,7 @@ Note.update = (userId, noteId, noteText, fancyInput, color) => {
const now = Math.round((+new Date)/1000)
db.promise()
.query('UPDATE notes SET text = ?, raw_input = ?, updated = ?, color = ? WHERE id = ? AND user = ? LIMIT 1', [noteText, fancyInput, now, color, noteId, userId])
.query('UPDATE note SET text = ?, raw_input = ?, updated = ?, color = ? WHERE id = ? AND user_id = ? LIMIT 1', [noteText, fancyInput, now, color, noteId, userId])
.then((rows, fields) => {
//Process note text and attachment data
@@ -52,7 +52,7 @@ Note.update = (userId, noteId, noteText, fancyInput, color) => {
'id': noteId,//string - ID of note
'user_id': userId,//int
'note_text': noteText,
'notes_tags': tagString,
'note_tag': tagString,
'attachment_text': attachmentText,
};
// Update document to Solr server
@@ -75,16 +75,12 @@ Note.update = (userId, noteId, noteText, fancyInput, color) => {
Note.delete = (userId, noteId) => {
return new Promise((resolve, reject) => {
// DELETE FROM notes WHERE notes.id = 290 AND notes.user = 61;
// DELETE FROM attachment WHERE attachment.note_id = 290 AND attachment.user_id = 61;
// DELETE FROM notes_tags WHERE notes_tags.note_id = 290 AND notes_tags.user_id = 61;
db.promise().query('DELETE FROM notes WHERE notes.id = ? AND notes.user = ?', [noteId,userId])
db.promise().query('DELETE FROM note WHERE note.id = ? AND note.user_id = ?', [noteId,userId])
.then((rows, fields) => {
db.promise().query('DELETE FROM attachment WHERE attachment.note_id = ? AND attachment.user_id = ?', [noteId,userId])
.then((rows, fields)=> {
db.promise().query('DELETE FROM notes_tags WHERE notes_tags.note_id = ? AND notes_tags.user_id = ?', [noteId,userId])
db.promise().query('DELETE FROM note_tag WHERE note_tag.note_id = ? AND note_tag.user_id = ?', [noteId,userId])
.then((rows, fields)=> {
console.log('All Deleted')
resolve(true)
})
})
@@ -95,7 +91,7 @@ Note.delete = (userId, noteId) => {
Note.get = (userId, noteId) => {
return new Promise((resolve, reject) => {
db.promise()
.query('SELECT text, updated, raw_input, color FROM notes WHERE user = ? AND id = ? LIMIT 1', [userId,noteId])
.query('SELECT text, updated, raw_input, color FROM note WHERE user_id = ? AND id = ? LIMIT 1', [userId,noteId])
.then((rows, fields) => {
resolve(rows[0][0])
})
@@ -103,29 +99,18 @@ Note.get = (userId, noteId) => {
})
}
Note.getLatest = (userId) => {
return new Promise((resolve, reject) => {
db.promise()
.query('SELECT id, SUBSTRING(text, 1, 100) as text FROM notes WHERE user = ? ORDER BY updated DESC, created DESC', [userId])
.then((rows, fields) => {
resolve(rows[0])
})
.catch(console.log)
})
}
Note.solrQuery = (userId, searchQuery, searchTags) => {
return new Promise((resolve, reject) => {
if(searchQuery != '' && searchQuery != null){
let urlQuery = `/solr/note/select?hl.fl=note_text&hl=on&q=user_id:${userId} AND note_text:${searchQuery}&wt=json`
urlQuery = `/solr/note/select?
hl.fl=note_text,attachment_text,notes_tags&
hl.fl=note_text,attachment_text,note_tag&
hl=on&
q=user_id:${userId} AND (note_text:${searchQuery} OR attachment_text:${searchQuery} OR notes_tags:${searchQuery})&
q=user_id:${userId} AND (note_text:${searchQuery} OR attachment_text:${searchQuery} OR note_tag:${searchQuery})&
wt=json&
fl=id&
hl.fl=note_text,attachment_text,notes_tags&
hl.fl=note_text,attachment_text,note_tag&
hl.snippets=20&
hl.maxAnalyzedChars=100000`
@@ -166,16 +151,20 @@ Note.search = (userId, searchQuery, searchTags) => {
//Default note lookup gets all notes
let noteSearchQuery = `
SELECT notes.id, SUBSTRING(notes.text, 1, 400) as text, updated, color, count(distinct notes_tags.id) as tag_count, count(distinct attachment.id) as attachment_count
FROM notes
LEFT JOIN notes_tags ON (notes.id = notes_tags.note_id)
LEFT JOIN attachment ON (notes.id = attachment.note_id AND attachment.attachment_type = 1)
WHERE user = ?`
SELECT note.id,
SUBSTRING(note.text, 1, 400) as text,
updated, color,
count(distinct note_tag.id) as tag_count,
count(distinct attachment.id) as attachment_count
FROM note
LEFT JOIN note_tag ON (note.id = note_tag.note_id)
LEFT JOIN attachment ON (note.id = attachment.note_id AND attachment.attachment_type = 1)
WHERE note.user_id = ?`
let searchParams = [userId]
if(solrNoteIds.length > 0){
searchParams.push(solrNoteIds)
noteSearchQuery += ' AND notes.id IN (?)'
noteSearchQuery += ' AND note.id IN (?)'
}
// if(searchQuery != ''){
@@ -186,11 +175,11 @@ Note.search = (userId, searchQuery, searchTags) => {
if(searchTags.length > 0){
//If tags are passed, use those tags in search
searchParams.push(searchTags)
noteSearchQuery += ' AND notes_tags.tag_id IN (?)'
noteSearchQuery += ' AND note_tag.tag_id IN (?)'
}
//Finish up note query
noteSearchQuery += ' GROUP BY notes.id ORDER BY updated DESC, created DESC, id DESC'
noteSearchQuery += ' GROUP BY note.id ORDER BY updated DESC, created DESC, id DESC'
db.promise()
.query(noteSearchQuery, searchParams)
@@ -243,8 +232,8 @@ Note.search = (userId, searchQuery, searchTags) => {
if(highlights && highlights[note.id] && highlights[note.id].attachment_text){
note['attachment_highlights'] = highlights[note.id].attachment_text
}
if(highlights && highlights[note.id] && highlights[note.id].notes_tags){
note['tag_highlights'] = highlights[note.id].notes_tags
if(highlights && highlights[note.id] && highlights[note.id].note_tag){
note['tag_highlights'] = highlights[note.id].note_tag
}
//Clear out note.text before sending it to front end
@@ -258,11 +247,11 @@ Note.search = (userId, searchQuery, searchTags) => {
//Only show tags of selected notes
db.promise()
.query(`SELECT tags.id, tags.text, count(tags.id) as usages FROM notes_tags
JOIN tags ON (tags.id = notes_tags.tag_id)
WHERE notes_tags.user_id = ?
.query(`SELECT tag.id, tag.text, count(tag.id) as usages FROM note_tag
JOIN tag ON (tag.id = note_tag.tag_id)
WHERE note_tag.user_id = ?
AND note_id IN (?)
GROUP BY tags.id
GROUP BY tag.id
ORDER BY usages DESC;`,[userId, noteIds])
.then((tagRows, tagFields) => {