WikiPages controls
This commit is contained in:
88
lib/Controller/WikiPageController.php
Normal file
88
lib/Controller/WikiPageController.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
namespace OCA\MyWiki\Controller;
|
||||
|
||||
use OCP\IRequest;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\AppFramework\Controller;
|
||||
|
||||
use OCA\MyWiki\Service\WikiPageService;
|
||||
|
||||
class WikiPageController extends Controller {
|
||||
|
||||
private $service;
|
||||
private $userId;
|
||||
|
||||
use Errors;
|
||||
|
||||
public function __construct(string $AppName, IRequest $request, WikiPageService $service, $UserId){
|
||||
parent::__construct($AppName, $request);
|
||||
$this->service = $service;
|
||||
$this->userId = $UserId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @param int $wikiId
|
||||
*/
|
||||
public function index(int $wikiId) {
|
||||
return $this->handleNotFound(function () use ($wikiId) {
|
||||
return $this->service->findAll($wikiId, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @param int $wikiId
|
||||
* @param int $id
|
||||
*/
|
||||
public function show(int $wikiId, int $id) {
|
||||
return $this->handleNotFound(function () use ($wikiId, $id) {
|
||||
return $this->service->find($wikiId, $id, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @param int $wikiId
|
||||
* @param int $parentFolderId
|
||||
* @param string $title
|
||||
* @param ?string $content
|
||||
*/
|
||||
public function create(int $wikiId, int $pid, string $title, ?string $content) {
|
||||
return $this->handleReadOnly(function () use ($wikiId, $pid, $title, $content) {
|
||||
return $this->service->create($wikiId, $pid, $title, $content, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @param int $wikiId
|
||||
* @param int $id
|
||||
* @param string $title
|
||||
* @param string $content
|
||||
*/
|
||||
public function update(int $wikiId, int $id, ?string $title, ?string $content) {
|
||||
return $this->handleNotFound(function () use ($wikiId, $id, $title, $content) {
|
||||
return $this->service->update($wikiId, $id, $title, $content, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @param int $wikiId
|
||||
* @param int $id
|
||||
* @param bool $removeFiles
|
||||
*/
|
||||
public function destroy(int $wikiId, int $id) {
|
||||
return $this->handleNotFound(function () use ($wikiId, $id) {
|
||||
return $this->service->delete($wikiId, $id, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
98
lib/Service/WikiPageService.php
Normal file
98
lib/Service/WikiPageService.php
Normal file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
namespace OCA\MyWiki\Service;
|
||||
|
||||
use Exception;
|
||||
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
||||
use OCP\Files\IRootFolder;
|
||||
use OCA\MyWiki\Db\Wiki;
|
||||
use OCA\MyWiki\Db\WikiMapper;
|
||||
use OCA\MyWiki\Helper\WikiHelper;
|
||||
|
||||
class WikiPageService {
|
||||
|
||||
private $mapper;
|
||||
private $userId;
|
||||
private $wikiHelper;
|
||||
|
||||
public function __construct(WikiMapper $mapper, IRootFolder $storage, $UserId) {
|
||||
$this->mapper = $mapper;
|
||||
$this->userId = $UserId;
|
||||
|
||||
$userFolder = $storage->getUserFolder($this->userId);
|
||||
$this->wikiHelper = new WikiHelper($userFolder);
|
||||
}
|
||||
|
||||
public function findAll(int $wikiId, string $userId) {
|
||||
try {
|
||||
$wiki = $this->mapper->find($wikiId, $userId);
|
||||
return $this->wikiHelper->setFolderId($wiki->getFileId())->getWikiData();
|
||||
} catch(Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
}
|
||||
|
||||
private function handleException ($e) {
|
||||
if ($e instanceof DoesNotExistException ||
|
||||
$e instanceof MultipleObjectsReturnedException) {
|
||||
throw new NotFoundException($e->getMessage());
|
||||
} else {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function find(int $wikiId, int $id, string $userId) {
|
||||
echo "\nwikiId: $wikiId";
|
||||
echo "\nid: $id";
|
||||
echo "\nuserId: $userId";
|
||||
die();
|
||||
}
|
||||
|
||||
public function create(int $wikiId, int $parentFolderId, string $title, ?string $content, string $userId):array {
|
||||
try {
|
||||
$wiki = $this->mapper->find($wikiId, $userId);
|
||||
$pageId = $this->wikiHelper->setFolderId($wiki->getFileId())->add($parentFolderId,$title,$content);
|
||||
if ( $pageId <= 0 ) {
|
||||
throw new ReadOnlyException('Error renaming wiki page');
|
||||
}
|
||||
} catch(Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
return ["pageId"=>$pageId];
|
||||
}
|
||||
|
||||
public function update(int $wikiId, int $id, ?string $title, ?string $content, string $userId) {
|
||||
try {
|
||||
$wiki = $this->mapper->find($wikiId, $userId);
|
||||
|
||||
$this->wikiHelper->setFolderId($wiki->getFileId());
|
||||
if (!is_null($title)) {
|
||||
if ( !$this->wikiHelper->rename($id, $title) ) {
|
||||
throw new ReadOnlyException('Error renaming wiki page');
|
||||
}
|
||||
}
|
||||
if (!is_null($content)) {
|
||||
if ( !$this->wikiHelper->update($id, $content) ) {
|
||||
throw new ReadOnlyException('Error updating wiki content');
|
||||
}
|
||||
}
|
||||
} catch(Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function delete(int $wikiId, int $id, string $userId) {
|
||||
try {
|
||||
$wiki = $this->mapper->find($wikiId, $userId);
|
||||
if ( !$this->wikiHelper->setFolderId($wiki->getFileId())->delete($id) ) {
|
||||
throw new ReadOnlyException('Error deleting wiki page');
|
||||
}
|
||||
} catch(Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user