Put color on own database. Umm.. Any relationship placed on the note controller.

This commit is contained in:
Matias De lellis
2016-05-26 18:09:55 -03:00
parent b7264d4377
commit 6df7fc0ba8
7 changed files with 163 additions and 21 deletions

View File

@@ -35,6 +35,33 @@
<default></default> <default></default>
<notnull>true</notnull> <notnull>true</notnull>
</field> </field>
<field>
<name>timestamp</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<length>4</length>
</field>
<field>
<name>color_id</name>
<type>integer</type>
<notnull>true</notnull>
<length>8</length>
</field>
</declaration>
</table>
<table>
<name>*dbprefix*quicknotes_colors</name>
<declaration>
<field>
<name>id</name>
<type>integer</type>
<notnull>true</notnull>
<autoincrement>true</autoincrement>
<unsigned>true</unsigned>
<primary>true</primary>
<length>8</length>
</field>
<field> <field>
<name>color</name> <name>color</name>
<type>text</type> <type>text</type>

View File

@@ -5,7 +5,7 @@
<description>Quick notes with text, check lists and pictures</description> <description>Quick notes with text, check lists and pictures</description>
<licence>AGPL</licence> <licence>AGPL</licence>
<author>Matias De lellis</author> <author>Matias De lellis</author>
<version>0.0.4</version> <version>0.0.6</version>
<namespace>QuickNotes</namespace> <namespace>QuickNotes</namespace>
<category>tool</category> <category>tool</category>
<website>https://github.com/matiasdelellis</website> <website>https://github.com/matiasdelellis</website>

View File

@@ -17,17 +17,20 @@ use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCA\QuickNotes\Db\Note; use OCA\QuickNotes\Db\Note;
use OCA\QuickNotes\Db\Color;
use OCA\QuickNotes\Db\NoteMapper; 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; 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); parent::__construct($AppName, $request);
$this->mapper = $mapper; $this->notemapper = $notemapper;
$this->colormapper = $colormapper;
$this->userId = $UserId; $this->userId = $UserId;
} }
@@ -35,7 +38,12 @@ class NoteController extends Controller {
* @NoAdminRequired * @NoAdminRequired
*/ */
public function index() { 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 * @param int $id
*/ */
public function show($id) { public function show($id) {
// TODO: Implement.
try { try {
return new DataResponse($this->mapper->find($id, $this->userId)); return new DataResponse($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);
} }
@@ -58,12 +67,27 @@ class NoteController extends Controller {
* @param string $content * @param string $content
*/ */
public function create($title, $content, $color = "#F7EB96") { 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 = new Note();
$note->setTitle($title); $note->setTitle($title);
$note->setContent($content); $note->setContent($content);
$note->setColor($color); $note->setTimestamp(time());
$note->setColorId($hcolor->id);
$note->setUserId($this->userId); $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 int $id
* @param string $title * @param string $title
* @param string $content * @param string $content
* @param string $color
*/ */
public function update($id, $title, $content, $color = "#F7EB96") { public function update($id, $title, $content, $color = "#F7EB96") {
// Get Note
try { try {
$note = $this->mapper->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);
} }
// 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->setTitle($title);
$note->setContent($content); $note->setContent($content);
$note->setColor($color); $note->setTimestamp(time());
return new DataResponse($this->mapper->update($note)); $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) { public function destroy($id) {
try { try {
$note = $this->mapper->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);
} }
/*$taskmapper = new TaskMapper($this->mapper->db); // TODO: Remove old color if necessary.
$tasks = $taskmapper->findAll($note);
foreach ($tasks as $task) {
$taskmapper->delete($task->id);
}*/
$this->mapper->delete($note);
return new DataResponse($note); return new DataResponse($note);
} }

19
db/color.php Normal file
View File

@@ -0,0 +1,19 @@
<?php
namespace OCA\QuickNotes\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class Color extends Entity implements JsonSerializable {
protected $color;
public function jsonSerialize() {
return [
'id' => $this->id,
'color' => $this->color
];
}
}

39
db/colormapper.php Normal file
View File

@@ -0,0 +1,39 @@
<?php
namespace OCA\QuickNotes\Db;
use OCP\IDb;
use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Db\DoesNotExistException;
class ColorMapper extends Mapper {
public function __construct(IDb $db) {
parent::__construct($db, 'quicknotes_colors', '\OCA\QuickNotes\Db\Color');
}
public function find($id) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_colors WHERE id = ?';
return $this->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;
}
}

View File

@@ -9,14 +9,23 @@ class Note extends Entity implements JsonSerializable {
protected $title; protected $title;
protected $content; protected $content;
protected $color; protected $timestamp;
protected $colorId;
protected $userId; protected $userId;
protected $color;
public function setColor($color) {
$this->color = $color;
}
public function jsonSerialize() { public function jsonSerialize() {
return [ return [
'id' => $this->id, 'id' => $this->id,
'title' => $this->title, 'title' => $this->title,
'content' => $this->content, 'content' => $this->content,
'timestamp' => $this->timestamp,
'colorid' => $this->colorId,
'color' => $this->color 'color' => $this->color
]; ];
} }

View File

@@ -3,6 +3,7 @@ namespace OCA\QuickNotes\Db;
use OCP\IDb; use OCP\IDb;
use OCP\AppFramework\Db\Mapper; use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Db\DoesNotExistException;
class NoteMapper extends Mapper { class NoteMapper extends Mapper {
@@ -20,4 +21,12 @@ class NoteMapper extends Mapper {
return $this->findEntities($sql, [$userId]); 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'];
}
} }