From bcda9ab5054b1c12a5f8fe7b251ba05e57d6f7cb Mon Sep 17 00:00:00 2001 From: Giuliano Mele Date: Thu, 22 Aug 2024 11:22:37 +0200 Subject: [PATCH] Add command to get folders by tag Fixes #3 --- appinfo/info.xml | 1 + lib/Command/GetFolder.php | 45 ++++++++++++++++++++++++++++++++++++++ lib/Db/TagMapper.php | 16 ++++++++++++++ lib/Service/TagService.php | 7 ++++++ 4 files changed, 69 insertions(+) create mode 100644 lib/Command/GetFolder.php diff --git a/appinfo/info.xml b/appinfo/info.xml index 8beb18f..afec412 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -20,5 +20,6 @@ OCA\GroupfolderTags\Command\Tag OCA\GroupfolderTags\Command\GetTag + OCA\GroupfolderTags\Command\GetFolder diff --git a/lib/Command/GetFolder.php b/lib/Command/GetFolder.php new file mode 100644 index 0000000..bcdf747 --- /dev/null +++ b/lib/Command/GetFolder.php @@ -0,0 +1,45 @@ +setName('groupfolder-tags:get-folder') + ->setDescription('Get Groupfolders with tag key optionally filtered by tag value.') + ->addArgument('key', InputArgument::REQUIRED, 'Key of the tag') + ->addArgument('value', InputArgument::OPTIONAL, 'Value of the tag'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $tagKey = $input->getArgument('key'); + $tagValue = $input->getArgument('value'); + + try { + $results = $this->service->findFolder($tagKey, $tagValue); + + if (empty($results)) { + $output->writeln("Could not find Groupfolders for tag"); + return 1; + } + + foreach ($results as $result) { + $folder = $this->folderManager->getFolder($result['group_folder_id'], $this->rootFolder->getMountPoint()->getNumericStorageId()); + if ($folder === false) { + $output->writeln("Folder not found: {$result['group_folder_id']}"); + continue; + } + $output->writeln(json_encode($folder)); + } + return 0; + } catch (Exception $e) { + $output->writeln("Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}"); + return 1; + } + } +} diff --git a/lib/Db/TagMapper.php b/lib/Db/TagMapper.php index ae8fcb4..93d4a0f 100644 --- a/lib/Db/TagMapper.php +++ b/lib/Db/TagMapper.php @@ -91,4 +91,20 @@ class TagMapper extends QBMapper { return $this->findEntities($qb); } + + /** + * @throws Exception + */ + public function findFolder(string $tagKey, ?string $tagValue): array { + $qb = $this->db->getQueryBuilder(); + $qb->selectDistinct('group_folder_id') + ->from(self::TABLENAME) + ->where($qb->expr()->eq('tag_key', $qb->createNamedParameter($tagKey))); + + if(isset($tagValue)) { + $qb->andWhere($qb->expr()->eq('tag_value', $qb->createNamedParameter($tagValue))); + } + + return $qb->executeQuery()->fetchAll(); + } } diff --git a/lib/Service/TagService.php b/lib/Service/TagService.php index 4f36f7f..8be97d7 100644 --- a/lib/Service/TagService.php +++ b/lib/Service/TagService.php @@ -78,4 +78,11 @@ class TagService { public function findByGroupFolderAndKey(int $groupFolderId, ?string $key): array { return $this->mapper->findByGroupFolderAndKey($groupFolderId, $key); } + + /** + * @throws \OCP\DB\Exception + */ + public function findFolder(string $key, ?string $value): array { + return $this->mapper->findFolder($key, $value); + } }