diff --git a/src/app/tag/controllers/TagController.php b/src/app/tag/controllers/TagController.php index dc13fcf61b62741efdb7c5a3b6b1b616b4b4ee8a..b9c2735b7a961ca18977ce43a58ed6af46a5d838 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 f99f8a154f327022443529b3643201c86bd20acd..b004fb2be2c2df5bb359ac8fa35cd9b6901b999c 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'] ]);