Implement backend code to update shares with others

This commit is contained in:
Matias De lellis
2020-06-15 23:52:24 -03:00
parent c7a29e51f9
commit 8f88f88dcb
4 changed files with 63 additions and 21 deletions

View File

@@ -206,7 +206,8 @@ View.prototype = {
attachts: this._editableAttachts(), attachts: this._editableAttachts(),
color: this._editableColor(), color: this._editableColor(),
pinned: this._editablePinned(), pinned: this._editablePinned(),
tags: this._editableTags() tags: this._editableTags(),
shares: this._editableShares()
}; };
var self = this; var self = this;
this._notes.update(fakeNote).done(function (note) { this._notes.update(fakeNote).done(function (note) {

View File

@@ -111,10 +111,11 @@ class NoteController extends Controller {
* @param array $attachts * @param array $attachts
* @param bool $pinned * @param bool $pinned
* @param array $tags * @param array $tags
* @param array $shares
* @param string $color * @param string $color
*/ */
public function update(int $id, string $title, string $content, array $attachts, bool $pinned, array $tags, string $color = "#F7EB96"): JSONResponse { 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, $color); $note = $this->noteService->update($this->userId, $id, $title, $content, $attachts, $pinned, $tags, $shares, $color);
if (is_null($note)) { if (is_null($note)) {
return new JSONResponse([], Http::STATUS_NOT_FOUND); return new JSONResponse([], Http::STATUS_NOT_FOUND);
} }

View File

@@ -45,4 +45,14 @@ class NoteShareMapper extends Mapper {
$sql = 'DELETE FROM *PREFIX*quicknotes_shares WHERE note_id = ?'; $sql = 'DELETE FROM *PREFIX*quicknotes_shares WHERE note_id = ?';
$this->execute($sql, [$noteId]); $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;
}
} }

View File

@@ -48,14 +48,14 @@ class NoteService {
private $notemapper; private $notemapper;
private $notetagmapper; private $notetagmapper;
private $colormapper; private $colormapper;
private $notesharemapper; private $noteShareMapper;
private $attachMapper; private $attachMapper;
private $tagmapper; private $tagmapper;
private $fileService; private $fileService;
public function __construct(NoteMapper $notemapper, public function __construct(NoteMapper $notemapper,
NoteTagMapper $notetagmapper, NoteTagMapper $notetagmapper,
NoteShareMapper $notesharemapper, NoteShareMapper $noteShareMapper,
ColorMapper $colormapper, ColorMapper $colormapper,
AttachMapper $attachMapper, AttachMapper $attachMapper,
TagMapper $tagmapper, TagMapper $tagmapper,
@@ -64,7 +64,7 @@ class NoteService {
$this->notemapper = $notemapper; $this->notemapper = $notemapper;
$this->notetagmapper = $notetagmapper; $this->notetagmapper = $notetagmapper;
$this->colormapper = $colormapper; $this->colormapper = $colormapper;
$this->notesharemapper = $notesharemapper; $this->noteShareMapper = $noteShareMapper;
$this->attachMapper = $attachMapper; $this->attachMapper = $attachMapper;
$this->tagmapper = $tagmapper; $this->tagmapper = $tagmapper;
$this->fileService = $fileService; $this->fileService = $fileService;
@@ -79,12 +79,12 @@ class NoteService {
// Set shares with others. // Set shares with others.
foreach($notes as $note) { foreach($notes as $note) {
$note->setIsShared(false); $note->setIsShared(false);
$note->setSharedWith($this->notesharemapper->getSharesForNote($note->getId())); $note->setSharedWith($this->noteShareMapper->getSharesForNote($note->getId()));
} }
// Get shares from others. // Get shares from others.
$shares = []; $shares = [];
$sharedEntries = $this->notesharemapper->findForUser($userId); $sharedEntries = $this->noteShareMapper->findForUser($userId);
foreach($sharedEntries as $sharedEntry) { foreach($sharedEntries as $sharedEntry) {
$sharedNote = $this->notemapper->findShared($sharedEntry->getNoteId()); $sharedNote = $this->notemapper->findShared($sharedEntry->getNoteId());
$sharedNote->setIsShared(true); $sharedNote->setIsShared(true);
@@ -181,6 +181,7 @@ class NoteService {
* @param array $attachts * @param array $attachts
* @param bool $pinned * @param bool $pinned
* @param array $tags * @param array $tags
* @param array $shares
* @param string $color * @param string $color
*/ */
public function update(string $userId, public function update(string $userId,
@@ -190,14 +191,14 @@ class NoteService {
array $attachts, array $attachts,
bool $pinned, bool $pinned,
array $tags, array $tags,
string $color): Note array $shares,
string $color): ?Note
{ {
// Get current Note and Color. // Get current Note and Color.
try { $note = $this->get($userId, $id);
$note = $this->notemapper->find($id, $userId); if (is_null($note))
} catch(Exception $e) {
return null; return null;
}
$oldcolorid = $note->getColorId(); $oldcolorid = $note->getColorId();
// Get new Color or append it. // 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 // Delete old tag relations
$dbTags = $this->tagmapper->getTagsForNote($userId, $id); $dbTags = $this->tagmapper->getTagsForNote($userId, $id);
foreach ($dbTags as $dbTag) { foreach ($dbTags as $dbTag) {
@@ -298,6 +324,10 @@ class NoteService {
} }
$newnote->setAttachts($attachts); $newnote->setAttachts($attachts);
// Fill shared with with others
$newnote->setIsShared(false);
$newnote->setSharedWith($this->noteShareMapper->getSharesForNote($newnote->getId()));
// Remove old color if necessary // Remove old color if necessary
if (($oldcolorid !== $hcolor->getId()) && if (($oldcolorid !== $hcolor->getId()) &&
(!$this->notemapper->colorIdCount($oldcolorid))) { (!$this->notemapper->colorIdCount($oldcolorid))) {
@@ -324,7 +354,7 @@ class NoteService {
} }
$oldcolorid = $note->getColorId(); $oldcolorid = $note->getColorId();
$this->notesharemapper->deleteByNoteId($note->getId()); $this->noteShareMapper->deleteByNoteId($note->getId());
// Delete note. // Delete note.
$this->notemapper->delete($note); $this->notemapper->delete($note);
@@ -375,7 +405,7 @@ class NoteService {
} }
$pos_users = array(); $pos_users = array();
$pos_groups = array(); $pos_groups = array();
$shares = $this->notesharemapper->getSharesForNote($noteId); $shares = $this->noteShareMapper->getSharesForNote($noteId);
foreach($shares as $s) { foreach($shares as $s) {
$shareType = $s->getSharedUser(); $shareType = $s->getSharedUser();
if(strlen($shareType) !== 0) { if(strlen($shareType) !== 0) {
@@ -400,14 +430,14 @@ class NoteService {
$share = new NoteShare(); $share = new NoteShare();
$share->setSharedGroup($groupId); $share->setSharedGroup($groupId);
$share->setNoteId($noteId); $share->setNoteId($noteId);
$this->notesharemapper->insert($share); $this->noteShareMapper->insert($share);
} }
/** /**
*/ */
public function removeGroupShare($groupId, $noteId) { public function removeGroupShare($groupId, $noteId) {
$share = $this->notesharemapper->findByNoteAndGroup($noteId, $groupId); $share = $this->noteShareMapper->findByNoteAndGroup($noteId, $groupId);
$this->notesharemapper->delete($share); $this->noteShareMapper->delete($share);
} }
/** /**
@@ -416,13 +446,13 @@ class NoteService {
$share = new NoteShare(); $share = new NoteShare();
$share->setSharedUser($userId); $share->setSharedUser($userId);
$share->setNoteId($noteId); $share->setNoteId($noteId);
$this->notesharemapper->insert($share); $this->noteShareMapper->insert($share);
} }
/** /**
*/ */
public function removeUserShare($userId, $noteId) { public function removeUserShare($userId, $noteId) {
$share = $this->notesharemapper->findByNoteAndUser($noteId, $userId); $share = $this->noteShareMapper->findByNoteAndUser($noteId, $userId);
$this->notesharemapper->delete($share); $this->noteShareMapper->delete($share);
} }
} }