From 7f4b80b6c93b2cf74643fcbc0b311f23111423cc Mon Sep 17 00:00:00 2001 From: Carlos Buenosvinos Date: Sun, 6 Mar 2022 21:48:17 +0100 Subject: [PATCH] [Chapter9] WIP --- .../Chapter7/CountFollowersController.php | 6 ++++-- .../Controller/Chapter7/GetTimelineController.php | 6 ++++-- .../Chapter7/SignUpAuthorController.php | 15 ++++++++++----- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/App/Controller/Chapter7/CountFollowersController.php b/src/App/Controller/Chapter7/CountFollowersController.php index da533f2..a0d333b 100644 --- a/src/App/Controller/Chapter7/CountFollowersController.php +++ b/src/App/Controller/Chapter7/CountFollowersController.php @@ -22,14 +22,14 @@ final class CountFollowersController extends AbstractController { $httpCode = Response::HTTP_ACCEPTED; $httpContent = [ - '_meta' => [], + 'meta' => [], 'data' => [], ]; + $query = null; try { $query = CountFollowersQuery::ofAuthor($authorId); $timeline = $queryBus->query($query); - $httpContent['_meta']['message_id'] = $query->messageId()?->toString(); $httpContent['data'] = $timeline; } catch (AuthorDoesNotExist $e) { $httpCode = Response::HTTP_NOT_FOUND; @@ -37,6 +37,8 @@ final class CountFollowersController extends AbstractController } catch (InvalidArgumentException $e) { $httpCode = Response::HTTP_INTERNAL_SERVER_ERROR; $httpContent['data'] = $e->getMessage(); + } finally { + $httpContent['meta']['message_id'] = $query?->messageId()?->toString(); } return $this->json( diff --git a/src/App/Controller/Chapter7/GetTimelineController.php b/src/App/Controller/Chapter7/GetTimelineController.php index fe37c5f..bc238a5 100644 --- a/src/App/Controller/Chapter7/GetTimelineController.php +++ b/src/App/Controller/Chapter7/GetTimelineController.php @@ -28,10 +28,11 @@ final class GetTimelineController extends AbstractController $httpCode = Response::HTTP_ACCEPTED; $httpContent = [ - '_meta' => [], + 'meta' => [], 'data' => [], ]; + $query = null; try { $query = TimelineQuery::fromArray([ 'author_id' => $authorId, @@ -40,7 +41,6 @@ final class GetTimelineController extends AbstractController ]); $timeline = $queryBus->query($query); - $httpContent['_meta']['message_id'] = $query->messageId()?->toString(); $httpContent['data'] = $timeline; } catch (AuthorDoesNotExist $e) { $httpCode = Response::HTTP_NOT_FOUND; @@ -48,6 +48,8 @@ final class GetTimelineController extends AbstractController } catch (\InvalidArgumentException $e) { $httpCode = Response::HTTP_INTERNAL_SERVER_ERROR; $httpContent['data'] = $e->getMessage(); + } finally { + $httpContent['meta']['message_id'] = $query?->messageId()?->toString(); } return $this->json( diff --git a/src/App/Controller/Chapter7/SignUpAuthorController.php b/src/App/Controller/Chapter7/SignUpAuthorController.php index 4f184b1..6016935 100644 --- a/src/App/Controller/Chapter7/SignUpAuthorController.php +++ b/src/App/Controller/Chapter7/SignUpAuthorController.php @@ -20,22 +20,27 @@ final class SignUpAuthorController extends AbstractController public function __invoke(Request $request, CommandBus $commandBus): Response { $httpCode = Response::HTTP_ACCEPTED; + $httpContent = [ + 'meta' => [], + 'data' => [], + ]; + + $command = null; try { $command = SignUpCommand::fromArray( $this->getRequestContentInJson($request) ); $commandBus->handle($command); - $httpContent = [ - 'message_id' => $command->messageId()?->toString(), - 'author_id' => $command->authorId(), - ]; + $httpContent['data']['author_id'] = $command->authorId(); } catch ( AuthorAlreadyExists |InvalidArgumentException $exception ) { $httpCode = Response::HTTP_INTERNAL_SERVER_ERROR; - $httpContent = ['message' => $exception->getMessage()]; + $httpContent['data']['message'] = $exception->getMessage(); + } finally { + $httpContent['meta']['message_id'] = $command?->messageId()?->toString(); } return $this->buildJsonResponse($httpContent, $httpCode);