Inital backend support to pinned notes

This commit is contained in:
Matias De lellis
2020-06-04 13:39:58 -03:00
parent 0845127458
commit c70c87a20d
10 changed files with 126 additions and 124 deletions

View File

@@ -88,10 +88,17 @@ class NoteController extends Controller {
}
}
$notes = array_merge($notes, $shares);
// Insert true color to response
foreach ($notes as $note) {
$note->setColor($this->colormapper->find($note->getColorId())->getColor());
}
// Insert true color to response
foreach ($notes as $note) {
$note->setIsPinned($note->getPinned() ? true : false);
}
return new DataResponse($notes);
}
@@ -145,10 +152,11 @@ class NoteController extends Controller {
* @param int $id
* @param string $title
* @param string $content
* @param boolean $pinned
* @param array $tags
* @param string $color
*/
public function update($id, $title, $content, $tags, $color = "#F7EB96") {
public function update($id, $title, $content, $pinned, $tags, $color = "#F7EB96") {
// Get current Note and Color.
try {
$note = $this->notemapper->find($id, $this->userId);
@@ -206,10 +214,13 @@ class NoteController extends Controller {
// Set new info on Note
$note->setTitle($title);
$note->setContent($content);
$note->setPinned($pinned ? 1 : 0);
$note->setTimestamp(time());
$note->setColorId($hcolor->id);
// Insert true color to response
$note->setColor($hcolor->getColor());
$note->setIsPinned($note->getPinned() ? true : false);
// Update note.
$newnote = $this->notemapper->update($note);

View File

@@ -9,6 +9,7 @@ class Note extends Entity implements JsonSerializable {
protected $title;
protected $content;
protected $pinned;
protected $timestamp;
protected $colorId;
protected $userId;
@@ -17,16 +18,23 @@ class Note extends Entity implements JsonSerializable {
protected $tags;
protected $color;
protected $isPinned;
public function setColor($color) {
$this->color = $color;
}
public function setIsPinned($pinned) {
$this->isPinned = $pinned;
}
public function jsonSerialize() {
return [
'id' => $this->id,
'title' => $this->title,
'content' => $this->content,
'pinned' => $this->pinned,
'ispinned' => $this->isPinned,
'timestamp' => $this->timestamp,
'colorid' => $this->colorId,
'color' => $this->color,

View File

@@ -1,14 +1,16 @@
<?php
<?php declare(strict_types=1);
namespace OCA\QuickNotes\Db;
use OCP\IDBConnection;
use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Db\QBMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\DB\QueryBuilder\IQueryBuilder;
class NoteMapper extends Mapper {
class NoteMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'quicknotes_notes', '\OCA\QuickNotes\Db\Note');
parent::__construct($db, 'quicknotes_notes');
}
/**
@@ -19,26 +21,34 @@ class NoteMapper extends Mapper {
* @return Note
*/
public function find($id, $userId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_notes WHERE id = ? AND user_id = ?';
return $this->findEntity($sql, [$id, $userId]);
}
public function findById($id) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_notes WHERE id = ?';
return $this->findEntity($sql, [$id]);
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->tableName)
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)),
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);
return $this->findEntity($qb);
}
public function findAll($userId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_notes WHERE user_id = ?';
return $this->findEntities($sql, [$userId]);
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->tableName)
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
);
return $this->findEntities($qb);
}
public function colorIdCount($colorid) {
$sql = 'SELECT COUNT(*) as `count` FROM *PREFIX*quicknotes_notes WHERE color_id = ?';
$result = $this->execute($sql, [$colorid]);
$row = $result->fetch();
$result->closeCursor();
return $row['count'];
$qb = $this->db->getQueryBuilder();
$qb->select('id')
->from($this->tableName)
->where(
$qb->expr()->eq('color_id', $qb->createNamedParameter($colorid, IQueryBuilder::PARAM_INT))
);
return count($this->findEntities($qb));
}
}

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace OCA\QuickNotes\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version000205Date20200604122312 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$table = $schema->getTable('quicknotes_notes');
$table->addColumn('pinned', 'boolean', [
'notnull' => true,
'default' => false,
]);
return $schema;
}
}

View File

@@ -1,4 +0,0 @@
<?php
namespace OCA\QuickNotes\Service;
class NotFoundException extends ServiceException {}

View File

@@ -1,77 +0,0 @@
<?php
namespace OCA\QuickNotes\Service;
use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCA\OwnNotes\Db\Note;
use OCA\OwnNotes\Db\NoteMapper;
class NoteService {
private $mapper;
public function __construct(NoteMapper $mapper){
$this->mapper = $mapper;
}
public function findAll($userId) {
return $this->mapper->findAll($userId);
}
private function handleException ($e) {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new NotFoundException($e->getMessage());
} else {
throw $e;
}
}
public function find($id, $userId) {
try {
return $this->mapper->find($id, $userId);
// in order to be able to plug in different storage backends like files
// for instance it is a good idea to turn storage related exceptions
// into service related exceptions so controllers and service users
// have to deal with only one type of exception
} catch(Exception $e) {
$this->handleException($e);
}
}
public function create($title, $content, $color = "#F7EB96", $userId) {
$note = new Note();
$note->setTitle($title);
$note->setContent($content);
$note->setColor($color);
$note->setUserId($userId);
return $this->mapper->insert($note);
}
public function update($id, $title, $content, $color = "#F7EB96", $userId) {
try {
$note = $this->mapper->find($id, $userId);
$note->setTitle($title);
$note->setContent($content);
$note->setColor($color);
return $this->mapper->update($note);
} catch(Exception $e) {
$this->handleException($e);
}
}
public function delete($id, $userId) {
try {
$note = $this->mapper->find($id, $userId);
$this->mapper->delete($note);
return $note;
} catch(Exception $e) {
$this->handleException($e);
}
}
}

View File

@@ -1,6 +0,0 @@
<?php
namespace OCA\QuickNotes\Service;
use Exception;
class ServiceException extends Exception {}