0
0
Fork 0
mirror of https://github.com/verdigado/groupfolder_tags.git synced 2024-10-22 15:39:48 +02:00

Add command to get folders by tag

Fixes #3
This commit is contained in:
Giuliano Mele 2024-08-22 11:22:37 +02:00
parent 8440b823cc
commit bcda9ab505
Signed by: MelGi
GPG key ID: E790C1211F6DEE5E
4 changed files with 69 additions and 0 deletions

View file

@ -20,5 +20,6 @@
<commands>
<command>OCA\GroupfolderTags\Command\Tag</command>
<command>OCA\GroupfolderTags\Command\GetTag</command>
<command>OCA\GroupfolderTags\Command\GetFolder</command>
</commands>
</info>

45
lib/Command/GetFolder.php Normal file
View file

@ -0,0 +1,45 @@
<?php
namespace OCA\GroupfolderTags\Command;
use OCP\DB\Exception;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class GetFolder extends TagCommand {
protected function configure(): void {
$this
->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("<error>Could not find Groupfolders for tag</error>");
return 1;
}
foreach ($results as $result) {
$folder = $this->folderManager->getFolder($result['group_folder_id'], $this->rootFolder->getMountPoint()->getNumericStorageId());
if ($folder === false) {
$output->writeln("<error>Folder not found: {$result['group_folder_id']}</error>");
continue;
}
$output->writeln(json_encode($folder));
}
return 0;
} catch (Exception $e) {
$output->writeln("<error>Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}</error>");
return 1;
}
}
}

View file

@ -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();
}
}

View file

@ -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);
}
}