diff --git a/appinfo/database.xml b/appinfo/database.xml index 4e2b92a..5389d05 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -35,6 +35,33 @@ true + + timestamp + integer + 0 + true + 4 + + + color_id + integer + true + 8 + + + + + *dbprefix*quicknotes_colors + + + id + integer + true + true + true + true + 8 + color text diff --git a/appinfo/info.xml b/appinfo/info.xml index f7a8224..7adfc92 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,7 +5,7 @@ Quick notes with text, check lists and pictures AGPL Matias De lellis - 0.0.4 + 0.0.6 QuickNotes tool https://github.com/matiasdelellis diff --git a/controller/notecontroller.php b/controller/notecontroller.php index 975168b..9fb3dce 100644 --- a/controller/notecontroller.php +++ b/controller/notecontroller.php @@ -17,17 +17,20 @@ use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Controller; use OCA\QuickNotes\Db\Note; +use OCA\QuickNotes\Db\Color; use OCA\QuickNotes\Db\NoteMapper; -use OCA\QuickNotes\Db\TaskMapper; +use OCA\QuickNotes\Db\ColorMapper; -class NoteController extends Controller { +class NoteController extends Controller { - private $mapper; + private $notemapper; + private $colormapper; private $userId; - public function __construct($AppName, IRequest $request, NoteMapper $mapper, $UserId) { + public function __construct($AppName, IRequest $request, NoteMapper $notemapper, ColorMapper $colormapper, $UserId) { parent::__construct($AppName, $request); - $this->mapper = $mapper; + $this->notemapper = $notemapper; + $this->colormapper = $colormapper; $this->userId = $UserId; } @@ -35,7 +38,12 @@ class NoteController extends Controller { * @NoAdminRequired */ public function index() { - return new DataResponse($this->mapper->findAll($this->userId)); + $notes = $this->notemapper->findAll($this->userId); + // Insert true color to response + foreach ($notes as $note) { + $note->setColor($this->colormapper->find($note->getColorId())->getColor()); + } + return new DataResponse($notes); } /** @@ -44,8 +52,9 @@ class NoteController extends Controller { * @param int $id */ public function show($id) { + // TODO: Implement. try { - return new DataResponse($this->mapper->find($id, $this->userId)); + return new DataResponse($this->notemapper->find($id, $this->userId)); } catch(Exception $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } @@ -58,12 +67,27 @@ class NoteController extends Controller { * @param string $content */ public function create($title, $content, $color = "#F7EB96") { + // Get color or append it + if ($this->colormapper->colorExists($color)) { + $hcolor = $this->colormapper->findByColor($color); + } else { + $hcolor = new Color(); + $hcolor->setColor($color); + $hcolor = $this->colormapper->insert($hcolor); + } + + // Create note and insert it $note = new Note(); $note->setTitle($title); $note->setContent($content); - $note->setColor($color); + $note->setTimestamp(time()); + $note->setColorId($hcolor->id); $note->setUserId($this->userId); - return new DataResponse($this->mapper->insert($note)); + + // Insert true color to response + $note->setColor($hcolor->getColor()); + + return new DataResponse($this->notemapper->insert($note)); } /** @@ -72,17 +96,37 @@ class NoteController extends Controller { * @param int $id * @param string $title * @param string $content + * @param string $color */ public function update($id, $title, $content, $color = "#F7EB96") { + // Get Note try { - $note = $this->mapper->find($id, $this->userId); + $note = $this->notemapper->find($id, $this->userId); } catch(Exception $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } + + // Get color or append it + if ($this->colormapper->colorExists($color)) { + $hcolor = $this->colormapper->findByColor($color); + } else { + $hcolor = new Color(); + $hcolor->setColor($color); + $hcolor = $this->colormapper->insert($hcolor); + } + + // TODO: Remove old color if necessary + + /* Update note */ $note->setTitle($title); $note->setContent($content); - $note->setColor($color); - return new DataResponse($this->mapper->update($note)); + $note->setTimestamp(time()); + $note->setColorId($hcolor->id); + + // Insert true color to response + $note->setColor($hcolor->getColor()); + + return new DataResponse($this->notemapper->update($note)); } /** @@ -92,17 +136,12 @@ class NoteController extends Controller { */ public function destroy($id) { try { - $note = $this->mapper->find($id, $this->userId); + $note = $this->notemapper->find($id, $this->userId); } catch(Exception $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } - /*$taskmapper = new TaskMapper($this->mapper->db); - $tasks = $taskmapper->findAll($note); - foreach ($tasks as $task) { - $taskmapper->delete($task->id); - }*/ - $this->mapper->delete($note); + // TODO: Remove old color if necessary. return new DataResponse($note); } diff --git a/db/color.php b/db/color.php new file mode 100644 index 0000000..0fb90b7 --- /dev/null +++ b/db/color.php @@ -0,0 +1,19 @@ + $this->id, + 'color' => $this->color + ]; + } + +} \ No newline at end of file diff --git a/db/colormapper.php b/db/colormapper.php new file mode 100644 index 0000000..ce3d41c --- /dev/null +++ b/db/colormapper.php @@ -0,0 +1,39 @@ +findEntity($sql, [$id]); + } + + public function findAll() { + $sql = 'SELECT * FROM *PREFIX*quicknotes_colors'; + return $this->findEntities($sql, []); + } + + public function findByColor($color) { + $sql = 'SELECT * FROM *PREFIX*quicknotes_colors WHERE color = ?'; + return $this->findEntity($sql, [$color]); + } + + public function colorExists($color) { + $sql = 'SELECT * FROM *PREFIX*quicknotes_colors WHERE color = ?'; + try { + $this->findEntity($sql, [$color]); + } catch (DoesNotExistException $e) { + return false; + } + return true; + } + +} \ No newline at end of file diff --git a/db/note.php b/db/note.php index b09b5cc..301e229 100644 --- a/db/note.php +++ b/db/note.php @@ -9,14 +9,23 @@ class Note extends Entity implements JsonSerializable { protected $title; protected $content; - protected $color; + protected $timestamp; + protected $colorId; protected $userId; + protected $color; + + public function setColor($color) { + $this->color = $color; + } + public function jsonSerialize() { return [ 'id' => $this->id, 'title' => $this->title, 'content' => $this->content, + 'timestamp' => $this->timestamp, + 'colorid' => $this->colorId, 'color' => $this->color ]; } diff --git a/db/notemapper.php b/db/notemapper.php index 47032d1..2406f7b 100644 --- a/db/notemapper.php +++ b/db/notemapper.php @@ -3,6 +3,7 @@ namespace OCA\QuickNotes\Db; use OCP\IDb; use OCP\AppFramework\Db\Mapper; +use OCP\AppFramework\Db\DoesNotExistException; class NoteMapper extends Mapper { @@ -20,4 +21,12 @@ class NoteMapper extends Mapper { return $this->findEntities($sql, [$userId]); } + public function colorIdCount($colorid) { + $sql = 'SELECT COUNT(*) as `count` FROM *PREFIX*quicknotes WHERE color_id = ?'; + $result = $this->execute($sql, [$colorid]); + $row = $result->fetch(); + $result->closeCursor(); + return $row['count']; + } + } \ No newline at end of file