diff --git a/controller/notecontroller.php b/controller/notecontroller.php index 9fb3dce..5c5b847 100644 --- a/controller/notecontroller.php +++ b/controller/notecontroller.php @@ -99,14 +99,15 @@ class NoteController extends Controller { * @param string $color */ public function update($id, $title, $content, $color = "#F7EB96") { - // Get Note + // Get current Note and Color. try { $note = $this->notemapper->find($id, $this->userId); } catch(Exception $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } + $oldcolorid = $note->getColorId(); - // Get color or append it + // Get new Color or append it. if ($this->colormapper->colorExists($color)) { $hcolor = $this->colormapper->findByColor($color); } else { @@ -115,18 +116,25 @@ class NoteController extends Controller { $hcolor = $this->colormapper->insert($hcolor); } - // TODO: Remove old color if necessary - - /* Update note */ + // Set new info on Note $note->setTitle($title); $note->setContent($content); $note->setTimestamp(time()); $note->setColorId($hcolor->id); - // Insert true color to response $note->setColor($hcolor->getColor()); - return new DataResponse($this->notemapper->update($note)); + // Update note. + $newnote = $this->notemapper->update($note); + + // Remove old color if necessary + if (($oldcolorid != $hcolor->getId()) && + (!$this->notemapper->colorIdCount($oldcolorid))) { + $oldcolor = $this->colormapper->find($oldcolorid); + $this->colormapper->delete($oldcolor); + } + + return new DataResponse($newnote); } /** @@ -135,14 +143,23 @@ class NoteController extends Controller { * @param int $id */ public function destroy($id) { + // Get Note and Color try { $note = $this->notemapper->find($id, $this->userId); } catch(Exception $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } + $oldcolorid = $note->getColorId(); - // TODO: Remove old color if necessary. + // Delete note. + $this->notemapper->delete($note); + + // Delete Color if necessary + if (!$this->notemapper->colorIdCount($oldcolorid)) { + $oldcolor = $this->colormapper->find($oldcolorid); + $this->colormapper->delete($oldcolor); + } return new DataResponse($note); } - } \ No newline at end of file +} \ No newline at end of file diff --git a/db/notemapper.php b/db/notemapper.php index 2406f7b..1cb3261 100644 --- a/db/notemapper.php +++ b/db/notemapper.php @@ -22,7 +22,7 @@ class NoteMapper extends Mapper { } public function colorIdCount($colorid) { - $sql = 'SELECT COUNT(*) as `count` FROM *PREFIX*quicknotes WHERE color_id = ?'; + $sql = 'SELECT COUNT(*) as `count` FROM *PREFIX*quicknotes_notes WHERE color_id = ?'; $result = $this->execute($sql, [$colorid]); $row = $result->fetch(); $result->closeCursor(); diff --git a/templates/part.note.php b/templates/part.note.php index 9001641..8cc1875 100644 --- a/templates/part.note.php +++ b/templates/part.note.php @@ -1,5 +1,5 @@
-
+
{{{ title }}}
{{{ content }}}