Removes the old color if not used by any other note

This commit is contained in:
Matias De lellis
2016-05-26 19:15:53 -03:00
parent 6df7fc0ba8
commit f32eccab48
3 changed files with 28 additions and 11 deletions

View File

@@ -99,14 +99,15 @@ class NoteController extends Controller {
* @param string $color * @param string $color
*/ */
public function update($id, $title, $content, $color = "#F7EB96") { public function update($id, $title, $content, $color = "#F7EB96") {
// Get Note // Get current Note and Color.
try { try {
$note = $this->notemapper->find($id, $this->userId); $note = $this->notemapper->find($id, $this->userId);
} catch(Exception $e) { } catch(Exception $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND); 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)) { if ($this->colormapper->colorExists($color)) {
$hcolor = $this->colormapper->findByColor($color); $hcolor = $this->colormapper->findByColor($color);
} else { } else {
@@ -115,18 +116,25 @@ class NoteController extends Controller {
$hcolor = $this->colormapper->insert($hcolor); $hcolor = $this->colormapper->insert($hcolor);
} }
// TODO: Remove old color if necessary // Set new info on Note
/* Update note */
$note->setTitle($title); $note->setTitle($title);
$note->setContent($content); $note->setContent($content);
$note->setTimestamp(time()); $note->setTimestamp(time());
$note->setColorId($hcolor->id); $note->setColorId($hcolor->id);
// Insert true color to response // Insert true color to response
$note->setColor($hcolor->getColor()); $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,13 +143,22 @@ class NoteController extends Controller {
* @param int $id * @param int $id
*/ */
public function destroy($id) { public function destroy($id) {
// Get Note and Color
try { try {
$note = $this->notemapper->find($id, $this->userId); $note = $this->notemapper->find($id, $this->userId);
} catch(Exception $e) { } catch(Exception $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND); 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); return new DataResponse($note);
} }

View File

@@ -22,7 +22,7 @@ class NoteMapper extends Mapper {
} }
public function colorIdCount($colorid) { 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]); $result = $this->execute($sql, [$colorid]);
$row = $result->fetch(); $row = $result->fetch();
$result->closeCursor(); $result->closeCursor();

View File

@@ -1,5 +1,5 @@
<div class="note-grid-item"> <div class="note-grid-item">
<div class="quicknote noselect {{#if active}}note-active{{/if}}" style="background-color: {{color}}" data-id="{{ id }}"> <div class="quicknote noselect {{#if active}}note-active{{/if}}" style="background-color: {{color}}" data-id="{{ id }}" data-timestamp="{{ timestamp }}" >
<div id='title-editable' class='note-title'>{{{ title }}}</div> <div id='title-editable' class='note-title'>{{{ title }}}</div>
<button class="icon-delete hide-delete-icon icon-delete-note" title="Delete"></button> <button class="icon-delete hide-delete-icon icon-delete-note" title="Delete"></button>
<div id='content-editable' class='note-content'>{{{ content }}}</div> <div id='content-editable' class='note-content'>{{{ content }}}</div>