Upgrade to some new nextcloud standards

This commit is contained in:
Matias De lellis
2020-05-30 18:33:01 -03:00
parent d417902d94
commit f7ab295a6c
21 changed files with 192 additions and 207 deletions

24
lib/Controller/Errors.php Normal file
View File

@@ -0,0 +1,24 @@
<?php
namespace OCA\QuickNotes\Controller;
use Closure;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCA\QuickNotes\Service\NotFoundException;
trait Errors {
protected function handleNotFound (Closure $callback) {
try {
return new DataResponse($callback());
} catch(NotFoundException $e) {
$message = ['message' => $e->getMessage()];
return new DataResponse($message, Http::STATUS_NOT_FOUND);
}
}
}

View File

@@ -0,0 +1,350 @@
<?php
/**
* ownCloud - quicknotes
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Matias De lellis <mati86dl@gmail.com>
* @copyright Matias De lellis 2016
*/
namespace OCA\QuickNotes\Controller;
use OCP\IRequest;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Controller;
use OCA\QuickNotes\Db\Color;
use OCA\QuickNotes\Db\ColorMapper;
use OCA\QuickNotes\Db\Note;
use OCA\QuickNotes\Db\NoteMapper;
use OCA\QuickNotes\Db\NoteTag;
use OCA\QuickNotes\Db\NoteTagMapper;
use OCA\QuickNotes\Db\NoteShare;
use OCA\QuickNotes\Db\NoteShareMapper;
use OCA\QuickNotes\Db\Tag;
use OCA\QuickNotes\Db\TagMapper;
class NoteController extends Controller {
private $notemapper;
private $notetagmapper;
private $colormapper;
private $notesharemapper;
private $tagmapper;
private $userId;
public function __construct($AppName,
IRequest $request,
NoteMapper $notemapper,
NoteTagMapper $notetagmapper,
NoteShareMapper $notesharemapper,
ColorMapper $colormapper,
TagMapper $tagmapper,
$UserId)
{
parent::__construct($AppName, $request);
$this->notemapper = $notemapper;
$this->notetagmapper = $notetagmapper;
$this->colormapper = $colormapper;
$this->notesharemapper = $notesharemapper;
$this->tagmapper = $tagmapper;
$this->userId = $UserId;
}
/**
* @NoAdminRequired
*/
public function index() {
$notes = $this->notemapper->findAll($this->userId);
foreach($notes as $note) {
$note->setIsShared(false);
$sharedWith = $this->notesharemapper->getSharesForNote($note->getId());
if(count($sharedWith) > 0) {
$shareList = array();
foreach($sharedWith as $share) {
$shareList[] = $share->getSharedUser();
}
$note->setSharedWith(implode(", ", $shareList));
} else {
$note->setSharedWith(null);
}
$note->setTags($this->tagmapper->getTagsForNote($this->userId, $note->getId()));
}
$shareEntries = $this->notesharemapper->findForUser($this->userId);
$shares = array();
foreach($shareEntries as $entry) {
try {
//find is only to check if current user is owner
$this->notemapper->find($entry->getNoteId(), $this->userId);
//user is owner, nothing to do
} catch(\OCP\AppFramework\Db\DoesNotExistException $e) {
$share = $this->notemapper->findById($entry->getNoteId());
$share->setIsShared(true);
$shares[] = $share;
}
}
$notes = array_merge($notes, $shares);
// Insert true color to response
foreach ($notes as $note) {
$note->setColor($this->colormapper->find($note->getColorId())->getColor());
}
return new DataResponse($notes);
}
/**
* @NoAdminRequired
*
* @param int $id
*/
public function show($id) {
// TODO: Implement.
try {
return new DataResponse($this->notemapper->find($id, $this->userId));
} catch(Exception $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
}
/**
* @NoAdminRequired
*
* @param string $title
* @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->setTimestamp(time());
$note->setColorId($hcolor->id);
$note->setUserId($this->userId);
// Insert true color to response
$note->setColor($hcolor->getColor());
return new DataResponse($this->notemapper->insert($note));
}
/**
* @NoAdminRequired
*
* @param int $id
* @param string $title
* @param string $content
* @param array $tags
* @param string $color
*/
public function update($id, $title, $content, $tags, $color = "#F7EB96") {
// 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 new 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);
}
// Delete old tag relations
$dbTags = $this->tagmapper->getTagsForNote($this->userId, $id);
foreach ($dbTags as $dbTag) {
$delete = true;
foreach ($tags as $tag) {
if ($dbTag->getId() == $tag['id']) {
$delete = false;
break;
}
}
if ($delete) {
$hnotetag = $this->notetagmapper->findNoteTag($this->userId, $id, $dbTag->getId());
$this->notetagmapper->delete($hnotetag);
}
}
// Add new tags and update relations.
foreach ($tags as $tag) {
if (!$this->tagmapper->tagExists($this->userId, $tag['name'])) {
$htag = new Tag();
$htag->setName($tag['name']);
$htag->setUserId($this->userId);
$htag = $this->tagmapper->insert($htag);
}
else {
$htag = $this->tagmapper->getTag($this->userId, $tag['name']);
}
if (!$this->notetagmapper->noteTagExists($this->userId, $id, $htag->getId())) {
$noteTag = new NoteTag();
$noteTag->setNoteId($id);
$noteTag->setTagId($htag->getId());
$noteTag->setUserId($this->userId);
$this->notetagmapper->insert($noteTag);
}
}
// 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());
// Update note.
$newnote = $this->notemapper->update($note);
// Fill new tags
$newnote->setTags($this->tagmapper->getTagsForNote($this->userId, $newnote->getId()));
// Remove old color if necessary
if (($oldcolorid !== $hcolor->getId()) &&
(!$this->notemapper->colorIdCount($oldcolorid))) {
$oldcolor = $this->colormapper->find($oldcolorid);
$this->colormapper->delete($oldcolor);
}
// Purge orphan tags.
$this->tagmapper->dropOld();
return new DataResponse($newnote);
}
/**
* @NoAdminRequired
*
* @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();
$this->notesharemapper->deleteByNoteId($note->getId());
// 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);
}
/**
* @NoAdminRequired
*/
public function getUserGroupsAndUsersWithShare($noteId) {
$userMgr = \OC::$server->getUserManager();
$grpMgr = \OC::$server->getGroupManager();
$users = array();
$groups = array();
if($grpMgr->isAdmin($this->userId)) {
$igroups = $grpMgr->search("");
$iusers = $userMgr->search("");
foreach($igroups as $g) {
$groups[] = $g->getGID();
}
foreach($iusers as $u) {
$users[] = $u->getUID();
}
} else {
$igroups = $grpMgr->getUserGroups($userMgr->get($this->userId));
foreach($igroups as $g) {
$iusers = $g->getUsers();
foreach($iusers as $u) {
$users[] = $u->getUID();
}
$groups[] = $g->getGID();
}
}
$users = array_unique($users);
if(($i = array_search($this->userId, $users)) !== false) {
unset($users[$i]);
}
$pos_users = array();
$pos_groups = array();
$shares = $this->notesharemapper->getSharesForNote($noteId);
foreach($shares as $s) {
$shareType = $s->getSharedUser();
if(strlen($shareType) !== 0) {
if(($i = array_search($shareType, $users)) !== false) {
unset($users[$i]);
$pos_users[] = $shareType;
}
} else {
$shareType = $s->getSharedGroup();
if(($i = array_search($shareType, $groups)) !== false) {
unset($groups[$i]);
$pos_groups[] = $shareType;
}
}
}
$params = array('groups' => $groups, 'users' => $users, 'posGroups' => $pos_groups, 'posUsers' => $pos_users);
return new JSONResponse($params);
}
/**
* @NoAdminRequired
*/
public function addGroupShare($groupId, $noteId) {
$share = new NoteShare();
$share->setSharedGroup($groupId);
$share->setNoteId($noteId);
$this->notesharemapper->insert($share);
}
/**
* @NoAdminRequired
*/
public function removeGroupShare($groupId, $noteId) {
$share = $this->notesharemapper->findByNoteAndGroup($noteId, $groupId);
$this->notesharemapper->delete($share);
}
/**
* @NoAdminRequired
*/
public function addUserShare($userId, $noteId) {
$share = new NoteShare();
$share->setSharedUser($userId);
$share->setNoteId($noteId);
$this->notesharemapper->insert($share);
}
/**
* @NoAdminRequired
*/
public function removeUserShare($userId, $noteId) {
$share = $this->notesharemapper->findByNoteAndUser($noteId, $userId);
$this->notesharemapper->delete($share);
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* ownCloud - quicknotes
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Matias De lellis <mati86dl@gmail.com>
* @copyright Matias De lellis 2016
*/
namespace OCA\QuickNotes\Controller;
use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
class PageController extends Controller {
private $userId;
public function __construct($AppName, IRequest $request, $UserId){
parent::__construct($AppName, $request);
$this->userId = $UserId;
}
/**
* CAUTION: the @Stuff turns off security checks; for this page no admin is
* required and no CSRF check. If you don't know what CSRF is, read
* it up in the docs or you might create a security hole. This is
* basically the only required method to add this exemption, don't
* add it to any other method if you don't exactly know what it does
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index() {
$params = ['user' => $this->userId];
return new TemplateResponse('quicknotes', 'main', $params); // templates/main.php
}
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* Nextcloud - quicknotes
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Matias De lellis <mati86dl@gmail.com>
* @copyright Matias De lellis 2019
*/
namespace OCA\QuickNotes\Controller;
use OCP\IRequest;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Controller;
use OCA\QuickNotes\Db\Tag;
use OCA\QuickNotes\Db\TagMapper;
class TagController extends Controller {
private $tagmapper;
private $userId;
public function __construct($AppName,
IRequest $request,
TagMapper $tagmapper,
$UserId)
{
parent::__construct($AppName, $request);
$this->tagmapper = $tagmapper;
$this->userId = $UserId;
}
/**
* @NoAdminRequired
*/
public function index() {
$notes = $this->tagmapper->findAll($this->userId);
return new DataResponse($notes);
}
}

19
lib/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
lib/Db/ColorMapper.php Normal file
View File

@@ -0,0 +1,39 @@
<?php
namespace OCA\QuickNotes\Db;
use OCP\IDBConnection;
use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Db\DoesNotExistException;
class ColorMapper extends Mapper {
public function __construct(IDBConnection $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;
}
}

39
lib/Db/Note.php Normal file
View File

@@ -0,0 +1,39 @@
<?php
namespace OCA\QuickNotes\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class Note extends Entity implements JsonSerializable {
protected $title;
protected $content;
protected $timestamp;
protected $colorId;
protected $userId;
protected $sharedWith;
protected $isShared;
protected $tags;
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,
'userid' => $this->userId,
'sharedwith' => $this->sharedWith,
'isshared' => $this->isShared,
'tags' => $this->tags
];
}
}

44
lib/Db/NoteMapper.php Normal file
View File

@@ -0,0 +1,44 @@
<?php
namespace OCA\QuickNotes\Db;
use OCP\IDBConnection;
use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Db\DoesNotExistException;
class NoteMapper extends Mapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'quicknotes_notes', '\OCA\QuickNotes\Db\Note');
}
/**
* @param int $id
* @param string $userId
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
* @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]);
}
public function findAll($userId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_notes WHERE user_id = ?';
return $this->findEntities($sql, [$userId]);
}
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'];
}
}

22
lib/Db/NoteShare.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
namespace OCA\QuickNotes\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class NoteShare extends Entity implements JsonSerializable {
protected $noteId;
protected $sharedUser;
protected $sharedGroup;
public function jsonSerialize() {
return [
'id' => $this->id,
'noteid' => $this->noteId,
'shareduser' => $this->sharedUser,
'sharedgroup' => $this->sharedGroup
];
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace OCA\QuickNotes\Db;
use OCP\IDBConnection;
use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Db\DoesNotExistException;
class NoteShareMapper extends Mapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'quicknotes_shares', '\OCA\QuickNotes\Db\NoteShare');
}
/*public function find($id, $userId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_shares WHERE id = ? AND user_id = ?';
return $this->findEntity($sql, [$id, $userId]);
}*/
public function findForUser($userId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_shares WHERE shared_user = ?';
return $this->findEntities($sql, [$userId]);
}
public function findForGroup($groupId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_shares WHERE shared_group = ?';
return $this->findEntities($sql, [$groupId]);
}
public function findByNoteAndUser($noteId, $userId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_shares WHERE shared_user = ? AND note_id = ?';
return $this->findEntity($sql, [$userId, $noteId]);
}
public function findByNoteAndGroup($noteId, $groupId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_shares WHERE shared_group = ? AND note_id = ?';
return $this->findEntity($sql, [$groupId, $noteId]);
}
public function getSharesForNote($noteId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_shares WHERE note_id = ?';
return $this->findEntities($sql, [$noteId]);
}
public function deleteByNoteId($noteId) {
$sql = 'DELETE FROM *PREFIX*quicknotes_shares WHERE note_id = ?';
$this->execute($sql, [$noteId]);
}
}

23
lib/Db/NoteTag.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
namespace OCA\QuickNotes\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class NoteTag extends Entity implements JsonSerializable {
protected $noteId;
protected $tagId;
protected $userId;
public function jsonSerialize() {
return [
'id' => $this->id,
'noteid' => $this->noteId,
'tagid' => $this->tagId,
'userid' => $this->userId
];
}
}

38
lib/Db/NoteTagMapper.php Normal file
View File

@@ -0,0 +1,38 @@
<?php
namespace OCA\QuickNotes\Db;
use OCP\IDBConnection;
use OCP\AppFramework\Db\Mapper;
class NoteTagMapper extends Mapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'quicknotes_note_tags', '\OCA\QuickNotes\Db\NoteTag');
}
public function find($id, $userId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_note_tags WHERE id = ? AND user_id = ?';
return $this->findEntity($sql, [$id, $userId]);
}
public function findAll($userId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_note_tags WHERE user_id = ?';
return $this->findEntities($sql, [$userId]);
}
public function findNoteTag($userId, $noteId, $tagId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_note_tags WHERE user_id = ? AND note_id = ? AND tag_id = ?';
return $this->findEntity($sql, [$userId, $noteId, $tagId]);
}
public function noteTagExists($userId, $noteId, $tagId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_note_tags WHERE user_id = ? AND note_id = ? AND tag_id = ?';
try {
return $this->findEntities($sql, [$userId, $noteId, $tagId]);
} catch (DoesNotExistException $e) {
return false;
}
return true;
}
}

21
lib/Db/Tag.php Normal file
View File

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

51
lib/Db/TagMapper.php Normal file
View File

@@ -0,0 +1,51 @@
<?php
namespace OCA\QuickNotes\Db;
use OCP\IDBConnection;
use OCP\AppFramework\Db\Mapper;
class TagMapper extends Mapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'quicknotes_tags', '\OCA\QuickNotes\Db\Tag');
}
public function find($id, $userId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_tags WHERE id = ? AND user_id = ?';
return $this->findEntity($sql, [$id, $userId]);
}
public function findAll($userId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_tags WHERE user_id = ?';
return $this->findEntities($sql, [$userId]);
}
public function getTagsForNote($userId, $noteId) {
$sql = 'SELECT T.id, T.name FROM *PREFIX*quicknotes_tags T ';
$sql.= 'INNER JOIN *PREFIX*quicknotes_note_tags NT ';
$sql.= 'ON T.id = NT.tag_id ';
$sql.= 'WHERE NT.user_id = ? AND NT.note_id = ?';
return $this->findEntities($sql, [$userId, $noteId]);
}
public function getTag($userId, $name) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_tags WHERE user_id = ? AND name = ?';
return $this->findEntity($sql, [$userId, $name]);
}
public function tagExists($userId, $name) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_tags WHERE user_id = ? AND name = ?';
try {
return $this->findEntities($sql, [$userId, $name]);
} catch (DoesNotExistException $e) {
return false;
}
return true;
}
public function dropOld () {
$sql = 'DELETE FROM *PREFIX*quicknotes_tags WHERE ';
$sql.= 'id NOT IN (SELECT tag_id FROM *PREFIX*quicknotes_note_tags)';
return $this->execute($sql, []);
}
}

24
lib/Db/Task.php Normal file
View File

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

23
lib/Db/TaskMapper.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
namespace OCA\QuickNotes\Db;
use OCP\IDBConnection;
use OCP\AppFramework\Db\Mapper;
class TaskMapper extends Mapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'quicknotes_tasks', '\OCA\QuickNotes\Db\Tasks');
}
public function find($id, $noteId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_tasks WHERE id = ? AND note_id = ?';
return $this->findEntity($sql, [$id, $noteId]);
}
public function findAll($noteId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_tasks WHERE note_id = ?';
return $this->findEntities($sql, [$noteId]);
}
}

View File

@@ -0,0 +1,192 @@
<?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 Version000204Date20200530211356 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
}
/**
* @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();
if (!$schema->hasTable('quicknotes_notes')) {
$table = $schema->createTable('quicknotes_notes');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 8,
'unsigned' => true,
]);
$table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 200,
'default' => '',
]);
$table->addColumn('title', 'string', [
'notnull' => true,
'length' => 200,
'default' => '',
]);
$table->addColumn('content', 'text', [
'notnull' => true,
'default' => '',
]);
$table->addColumn('timestamp', 'integer', [
'notnull' => true,
'length' => 4,
'default' => 0,
]);
$table->addColumn('color_id', 'bigint', [
'notnull' => true,
'length' => 8,
]);
$table->setPrimaryKey(['id']);
}
if (!$schema->hasTable('quicknotes_colors')) {
$table = $schema->createTable('quicknotes_colors');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 8,
'unsigned' => true,
]);
$table->addColumn('color', 'string', [
'notnull' => false,
]);
$table->setPrimaryKey(['id']);
}
if (!$schema->hasTable('quicknotes_tags')) {
$table = $schema->createTable('quicknotes_tags');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 8,
'unsigned' => true,
]);
$table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 200,
'default' => '',
]);
$table->addColumn('name', 'string', [
'notnull' => true,
'length' => 200,
'default' => '',
]);
$table->setPrimaryKey(['id']);
}
if (!$schema->hasTable('quicknotes_note_tags')) {
$table = $schema->createTable('quicknotes_note_tags');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 8,
'unsigned' => true,
]);
$table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 200,
'default' => '',
]);
$table->addColumn('note_id', 'bigint', [
'notnull' => true,
'length' => 8,
'unsigned' => true,
]);
$table->addColumn('tag_id', 'bigint', [
'notnull' => true,
'length' => 8,
'unsigned' => true,
]);
$table->setPrimaryKey(['id']);
}
if (!$schema->hasTable('quicknotes_tasks')) {
$table = $schema->createTable('quicknotes_tasks');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 8,
'unsigned' => true,
]);
$table->addColumn('note_id', 'bigint', [
'notnull' => true,
'length' => 8,
'unsigned' => true,
]);
$table->addColumn('description', 'string', [
'notnull' => true,
'length' => 200,
'default' => '',
]);
$table->addColumn('done', 'boolean', [
'notnull' => true,
'default' => false,
]);
$table->addColumn('ordering', 'integer', [
'notnull' => true,
'default' => 0,
]);
$table->setPrimaryKey(['id']);
}
if (!$schema->hasTable('quicknotes_shares')) {
$table = $schema->createTable('quicknotes_shares');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 8,
'unsigned' => true,
]);
$table->addColumn('note_id', 'bigint', [
'notnull' => true,
'length' => 8,
'unsigned' => true,
]);
$table->addColumn('shared_user', 'string', [
'notnull' => false,
'length' => 200,
]);
$table->addColumn('shared_group', 'string', [
'notnull' => false,
'length' => 200,
]);
$table->setPrimaryKey(['id']);
}
return $schema;
}
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
}
}

View File

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

View File

@@ -0,0 +1,77 @@
<?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

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