Skip to content
Snippets Groups Projects
Commit cc060e61 authored by Guillaume Heurtier's avatar Guillaume Heurtier
Browse files

FEAT #12471 TIME 0:35 added resources count in tag list + get by id

parent ee4a97ab
No related branches found
No related tags found
No related merge requests found
...@@ -25,7 +25,21 @@ class TagController ...@@ -25,7 +25,21 @@ class TagController
{ {
public function get(Request $request, Response $response) public function get(Request $request, Response $response)
{ {
$tags = TagModel::get(); $tags = TagModel::get(['orderBy' => ['id']]);
$ids = array_column($tags, 'id');
$countResources = ResourceTagModel::get([
'select' => ['count(res_id)', 'tag_id'],
'where' => ['tag_id in (?)'],
'data' => [$ids],
'groupBy' => ['tag_id']
]);
$countResources = array_column($countResources, 'count', 'tag_id');
foreach ($tags as $key => $tag) {
$tags[$key]['countResources'] = $countResources[$tag['id']] ?? 0;
}
return $response->withJson(['tags' => $tags]); return $response->withJson(['tags' => $tags]);
} }
...@@ -41,6 +55,13 @@ class TagController ...@@ -41,6 +55,13 @@ class TagController
return $response->withStatus(404)->withJson(['errors' => 'id not found']); return $response->withStatus(404)->withJson(['errors' => 'id not found']);
} }
$countResources = ResourceTagModel::get([
'select' => ['count(1)'],
'where' => ['tag_id = ?'],
'data' => [$args['id']]
]);
$tag['countResources'] = $countResources[0]['count'];
return $response->withJson($tag); return $response->withJson($tag);
} }
......
...@@ -31,6 +31,7 @@ class ResourceTagModel ...@@ -31,6 +31,7 @@ class ResourceTagModel
'where' => empty($aArgs['where']) ? [] : $aArgs['where'], 'where' => empty($aArgs['where']) ? [] : $aArgs['where'],
'data' => empty($aArgs['data']) ? [] : $aArgs['data'], 'data' => empty($aArgs['data']) ? [] : $aArgs['data'],
'order_by' => empty($aArgs['orderBy']) ? [] : $aArgs['orderBy'], 'order_by' => empty($aArgs['orderBy']) ? [] : $aArgs['orderBy'],
'groupBy' => empty($aArgs['groupBy']) ? [] : $aArgs['groupBy'],
'limit' => empty($aArgs['limit']) ? 0 : $aArgs['limit'] 'limit' => empty($aArgs['limit']) ? 0 : $aArgs['limit']
]); ]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment