summaryrefslogtreecommitdiffstats
path: root/lib/Controller
diff options
context:
space:
mode:
authorPaul Tirk <paultirk@paultirk.com>2021-01-09 19:05:40 +0100
committerSean Molenaar <SMillerDev@users.noreply.github.com>2021-04-08 10:23:11 +0200
commit0d7d3cdfb4af9e5f369e229a0eb05d506f801da2 (patch)
treeeddb5f6e2c73598d64701aafab85b75edf54a857 /lib/Controller
parentdff855fba5ce7e43d0ae4b68fd53eeab43dc7b5e (diff)
move v2 api responses into existing php traits
Signed-off-by: Paul Tirk <paultirk@paultirk.com>
Diffstat (limited to 'lib/Controller')
-rw-r--r--lib/Controller/ApiPayloadTrait.php37
-rw-r--r--lib/Controller/ApiV2ResponseTrait.php69
-rw-r--r--lib/Controller/FolderApiV2Controller.php25
-rw-r--r--lib/Controller/JSONHttpErrorTrait.php15
4 files changed, 64 insertions, 82 deletions
diff --git a/lib/Controller/ApiPayloadTrait.php b/lib/Controller/ApiPayloadTrait.php
index 2bb31784e..c40ec9c25 100644
--- a/lib/Controller/ApiPayloadTrait.php
+++ b/lib/Controller/ApiPayloadTrait.php
@@ -1,8 +1,10 @@
<?php
-
namespace OCA\News\Controller;
+use \OCP\AppFramework\Http;
+use \OCP\AppFramework\Http\JSONResponse;
+
use OCA\News\Db\IAPI;
trait ApiPayloadTrait
@@ -33,4 +35,37 @@ trait ApiPayloadTrait
}
return $return;
}
+
+ /**
+ * Serialize an entity
+ *
+ * @param IAPI $data
+ *
+ * @return array
+ */
+ public function serializeEntityV2($data, bool $reduced = false): array
+ {
+ return $data->toAPI2($reduced);
+ }
+
+ /**
+ * Serialize array of entities
+ *
+ * @param array $data
+ *
+ * @return array
+ */
+ public function serializeEntitiesV2($data, bool $reduced = false): array
+ {
+ $return = [];
+ foreach ($data as $entity) {
+ $return[] = $entity->toAPI2($reduced);
+ }
+ return $return;
+ }
+
+ public function responseV2($data, $code = Http::STATUS_OK)
+ {
+ return new JSONResponse($data, $code);
+ }
}
diff --git a/lib/Controller/ApiV2ResponseTrait.php b/lib/Controller/ApiV2ResponseTrait.php
deleted file mode 100644
index 3d803b28f..000000000
--- a/lib/Controller/ApiV2ResponseTrait.php
+++ /dev/null
@@ -1,69 +0,0 @@
-<?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\AppFramework\Http;
-use \OCP\AppFramework\Http\JSONResponse;
-
-use \OCA\News\Db\IAPI;
-
-trait ApiV2ResponseTrait
-{
- /**
- * Serialize an entity
- *
- * @param IAPI $data
- *
- * @return array
- */
- public function serializeEntity($data, bool $reduced = false): array
- {
- return $data->toAPI2($reduced);
- }
-
- /**
- * Serialize array of entities
- *
- * @param array $data
- *
- * @return array
- */
- public function serializeEntities($data, bool $reduced = false): array
- {
- $return = [];
- foreach ($data as $entity) {
- $return[] = $entity->toAPI2($reduced);
- }
- return $return;
- }
-
- public function response($data, $code = Http::STATUS_OK)
- {
- return new JSONResponse($data, $code);
- }
-
- /**
- * @param \Exception $exception
- * @param int $code
- * @return \OCP\AppFramework\Http\JSONResponse
- */
- public function errorResponse(\Exception $exception, $code)
- {
- return new JSONResponse([
- 'error' => [
- 'code' => $exception->getCode(),
- 'message' => $exception->getMessage()
- ]
- ], $code);
- }
-}
diff --git a/lib/Controller/FolderApiV2Controller.php b/lib/Controller/FolderApiV2Controller.php
index 683169006..b4ba8fcda 100644
--- a/lib/Controller/FolderApiV2Controller.php
+++ b/lib/Controller/FolderApiV2Controller.php
@@ -23,7 +23,8 @@ use \OCA\News\Service\Exceptions\ServiceValidationException;
class FolderApiV2Controller extends ApiController
{
- use ApiV2ResponseTrait;
+ use ApiPayloadTrait;
+ use JSONHttpErrorTrait;
private $folderService;
private $itemService;
@@ -52,16 +53,16 @@ class FolderApiV2Controller extends ApiController
{
try {
$this->folderService->purgeDeleted($this->getUserId(), false);
- $responseData = $this->serializeEntity(
+ $responseData = $this->serializeEntityV2(
$this->folderService->create($this->getUserId(), $name)
);
- return $this->response([
+ return $this->responseV2([
'folder' => $responseData
]);
} catch (ServiceValidationException $ex) {
- return $this->errorResponse($ex, Http::STATUS_BAD_REQUEST);
+ return $this->errorResponseV2($ex, Http::STATUS_BAD_REQUEST);
} catch (ServiceConflictException $ex) {
- return $this->errorResponse($ex, Http::STATUS_CONFLICT);
+ return $this->errorResponseV2($ex, Http::STATUS_CONFLICT);
}
}
@@ -79,14 +80,14 @@ class FolderApiV2Controller extends ApiController
try {
$response = $this->folderService->rename($this->getUserId(), $folderId, $name);
} catch (ServiceValidationException $ex) {
- return $this->errorResponse($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
+ return $this->errorResponseV2($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
} catch (ServiceConflictException $ex) {
- return $this->errorResponse($ex, Http::STATUS_CONFLICT);
+ return $this->errorResponseV2($ex, Http::STATUS_CONFLICT);
} catch (ServiceNotFoundException $ex) {
- return $this->errorResponse($ex, Http::STATUS_NOT_FOUND);
+ return $this->errorResponseV2($ex, Http::STATUS_NOT_FOUND);
}
- return $this->response([
+ return $this->responseV2([
'folder' => $response
]);
}
@@ -103,14 +104,14 @@ class FolderApiV2Controller extends ApiController
public function delete($folderId)
{
try {
- $responseData = $this->serializeEntity(
+ $responseData = $this->serializeEntityV2(
$this->folderService->delete($this->getUserId(), $folderId)
);
- return $this->response([
+ return $this->responseV2([
'folder' => $responseData
]);
} catch (ServiceNotFoundException $ex) {
- return $this->errorResponse($ex, Http::STATUS_NOT_FOUND);
+ return $this->errorResponseV2($ex, Http::STATUS_NOT_FOUND);
}
}
}
diff --git a/lib/Controller/JSONHttpErrorTrait.php b/lib/Controller/JSONHttpErrorTrait.php
index e3e343f16..5c5d7a8cf 100644
--- a/lib/Controller/JSONHttpErrorTrait.php
+++ b/lib/Controller/JSONHttpErrorTrait.php
@@ -24,4 +24,19 @@ trait JSONHttpErrorTrait
{
return new JSONResponse(['message' => $exception->getMessage()], $code);
}
+
+ /**
+ * @param \Exception $exception
+ * @param int $code
+ * @return \OCP\AppFramework\Http\JSONResponse
+ */
+ public function errorResponseV2(\Exception $exception, $code)
+ {
+ return new JSONResponse([
+ 'error' => [
+ 'code' => $exception->getCode(),
+ 'message' => $exception->getMessage()
+ ]
+ ], $code);
+ }
}