0
0
Fork 0
mirror of https://github.com/verdigado/groupfolder_tags.git synced 2024-11-23 21:40:27 +01:00

Merge pull request #4 from verdigado/feature/occ-commands

Add occ command
This commit is contained in:
Jonathan Treffler 2024-09-04 22:47:36 +02:00 committed by GitHub
commit 1f66db9c2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 184 additions and 7 deletions

View file

@ -19,5 +19,7 @@
</dependencies>
<commands>
<command>OCA\GroupfolderTags\Command\Tag</command>
<command>OCA\GroupfolderTags\Command\GetTag</command>
<command>OCA\GroupfolderTags\Command\GetFolder</command>
</commands>
</info>
</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;
}
}
}

52
lib/Command/GetTag.php Normal file
View file

@ -0,0 +1,52 @@
<?php
namespace OCA\GroupfolderTags\Command;
use OCA\GroupfolderTags\Db\Tag;
use OCP\DB\Exception;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class GetTag extends TagCommand {
protected function configure(): void {
$this
->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("<error>Could not find tag for Groupfolder</error>");
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("<error>Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}</error>");
return 1;
}
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace OCA\GroupfolderTags\Command;
use OCA\GroupFolders\Command\FolderCommand;
use OCA\GroupFolders\Folder\FolderManager;
use OCA\GroupFolders\Mount\MountProvider;
use OCA\GroupfolderTags\Service\TagService;
use OCP\Files\IRootFolder;
abstract class TagCommand extends FolderCommand {
public function __construct(
protected readonly TagService $service,
FolderManager $folderManager,
IRootFolder $rootFolder,
MountProvider $mountProvider
) {
parent::__construct($folderManager, $rootFolder, $mountProvider);
}
}

View file

@ -17,6 +17,15 @@ class Tag extends Entity implements JsonSerializable {
$this->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,
];
}
}
}

View file

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

View file

@ -70,4 +70,19 @@ class TagService {
$this->handleException($e);
}
}
}
/**
* @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);
}
}