diff --git a/app/src/Controller/Group/GroupApi.php b/app/src/Controller/Group/GroupApi.php index 61341b6..d4655dc 100644 --- a/app/src/Controller/Group/GroupApi.php +++ b/app/src/Controller/Group/GroupApi.php @@ -46,12 +46,29 @@ public function __construct( public function __invoke(GroupInterface $group, Response $response): Response { $this->validateAccess($group); + $group = $this->mutateGroup($group); $payload = json_encode($group, JSON_THROW_ON_ERROR); $response->getBody()->write($payload); return $response->withHeader('Content-Type', 'application/json'); } + /** + * Add or remove fields from the group object before returning it. + * TIP : When extending this class, you can use this method to add your own fields. + * + * @param GroupInterface $group + * + * @return GroupInterface + */ + protected function mutateGroup(GroupInterface $group): GroupInterface + { + // Add the user count to the group object + $group->loadCount('users'); + + return $group; + } + /** * Validate access to the page. * diff --git a/app/tests/Controller/Group/GroupApiTest.php b/app/tests/Controller/Group/GroupApiTest.php index 20778aa..88f5baa 100644 --- a/app/tests/Controller/Group/GroupApiTest.php +++ b/app/tests/Controller/Group/GroupApiTest.php @@ -99,7 +99,8 @@ public function testApi(): void 'description', 'icon', 'created_at', - 'updated_at' + 'updated_at', + 'users_count', ], $response); } }