1
0
mirror of https://github.com/chylex/Nextcloud-News.git synced 2025-04-30 16:34:08 +02:00
Nextcloud-News/lib/Controller/FolderApiV2Controller.php
Paul Tirk 05b7ed7994 remove unused service exceptions & allow equally named folders
Signed-off-by: Paul Tirk <paultirk@paultirk.com>
2021-04-08 10:23:11 +02:00

112 lines
2.9 KiB
PHP

<?php
/**
* Nextcloud - News
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Paul Tirk <paultirk@paultirk.com>
* @copyright 2020 Paul Tirk
*/
namespace OCA\News\Controller;
use \OCP\IRequest;
use \OCP\IUserSession;
use \OCP\AppFramework\Http;
use \OCA\News\Service\FolderServiceV2;
use \OCA\News\Service\ItemServiceV2;
use \OCA\News\Service\Exceptions\ServiceNotFoundException;
class FolderApiV2Controller extends ApiController
{
use ApiPayloadTrait;
use JSONHttpErrorTrait;
private $folderService;
private $itemService;
public function __construct(
IRequest $request,
IUserSession $userSession,
FolderServiceV2 $folderService,
ItemServiceV2 $itemService
) {
parent::__construct($request, $userSession);
$this->folderService = $folderService;
$this->itemService = $itemService;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
*
* @param string $name
* @return array|mixed|\OCP\AppFramework\Http\JSONResponse
*/
public function create($name)
{
try {
$this->folderService->purgeDeleted($this->getUserId(), false);
$responseData = $this->serializeEntityV2(
$this->folderService->create($this->getUserId(), $name)
);
return $this->responseV2([
'folder' => $responseData
]);
} catch (ServiceValidationException $ex) {
return $this->errorResponseV2($ex, Http::STATUS_BAD_REQUEST);
} catch (ServiceConflictException $ex) {
return $this->errorResponseV2($ex, Http::STATUS_CONFLICT);
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
* @param int $folderId
* @param string $name
* @return array|\OCP\AppFramework\Http\JSONResponse
*/
public function update($folderId, $name)
{
$response = null;
try {
$response = $this->folderService->rename($this->getUserId(), $folderId, $name);
} catch (ServiceNotFoundException $ex) {
return $this->errorResponseV2($ex, Http::STATUS_NOT_FOUND);
}
return $this->responseV2([
'folder' => $response
]);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
*
* @param int $folderId
* @return array|\OCP\AppFramework\Http\JSONResponse
*/
public function delete($folderId)
{
try {
$responseData = $this->serializeEntityV2(
$this->folderService->delete($this->getUserId(), $folderId)
);
return $this->responseV2([
'folder' => $responseData
]);
} catch (ServiceNotFoundException $ex) {
return $this->errorResponseV2($ex, Http::STATUS_NOT_FOUND);
}
}
}