move v2 api responses into existing php traits

Signed-off-by: Paul Tirk <paultirk@paultirk.com>
This commit is contained in:
Paul Tirk 2021-01-09 19:05:40 +01:00 committed by Sean Molenaar
parent dff855fba5
commit 0d7d3cdfb4
4 changed files with 64 additions and 82 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}