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 2019-07-30 20:27:26 +00:00
parent 0d86aa4ff9
commit b2dc6e5218
8 changed files with 111 additions and 77 deletions

View File

@ -232,7 +232,7 @@
setTimeout(() => {
resolve(true)
return
}, 500)
}, 300)
}
const postData = {
@ -256,7 +256,7 @@
vm.lastNoteHash = vm.hashString(vm.noteText)
resolve(true)
})
}, 500)
}, 300)
})
},
@ -335,7 +335,7 @@
}
.size-down {
animation: size-down 0.5s linear both;
animation: size-down 0.5s ease;
}
@keyframes size-down {
@ -345,7 +345,7 @@
}
100% {
opacity: 0;
top: 405vh;
top: 30vh;
}
}

View File

@ -4,10 +4,13 @@
{{ucWords(tag.text)}} <i class="delete icon" v-on:click="removeTag(tag.id)"></i>
</div>
<div class="ui form">
<input v-model="newTagInput" placeholder="Add Tag"
<input
placeholder="Add Tag"
v-model="newTagInput"
v-on:keydown="tagInput"
v-on:keyup="onKeyup"
v-on:blur="onBlur"
v-on:focus="onFocus"
/>
<div class="suggestion-box" v-if="suggestions.length > 0">
<div class="suggestion-item" v-for="(item, index) in suggestions" :class="{ 'active':(index == selection) }" v-on:click="onClickTag(index)">
@ -70,7 +73,6 @@
if(this.selection >= this.suggestions.length){
this.selection = -1 //No selection made
}
console.log('Current Selection Index: ', this.selection)
return
}
//Down - 40 - Go Down Suggestion
@ -79,7 +81,6 @@
if(this.selection < -1){
this.selection = this.suggestions.length -1 //No selection made
}
console.log('Current Selection Index: ', this.selection)
return;
}
@ -137,7 +138,22 @@
vm.getTags()
})
},
onFocus(){
console.log('Focused on tag edit')
//Show suggested tags
let vm = this
let postData = {
'noteId':this.noteId
}
axios.post('/api/tag/latest', postData)
.then(response => {
vm.suggestions = response.data
vm.selection = -1 //Nothing selected
})
},
onKeyup(){
//Clear tags if backspaced
if(this.newTagInput == ''){
this.clearSuggestions()
}

View File

@ -28,12 +28,10 @@
<delete-button :note-id="note.id" />
</div>
</div>
</div>
<!-- Display highlights from solr results -->
<div v-if="note.note_highlights.length > 0" class="term-usage">
<p>Note Text</p>
<p><i class="paragraph icon"></i> Note Text</p>
<div class="usage-row" v-for="highlight in note.note_highlights" v-html="highlight"></div>
</div>
<div v-if="note.attachment_highlights.length > 0" class="term-usage">
@ -46,6 +44,8 @@
</div>
</div>
</div>
</template>
<script>
@ -77,8 +77,9 @@
<style type="text/css">
.term-usage {
border: 1px solid #DDD;
border-top: 1px solid #DDD;
padding: 10px;
width: 100%;
}
.term-usage em {
color: green;

View File

@ -77,7 +77,7 @@
v-for="note in notes"
:onClick="openNote"
:data="note"
:key="note.id + note.color + searchTerm + note.note_highlights.length + note.attachment_highlights.length + ' -' + note.tag_highlights.length + '-' +note.title.length + '-' +note.subtext.length"
:key="note.id + note.color + note.note_highlights.length + note.attachment_highlights.length + ' -' + note.tag_highlights.length + '-' +note.title.length + '-' +note.subtext.length"
/>
</div>

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) => {

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])
@ -145,3 +145,25 @@ 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)
})
}

View File

@ -13,7 +13,7 @@ User.login = (username, password) => {
const lowerName = username.toLowerCase();
db.promise()
.query('SELECT * FROM users WHERE username = ? LIMIT 1', [lowerName])
.query('SELECT * FROM user WHERE username = ? LIMIT 1', [lowerName])
.then((rows, fields) => {
//Pull out user data from database results
@ -58,7 +58,7 @@ User.create = (username, password) => {
return new Promise((resolve, reject) => {
db.promise()
.query('SELECT * FROM users WHERE username = ? LIMIT 1', [lowerName])
.query('SELECT * FROM user WHERE username = ? LIMIT 1', [lowerName])
.then((rows, fields) => {
if(rows[0].length === 0){ //No users returned, create new one. Start with hashing password
@ -88,7 +88,7 @@ User.create = (username, password) => {
};
db.promise()
.query('INSERT INTO users SET ?', new_user)
.query('INSERT INTO user SET ?', new_user)
.then((rows, fields) => {
if(rows[0].affectedRows == 1){

View File

@ -19,6 +19,12 @@ router.post('/suggest', function (req, res) {
.then( data => res.send(data) )
})
//Get latest tags based on when it was put on a note
router.post('/latest', function (req, res) {
Tags.latest(userId, req.body.noteId)
.then( data => res.send(data) )
})
//Get the latest notes the user has created
router.post('/addtonote', function (req, res) {
Tags.addToNote(userId, req.body.noteId, req.body.tagText.toLowerCase())