From 9bf99436bcac8c7aafbb713f8eb48604cd309773 Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Tue, 12 Nov 2019 00:10:03 -0300 Subject: [PATCH] Missiong update on note in grid, but now you can create tags in the modal.. =) --- controller/notecontroller.php | 34 ++++++++++++++++++++++++++++++++-- db/notetag.php | 2 +- db/notetagmapper.php | 17 ++++++++++++++++- db/tagmapper.php | 21 ++++++++++++++++++--- js/script.js | 12 ++++++++---- 5 files changed, 75 insertions(+), 11 deletions(-) diff --git a/controller/notecontroller.php b/controller/notecontroller.php index 77e3f90..4fb0919 100644 --- a/controller/notecontroller.php +++ b/controller/notecontroller.php @@ -72,7 +72,7 @@ class NoteController extends Controller { } else { $note->setSharedWith(null); } - $note->setTags($this->tagmapper->getTagsForNote($note->getId())); + $note->setTags($this->tagmapper->getTagsForNote($this->userId, $note->getId())); } $shareEntries = $this->notesharemapper->findForUser($this->userId); $shares = array(); @@ -147,7 +147,7 @@ class NoteController extends Controller { * @param string $content * @param string $color */ - public function update($id, $title, $content, $color = "#F7EB96") { + public function update($id, $title, $content, $tags, $color = "#F7EB96") { // Get current Note and Color. try { $note = $this->notemapper->find($id, $this->userId); @@ -165,6 +165,36 @@ class NoteController extends Controller { $hcolor = $this->colormapper->insert($hcolor); } + // Delete old tag relations + $noteTags = $this->tagmapper->getTagsForNote($this->userId, $id); + foreach ($noteTags as $tag) { + if (!in_array($tag->getName(), $tags)) { + $hnotetag = $this->notetagmapper->findNoteTag($this->userId, $id, $tag->getId()); + $this->notetagmapper->delete($hnotetag); + } + } + + // Add new tags and update relations. + foreach ($tags as $name) { + if (!$this->tagmapper->tagExists($this->userId, $name)) { + $htag = new Tag(); + $htag->setName($name); + $htag->setUserId($this->userId); + $htag = $this->tagmapper->insert($htag); + } + else { + $htag = $this->tagmapper->getTag($this->userId, $name); + } + + if (!$this->notetagmapper->noteTagExists($this->userId, $id, $htag->getId())) { + $noteTag = new NoteTag(); + $noteTag->setNoteId($id); + $noteTag->setTagId($htag->getId()); + $noteTag->setUserId($this->userId); + $this->notetagmapper->insert($noteTag); + } + } + // Set new info on Note $note->setTitle($title); $note->setContent($content); diff --git a/db/notetag.php b/db/notetag.php index dd9374d..c68d5de 100644 --- a/db/notetag.php +++ b/db/notetag.php @@ -8,7 +8,7 @@ use OCP\AppFramework\Db\Entity; class NoteTag extends Entity implements JsonSerializable { protected $noteId; - protected $tagId + protected $tagId; protected $userId; public function jsonSerialize() { diff --git a/db/notetagmapper.php b/db/notetagmapper.php index d540eff..a5f4b5a 100644 --- a/db/notetagmapper.php +++ b/db/notetagmapper.php @@ -7,7 +7,7 @@ use OCP\AppFramework\Db\Mapper; class NoteTagMapper extends Mapper { public function __construct(IDBConnection $db) { - parent::__construct($db, 'quicknotes_note_tags', '\OCA\QuickNotes\Db\Tag'); + parent::__construct($db, 'quicknotes_note_tags', '\OCA\QuickNotes\Db\NoteTag'); } public function find($id, $userId) { @@ -20,4 +20,19 @@ class NoteTagMapper extends Mapper { return $this->findEntities($sql, [$userId]); } + public function findNoteTag($userId, $noteId, $tagId) { + $sql = 'SELECT * FROM *PREFIX*quicknotes_note_tags WHERE user_id = ? AND note_id = ? AND tag_id = ?'; + return $this->findEntity($sql, [$userId, $noteId, $tagId]); + } + + public function noteTagExists($userId, $noteId, $tagId) { + $sql = 'SELECT * FROM *PREFIX*quicknotes_note_tags WHERE user_id = ? AND note_id = ? AND tag_id = ?'; + try { + return $this->findEntities($sql, [$userId, $noteId, $tagId]); + } catch (DoesNotExistException $e) { + return false; + } + return true; + } + } \ No newline at end of file diff --git a/db/tagmapper.php b/db/tagmapper.php index db35572..4cf5927 100644 --- a/db/tagmapper.php +++ b/db/tagmapper.php @@ -20,12 +20,27 @@ class TagMapper extends Mapper { return $this->findEntities($sql, [$userId]); } - public function getTagsForNote ($noteId) { + public function getTagsForNote($userId, $noteId) { $sql = 'SELECT T.id, T.name FROM *PREFIX*quicknotes_tags T '; $sql.= 'INNER JOIN *PREFIX*quicknotes_note_tags NT '; $sql.= 'ON T.id = NT.tag_id '; - $sql.= 'WHERE NT.note_id = ?'; - return $this->findEntities($sql, [$noteId]); + $sql.= 'WHERE NT.user_id = ? AND NT.note_id = ?'; + return $this->findEntities($sql, [$userId, $noteId]); + } + + public function getTag($userId, $name) { + $sql = 'SELECT * FROM *PREFIX*quicknotes_tags WHERE user_id = ? AND name = ?'; + return $this->findEntity($sql, [$userId, $name]); + } + + public function tagExists($userId, $name) { + $sql = 'SELECT * FROM *PREFIX*quicknotes_tags WHERE user_id = ? AND name = ?'; + try { + return $this->findEntities($sql, [$userId, $name]); + } catch (DoesNotExistException $e) { + return false; + } + return true; } } \ No newline at end of file diff --git a/js/script.js b/js/script.js index 5a4d157..3dc3ebe 100644 --- a/js/script.js +++ b/js/script.js @@ -126,10 +126,11 @@ Notes.prototype = { isLoaded: function () { return this._loaded; }, - updateActive: function (title, content, color) { + updateActive: function (title, content, tags, color) { var note = this.getActive(); note.title = title; note.content = content; + note.tags = tags; note.color = color; return $.ajax({ @@ -139,9 +140,9 @@ Notes.prototype = { data: JSON.stringify(note) }); }, - updateId: function (id, title, content, color) { + updateId: function (id, title, content, tags, color) { this.load(id); - return this.updateActive(title, content, color); + return this.updateActive(title, content, tags, color); } }; @@ -328,6 +329,9 @@ View.prototype = { var title = $('#modal-note-div #title-editable').html().trim(); var content = $('#modal-note-div #content-editable').html().trim(); var color = this.colorToHex($("#modal-note-div .quicknote").css("background-color")); + var tags = $("#modal-note-div .slim-tag").toArray().map(function (value) { + return value.textContent.trim(); + }); /* var shareSelect = $('.note-share-select'); @@ -343,7 +347,7 @@ View.prototype = { */ var self = this; - this._notes.updateId(id, title, content, color).done(function () { + this._notes.updateId(id, title, content, tags, color).done(function () { var modal = $('#modal-note-div'); var modalnote = $("#modal-note-div .quicknote"); var modaltitle = $('#modal-note-div #title-editable');