implementing db backend ...

This commit is contained in:
Jonas Heinrich 2017-01-08 09:37:11 +01:00
parent ee1a3bfc59
commit 1253c560f3
3 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,52 @@
<?php
namespace OCA\OwnNotes\Controller;
use OCP\IRequest;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCA\OwnNotes\Service\NoteService;
class NoteController extends Controller {
private $service;
private $userId;
use Errors;
public function __construct($AppName, IRequest $request,
NoteService $service, $UserId){
parent::__construct($AppName, $request);
$this->service = $service;
$this->userId = $UserId;
}
/**
* @NoAdminRequired
*/
public function favorites() {
return new DataResponse($this->service->findAll($this->userId));
}
/**
* @NoAdminRequired
*
* @param string $title
* @param string $content
*/
public function fav($id) {
return $this->service->create($id, $this->userId);
}
/**
* @NoAdminRequired
*
* @param int $id
*/
public function unfav($id) {
return $this->handleNotFound(function () use ($id) {
return $this->service->delete($id, $this->userId);
});
}
}

BIN
img/fav_hover.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

51
service/streamservice.php Normal file
View File

@ -0,0 +1,51 @@
<?php
namespace OCA\OwnNotes\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 fav($id, $userId) {
$station = new Station();
$station->setId($id);
$station->setUserId($userId);
return $this->mapper->insert($station);
}
public function unfav($id, $userId) {
try {
$station = $this->mapper->find($id, $userId);
$this->mapper->delete($station);
return $station;
} catch(Exception $e) {
$this->handleException($e);
}
}
}