diff --git a/appinfo/info.xml b/appinfo/info.xml index 25e00d2..afec412 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -19,5 +19,7 @@ OCA\GroupfolderTags\Command\Tag + OCA\GroupfolderTags\Command\GetTag + OCA\GroupfolderTags\Command\GetFolder - \ No newline at end of file + 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/Command/GetTag.php b/lib/Command/GetTag.php new file mode 100644 index 0000000..1817457 --- /dev/null +++ b/lib/Command/GetTag.php @@ -0,0 +1,52 @@ +setName('groupfolder-tags:get') + ->setDescription('Get single tag value by Groupfolder ID and key. Omit key to get all tags.') + ->addArgument('folder_id', InputArgument::REQUIRED, 'Groupfolder ID of the tag') + ->addArgument('key', InputArgument::OPTIONAL, 'Key of the tag'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $groupFolderId = $input->getArgument('folder_id'); + $tagKey = $input->getArgument('key'); + + if (!$this->getFolder($input, $output)) { + return 1; + } + + try { + $tags = $this->service->findByGroupFolderAndKey($groupFolderId, $tagKey); + + if (empty($tags)) { + $output->writeln("Could not find tag for Groupfolder"); + return 1; + } + + if (isset($tagKey)) { + /** @var Tag $tag */ + $tag = current($tags); + $output->writeln($tag->getTagValue()); + return 0; + } + + foreach ($tags as $tag) { + $output->writeln(json_encode($tag)); + } + return 0; + } catch (Exception $e) { + $output->writeln("Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}"); + return 1; + } + } +} diff --git a/lib/Command/TagCommand.php b/lib/Command/TagCommand.php new file mode 100644 index 0000000..27aa9d2 --- /dev/null +++ b/lib/Command/TagCommand.php @@ -0,0 +1,20 @@ +addType('lastUpdatedTimestamp','integer'); } + /** + * @return mixed + */ + public function getTagValue() { + return $this->tagValue; + } + + + public function jsonSerialize(): array { return [ 'groupFolderId' => $this->groupFolderId, @@ -25,4 +34,4 @@ class Tag extends Entity implements JsonSerializable { 'lastUpdatedTimestamp' => $this->lastUpdatedTimestamp, ]; } -} \ No newline at end of file +} diff --git a/lib/Db/TagMapper.php b/lib/Db/TagMapper.php index 7b0604e..93d4a0f 100644 --- a/lib/Db/TagMapper.php +++ b/lib/Db/TagMapper.php @@ -5,6 +5,7 @@ namespace OCA\GroupfolderTags\Db; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\Entity; use OCP\AppFramework\Db\QBMapper; +use OCP\DB\Exception; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; @@ -30,7 +31,7 @@ class TagMapper extends QBMapper { ->from(self::TABLENAME) ->where($qb->expr()->eq('group_folder_id', $qb->createNamedParameter($groupFolderId, IQueryBuilder::PARAM_INT))) ->andWhere($qb->expr()->eq('tag_key', $qb->createNamedParameter($tagKey))); - + return $this->findEntity($qb); } @@ -51,7 +52,7 @@ class TagMapper extends QBMapper { ->leftJoin('t', self::GROUP_FOLDERS_TABLENAME, 'g', $qb->expr()->andX( $qb->expr()->eq('t.group_folder_id', 'g.id'), )); - + return $this->findOneQuery($qb); } @@ -70,7 +71,40 @@ class TagMapper extends QBMapper { if(isset($tagValue)) { $qb->andWhere($qb->expr()->eq('tag_value', $qb->createNamedParameter($tagValue))); } - + return $this->findEntities($qb); } -} \ No newline at end of file + + /** + * @return Tag[] + * @throws Exception + */ + public function findByGroupFolderAndKey(string $groupFolderId, ?string $tagKey): array { + $qb = $this->db->getQueryBuilder(); + $qb->select('*') + ->from(self::TABLENAME) + ->where($qb->expr()->eq('group_folder_id', $qb->createNamedParameter($groupFolderId, IQueryBuilder::PARAM_INT))); + + if(isset($tagKey)) { + $qb->andWhere($qb->expr()->eq('tag_key', $qb->createNamedParameter($tagKey))); + } + + 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 1127b90..8be97d7 100644 --- a/lib/Service/TagService.php +++ b/lib/Service/TagService.php @@ -70,4 +70,19 @@ class TagService { $this->handleException($e); } } -} \ No newline at end of file + + /** + * @return Tag[] + * @throws \OCP\DB\Exception + */ + 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); + } +}