From cc060e618ed51580b1c0e8a3e6bf425ebb8ef848 Mon Sep 17 00:00:00 2001 From: Guillaume Heurtier <guillaume.heurtier@maarch.org> Date: Wed, 12 Feb 2020 14:32:25 +0100 Subject: [PATCH] FEAT #12471 TIME 0:35 added resources count in tag list + get by id --- src/app/tag/controllers/TagController.php | 23 ++++++++++++++++++++++- src/app/tag/models/ResourceTagModel.php | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/app/tag/controllers/TagController.php b/src/app/tag/controllers/TagController.php index dc13fcf61b6..b9c2735b7a9 100644 --- a/src/app/tag/controllers/TagController.php +++ b/src/app/tag/controllers/TagController.php @@ -25,7 +25,21 @@ class TagController { 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]); } @@ -41,6 +55,13 @@ class TagController 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); } diff --git a/src/app/tag/models/ResourceTagModel.php b/src/app/tag/models/ResourceTagModel.php index f99f8a154f3..b004fb2be2c 100644 --- a/src/app/tag/models/ResourceTagModel.php +++ b/src/app/tag/models/ResourceTagModel.php @@ -31,6 +31,7 @@ class ResourceTagModel 'where' => empty($aArgs['where']) ? [] : $aArgs['where'], 'data' => empty($aArgs['data']) ? [] : $aArgs['data'], 'order_by' => empty($aArgs['orderBy']) ? [] : $aArgs['orderBy'], + 'groupBy' => empty($aArgs['groupBy']) ? [] : $aArgs['groupBy'], 'limit' => empty($aArgs['limit']) ? 0 : $aArgs['limit'] ]); -- GitLab