mirror of
https://github.com/JanGross/quicknotes.git
synced 2025-11-30 23:37:16 +01:00
Initial release
This commit is contained in:
77
service/noteservice.php
Normal file
77
service/noteservice.php
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
4
service/notfoundexception.php
Normal file
4
service/notfoundexception.php
Normal file
@@ -0,0 +1,4 @@
|
||||
<?php
|
||||
namespace OCA\QuickNotes\Service;
|
||||
|
||||
class NotFoundException extends ServiceException {}
|
||||
6
service/serviceexception.php
Normal file
6
service/serviceexception.php
Normal file
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
namespace OCA\QuickNotes\Service;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ServiceException extends Exception {}
|
||||
Reference in New Issue
Block a user