. */ namespace Poniverse\Ponyfm\Http\Controllers; use Poniverse\Ponyfm\Commands\CommandBase; use Response; abstract class ApiControllerBase extends Controller { /** * NOTE: This function is used by the v1 API. If the response JSON format * it returns changes, don't break the API! * * @param CommandBase $command * @return \Illuminate\Http\JsonResponse */ protected function execute(CommandBase $command) { if (!$command->authorize()) { return $this->notAuthorized(); } $result = $command->execute(); if ($result->didFail()) { return Response::json([ 'message' => 'Validation failed', 'errors' => $result->getMessages() ], $result->getStatusCode()); } return Response::json($result->getResponse(), $result->getStatusCode()); } public function notAuthorized() { return Response::json(['message' => 'You may not do this!'], 403); } public function notFound($message) { return Response::json(['message' => $message], 403); } }