first commit
This commit is contained in:
33
lib/Controller/Errors.php
Normal file
33
lib/Controller/Errors.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\MyWiki\Controller;
|
||||
|
||||
use Closure;
|
||||
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
|
||||
use OCA\MyWiki\Service\NotFoundException;
|
||||
use OCA\MyWiki\Service\ReadOnlyException;
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
protected function handleReadOnly (Closure $callback) {
|
||||
try {
|
||||
return new DataResponse($callback());
|
||||
} catch(ReadOnlyException $e) {
|
||||
$message = ['message' => $e->getMessage()];
|
||||
return new DataResponse($message, Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
31
lib/Controller/PageController.php
Normal file
31
lib/Controller/PageController.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace OCA\MyWiki\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() {
|
||||
return new TemplateResponse('mywiki', 'index'); // templates/index.php
|
||||
}
|
||||
|
||||
}
|
81
lib/Controller/WikiController.php
Normal file
81
lib/Controller/WikiController.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
namespace OCA\MyWiki\Controller;
|
||||
|
||||
use OCP\IRequest;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\AppFramework\Controller;
|
||||
|
||||
use OCA\MyWiki\Service\WikiService;
|
||||
|
||||
class WikiController extends Controller {
|
||||
|
||||
private $service;
|
||||
private $userId;
|
||||
|
||||
use Errors;
|
||||
|
||||
public function __construct(string $AppName, IRequest $request, WikiService $service, $UserId){
|
||||
parent::__construct($AppName, $request);
|
||||
$this->service = $service;
|
||||
$this->userId = $UserId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function index() {
|
||||
return new DataResponse($this->service->test($this->userId));
|
||||
|
||||
return new DataResponse($this->service->findAll($this->userId));
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @param int $id
|
||||
*/
|
||||
public function show(int $id) {
|
||||
return $this->handleNotFound(function () use ($id) {
|
||||
return $this->service->find($id, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @param string $title
|
||||
* @param string $fileId
|
||||
*/
|
||||
public function create(string $title, int $fileId) {
|
||||
return $this->handleReadOnly(function () use ($title, $fileId) {
|
||||
return $this->service->create($title, $fileId, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @param int $id
|
||||
* @param string $title
|
||||
*/
|
||||
public function update(int $id, string $title) {
|
||||
return $this->handleNotFound(function () use ($id, $title) {
|
||||
return $this->service->update($id, $title, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @param int $id
|
||||
* @param bool $removeFiles
|
||||
*/
|
||||
public function destroy(int $id, bool $removeFiles) {
|
||||
return $this->handleNotFound(function () use ($id, $removeFiles) {
|
||||
return $this->service->delete($id, $removeFiles, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user