Skip to content

Commit

Permalink
Add users_count to API
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Oct 12, 2024
1 parent bedc7d4 commit 0eb8510
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
17 changes: 17 additions & 0 deletions app/src/Controller/Group/GroupApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
3 changes: 2 additions & 1 deletion app/tests/Controller/Group/GroupApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public function testApi(): void
'description',
'icon',
'created_at',
'updated_at'
'updated_at',
'users_count',
], $response);
}
}

0 comments on commit 0eb8510

Please sign in to comment.