diff --git a/js/script.js b/js/script.js index c68c93b..1847229 100644 --- a/js/script.js +++ b/js/script.js @@ -206,7 +206,8 @@ View.prototype = { attachts: this._editableAttachts(), color: this._editableColor(), pinned: this._editablePinned(), - tags: this._editableTags() + tags: this._editableTags(), + shares: this._editableShares() }; var self = this; this._notes.update(fakeNote).done(function (note) { diff --git a/lib/Controller/NoteController.php b/lib/Controller/NoteController.php index 4be01ca..ee18753 100644 --- a/lib/Controller/NoteController.php +++ b/lib/Controller/NoteController.php @@ -111,10 +111,11 @@ class NoteController extends Controller { * @param array $attachts * @param bool $pinned * @param array $tags + * @param array $shares * @param string $color */ - public function update(int $id, string $title, string $content, array $attachts, bool $pinned, array $tags, string $color = "#F7EB96"): JSONResponse { - $note = $this->noteService->update($this->userId, $id, $title, $content, $attachts, $pinned, $tags, $color); + public function update(int $id, string $title, string $content, array $attachts, bool $pinned, array $tags, array $shares, string $color = "#F7EB96"): JSONResponse { + $note = $this->noteService->update($this->userId, $id, $title, $content, $attachts, $pinned, $tags, $shares, $color); if (is_null($note)) { return new JSONResponse([], Http::STATUS_NOT_FOUND); } diff --git a/lib/Db/NoteShareMapper.php b/lib/Db/NoteShareMapper.php index f9d9d6b..50fcc67 100644 --- a/lib/Db/NoteShareMapper.php +++ b/lib/Db/NoteShareMapper.php @@ -45,4 +45,14 @@ class NoteShareMapper extends Mapper { $sql = 'DELETE FROM *PREFIX*quicknotes_shares WHERE note_id = ?'; $this->execute($sql, [$noteId]); } + + public function existsByNoteAndUser($noteId, $userId) { + $sql = 'SELECT * FROM *PREFIX*quicknotes_shares WHERE shared_user = ? AND note_id = ?'; + try { + return $this->findEntities($sql, [$userId, $noteId]); + } catch (DoesNotExistException $e) { + return false; + } + return true; + } } diff --git a/lib/Service/NoteService.php b/lib/Service/NoteService.php index 265b73a..2bb9645 100644 --- a/lib/Service/NoteService.php +++ b/lib/Service/NoteService.php @@ -48,14 +48,14 @@ class NoteService { private $notemapper; private $notetagmapper; private $colormapper; - private $notesharemapper; + private $noteShareMapper; private $attachMapper; private $tagmapper; private $fileService; public function __construct(NoteMapper $notemapper, NoteTagMapper $notetagmapper, - NoteShareMapper $notesharemapper, + NoteShareMapper $noteShareMapper, ColorMapper $colormapper, AttachMapper $attachMapper, TagMapper $tagmapper, @@ -64,7 +64,7 @@ class NoteService { $this->notemapper = $notemapper; $this->notetagmapper = $notetagmapper; $this->colormapper = $colormapper; - $this->notesharemapper = $notesharemapper; + $this->noteShareMapper = $noteShareMapper; $this->attachMapper = $attachMapper; $this->tagmapper = $tagmapper; $this->fileService = $fileService; @@ -79,12 +79,12 @@ class NoteService { // Set shares with others. foreach($notes as $note) { $note->setIsShared(false); - $note->setSharedWith($this->notesharemapper->getSharesForNote($note->getId())); + $note->setSharedWith($this->noteShareMapper->getSharesForNote($note->getId())); } // Get shares from others. $shares = []; - $sharedEntries = $this->notesharemapper->findForUser($userId); + $sharedEntries = $this->noteShareMapper->findForUser($userId); foreach($sharedEntries as $sharedEntry) { $sharedNote = $this->notemapper->findShared($sharedEntry->getNoteId()); $sharedNote->setIsShared(true); @@ -181,6 +181,7 @@ class NoteService { * @param array $attachts * @param bool $pinned * @param array $tags + * @param array $shares * @param string $color */ public function update(string $userId, @@ -190,14 +191,14 @@ class NoteService { array $attachts, bool $pinned, array $tags, - string $color): Note + array $shares, + string $color): ?Note { // Get current Note and Color. - try { - $note = $this->notemapper->find($id, $userId); - } catch(Exception $e) { + $note = $this->get($userId, $id); + if (is_null($note)) return null; - } + $oldcolorid = $note->getColorId(); // Get new Color or append it. @@ -236,6 +237,31 @@ class NoteService { } } + // Delete old shares + $dbShares = $this->noteShareMapper->getSharesForNote($id); + foreach ($dbShares as $dbShare) { + $delete = true; + foreach ($shares as $share) { + if ($dbShare->getSharedUser() === $share['name']) { + $delete = false; + break; + } + } + if ($delete) { + $this->noteShareMapper->delete($dbShare); + } + } + + // Add new shares + foreach ($shares as $share) { + if (!$this->noteShareMapper->existsByNoteAndUser($id, $share['name'])) { + $hShare = new NoteShare(); + $hShare->setNoteId($id); + $hShare->setSharedUser($share['name']); + $this->noteShareMapper->insert($hShare); + } + } + // Delete old tag relations $dbTags = $this->tagmapper->getTagsForNote($userId, $id); foreach ($dbTags as $dbTag) { @@ -298,6 +324,10 @@ class NoteService { } $newnote->setAttachts($attachts); + // Fill shared with with others + $newnote->setIsShared(false); + $newnote->setSharedWith($this->noteShareMapper->getSharesForNote($newnote->getId())); + // Remove old color if necessary if (($oldcolorid !== $hcolor->getId()) && (!$this->notemapper->colorIdCount($oldcolorid))) { @@ -324,7 +354,7 @@ class NoteService { } $oldcolorid = $note->getColorId(); - $this->notesharemapper->deleteByNoteId($note->getId()); + $this->noteShareMapper->deleteByNoteId($note->getId()); // Delete note. $this->notemapper->delete($note); @@ -375,7 +405,7 @@ class NoteService { } $pos_users = array(); $pos_groups = array(); - $shares = $this->notesharemapper->getSharesForNote($noteId); + $shares = $this->noteShareMapper->getSharesForNote($noteId); foreach($shares as $s) { $shareType = $s->getSharedUser(); if(strlen($shareType) !== 0) { @@ -400,14 +430,14 @@ class NoteService { $share = new NoteShare(); $share->setSharedGroup($groupId); $share->setNoteId($noteId); - $this->notesharemapper->insert($share); + $this->noteShareMapper->insert($share); } /** */ public function removeGroupShare($groupId, $noteId) { - $share = $this->notesharemapper->findByNoteAndGroup($noteId, $groupId); - $this->notesharemapper->delete($share); + $share = $this->noteShareMapper->findByNoteAndGroup($noteId, $groupId); + $this->noteShareMapper->delete($share); } /** @@ -416,13 +446,13 @@ class NoteService { $share = new NoteShare(); $share->setSharedUser($userId); $share->setNoteId($noteId); - $this->notesharemapper->insert($share); + $this->noteShareMapper->insert($share); } /** */ public function removeUserShare($userId, $noteId) { - $share = $this->notesharemapper->findByNoteAndUser($noteId, $userId); - $this->notesharemapper->delete($share); + $share = $this->noteShareMapper->findByNoteAndUser($noteId, $userId); + $this->noteShareMapper->delete($share); } }