diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-04-09 16:10:48 +0200 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-04-09 22:52:27 +0200 |
commit | 643fa4624dd7ba2db1349f16131bf330aeee3387 (patch) | |
tree | 73bc787a3eb1ed5f53ab932187546c980ac50bb5 /controller/folderapicontroller.php | |
parent | efe9db1e064c9736fe35c27040ee60fa28ca8d4d (diff) |
port to internal controller, some routes are still broken
Diffstat (limited to 'controller/folderapicontroller.php')
-rw-r--r-- | controller/folderapicontroller.php | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/controller/folderapicontroller.php b/controller/folderapicontroller.php new file mode 100644 index 000000000..0954380c3 --- /dev/null +++ b/controller/folderapicontroller.php @@ -0,0 +1,170 @@ +<?php + +/** +* ownCloud - News +* +* @author Alessandro Cosentino +* @author Bernhard Posselt +* @copyright 2012 Alessandro Cosentino cosenal@gmail.com +* @copyright 2012 Bernhard Posselt dev@bernhard-posselt.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +namespace OCA\News\Controller; + +use \OCP\IRequest; +use \OCP\AppFramework\Controller; +use \OCP\AppFramework\Http; +use \OCP\AppFramework\Http\JSONResponse; + +use \OCA\News\Core\API; +use \OCA\News\BusinessLayer\FolderBusinessLayer; +use \OCA\News\BusinessLayer\ItemBusinessLayer; +use \OCA\News\BusinessLayer\BusinessLayerException; +use \OCA\News\BusinessLayer\BusinessLayerConflictException; +use \OCA\News\BusinessLayer\BusinessLayerValidationException; + + +class FolderApiController extends Controller { + + private $folderBusinessLayer; + private $itemBusinessLayer; + private $api; + + public function __construct(API $api, + IRequest $request, + FolderBusinessLayer $folderBusinessLayer, + ItemBusinessLayer $itemBusinessLayer){ + parent::__construct($api->getAppName(), $request); + $this->folderBusinessLayer = $folderBusinessLayer; + $this->itemBusinessLayer = $itemBusinessLayer; + $this->api = $api; + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function index() { + $userId = $this->api->getUserId(); + $result = array( + 'folders' => array() + ); + + foreach ($this->folderBusinessLayer->findAll($userId) as $folder) { + array_push($result['folders'], $folder->toAPI()); + } + + return new JSONResponse($result); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function create() { + $userId = $this->api->getUserId(); + $folderName = $this->params('name'); + $result = array( + 'folders' => array() + ); + + try { + $this->folderBusinessLayer->purgeDeleted($userId, false); + $folder = $this->folderBusinessLayer->create($folderName, $userId); + array_push($result['folders'], $folder->toAPI()); + + return new JSONResponse($result); + + } catch(BusinessLayerValidationException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_UNPROCESSABLE_ENTITY); + + } catch(BusinessLayerConflictException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_CONFLICT); + } + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function delete() { + $userId = $this->api->getUserId(); + $folderId = (int) $this->params('folderId'); + + try { + $this->folderBusinessLayer->delete($folderId, $userId); + return new JSONResponse(); + } catch(BusinessLayerException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function update() { + $userId = $this->api->getUserId(); + $folderId = (int) $this->params('folderId'); + $folderName = $this->params('name'); + + try { + $this->folderBusinessLayer->rename($folderId, $folderName, $userId); + return new JSONResponse(); + + } catch(BusinessLayerValidationException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_UNPROCESSABLE_ENTITY); + + } catch(BusinessLayerConflictException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_CONFLICT); + + } catch(BusinessLayerException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function read() { + $userId = $this->api->getUserId(); + $folderId = (int) $this->params('folderId'); + $newestItemId = (int) $this->params('newestItemId'); + + $this->itemBusinessLayer->readFolder($folderId, $newestItemId, $userId); + return new JSONResponse(); + } + + +} |