Skip to content

Commit

Permalink
fixes api get endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Elorfin committed Apr 11, 2023
1 parent 9b23e0f commit f5ce724
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 42 deletions.
17 changes: 13 additions & 4 deletions src/main/app/API/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,22 @@ public function __construct(
*
* @return object|null
*/
public function get(string $class, $id)
public function get(string $class, $id, string $idProp = 'id')
{
if (!is_numeric($id) && property_exists($class, 'uuid')) {
return $this->om->getRepository($class)->findOneBy(['uuid' => $id]);
if ('id' === $idProp) {
if (!is_numeric($id) && property_exists($class, 'uuid')) {
return $this->om->getRepository($class)->findOneBy(['uuid' => $id]);
}

return $this->om->getRepository($class)->findOneBy(['id' => $id]);
}

$identifiers = $this->schema->getIdentifiers($class);
if (!in_array($idProp, $identifiers)) {
throw new \LogicException(sprintf('You can only get entities with an identifier property (identifiers: %s).', implode(', ', $identifiers)));
}

return $this->om->getRepository($class)->findOneBy(['id' => $id]);
return $this->om->getRepository($class)->findOneBy([$idProp => $id]);
}

public function find(string $class, $data)
Expand Down
15 changes: 6 additions & 9 deletions src/main/app/Controller/AbstractCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,27 @@ public function findAction(Request $request, $class)
* @ApiDoc(
* description="Finds an object class $class.",
* parameters={
* {"name": "id", "type": {"string", "integer"}, "description": "The object id or uuid"}
* {"name": "field", "type": "string", "description": "The name of the identifier we want to use (eg. id, slug)"},
* {"name": "id", "type": {"string", "integer"}, "description": "The object identifier value"}
* },
* response={"$object"}
* )
*
* @param string|int $id
* @param string $class
*/
public function getAction(Request $request, $id, $class): JsonResponse
public function getAction(string $field, $id, $class): JsonResponse
{
$query = $request->query->all();
$object = $this->crud->get($class, $id);
$options = static::getOptions();

$options = $this->options['get'];
if (isset($query['options'])) {
$options = $query['options'];
}
$object = $this->crud->get($class, $id, $field);

if (!$object) {
throw new NotFoundHttpException(sprintf('No object found for id %s of class %s', $id.'', $class));
}

return new JsonResponse(
$this->serializer->serialize($object, $options ?? [])
$this->serializer->serialize($object, $options['get'] ?? [])
);
}

Expand Down
22 changes: 13 additions & 9 deletions src/main/app/Routing/ApiLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@

class ApiLoader extends Loader
{
// Route format : [path, method, defaults]
const DEFAULT_MAP = [
'create' => ['', 'POST'],
'deleteBulk' => ['', 'DELETE'],
'list' => ['', 'GET'],
'csv' => ['/csv', 'GET'],
'find' => ['/find', 'GET'],
'copyBulk' => ['/copy', 'GET'],
'update' => ['/{id}', 'PUT'],
'get' => ['/{id}', 'GET'],
'exist' => ['/exist/{field}/{value}', 'GET'],
'create' => ['', 'POST'],
'deleteBulk' => ['', 'DELETE'],
'list' => ['', 'GET'],
'csv' => ['/csv', 'GET'],
'find' => ['/find', 'GET'],
'copyBulk' => ['/copy', 'GET'],
'update' => ['/{id}', 'PUT'],
'get' => ['/{field}/{id}', 'GET', ['field' => 'id']],
'exist' => ['/exist/{field}/{value}', 'GET'],
];

/** @var bool */
Expand Down Expand Up @@ -151,6 +152,9 @@ private function loadFromPath($path, RouteCollection $routes)
$route = new ApiRoute($pattern, $routeDefaults, []);
$route->setAction($name);
$route->setMethods([$options[1]]);
if (isset($options[2])) {
$route->addDefaults($options[2]);
}
$requirements = $refClass->newInstanceWithoutConstructor()->getRequirements();

if (isset($requirements[$name])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ actions.open = (username, reload = false) => (dispatch, getState) => {

return dispatch({
[API_REQUEST]: {
url: url(['apiv2_user_find'], {filters: {username: username}}),
url: ['apiv2_user_get', {field: 'username', id: username}],
silent: true,
success: (response, dispatch) => dispatch(formActions.resetForm(selectors.FORM_NAME, response, false))
}
Expand Down
12 changes: 2 additions & 10 deletions src/main/core/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,7 @@ public function openAction(string $slug, ?User $user = null): JsonResponse
* Opens a tool.
*
* @Route("/{id}/tool/{toolName}", name="claro_workspace_open_tool")
* @EXT\ParamConverter(
* "workspace",
* class="Claroline\CoreBundle\Entity\Workspace\Workspace",
* options={"mapping": {"id": "uuid"}}
* )
* @EXT\ParamConverter("workspace", class="Claroline\CoreBundle\Entity\Workspace\Workspace", options={"mapping": {"id": "uuid"}})
*/
public function openToolAction(Workspace $workspace, string $toolName): JsonResponse
{
Expand Down Expand Up @@ -220,11 +216,7 @@ public function openToolAction(Workspace $workspace, string $toolName): JsonResp
* Submit access code.
*
* @Route("/unlock/{id}", name="claro_workspace_unlock", methods={"POST"})
* @EXT\ParamConverter(
* "workspace",
* class="Claroline\CoreBundle\Entity\Workspace\Workspace",
* options={"mapping": {"id": "uuid"}}
* )
* @EXT\ParamConverter("workspace", class="Claroline\CoreBundle\Entity\Workspace\Workspace", options={"mapping": {"id": "uuid"}})
*/
public function unlockAction(Workspace $workspace, Request $request): JsonResponse
{
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/claco-form/Controller/API/EntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public function getName(): string
return 'clacoformentry';
}

public function getAction(Request $request, $id, $class): JsonResponse
public function getAction(string $field, $id, $class): JsonResponse
{
/** @var Entry $entry */
$entry = $this->crud->get($class, $id);
$entry = $this->crud->get($class, $id, $field);
if (!$entry) {
throw new NotFoundHttpException('Entry cannot be found');
}
Expand Down
10 changes: 3 additions & 7 deletions src/plugin/message/Controller/MessageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,23 +277,19 @@ public function getRootAction($id): JsonResponse
return new JsonResponse($this->serializer->serialize($root, [Options::IS_RECURSIVE]));
}

public function getAction(Request $request, $id, $class): JsonResponse
public function getAction(string $field, $id, $class): JsonResponse
{
$currentUser = $this->tokenStorage->getToken()->getUser();

$query = $request->query->all();
$object = $this->crud->get($class, $id);
$um = $this->om->getRepository(UserMessage::class)->findOneBy(['message' => $object, 'user' => $currentUser]);
$this->crud->replace($um, 'isRead', true);
$options = $this->options['get'];

if (isset($query['options'])) {
$options = $query['options'];
}
$options = static::getOptions();

if ($object) {
return new JsonResponse(
$this->serializer->serialize($object, $options)
$this->serializer->serialize($object, $options['get'] ?? [])
);
}

Expand Down

0 comments on commit f5ce724

Please sign in to comment.