0
0
Fork 0
mirror of https://github.com/verdigado/groupfolder_tags.git synced 2024-11-24 05:50:26 +01:00

Add command to get one or multiple tags by folder and key

Fixes #1, Fixes #2
This commit is contained in:
Giuliano Mele 2024-08-22 11:21:25 +02:00
parent ee525d8b7c
commit 8440b823cc
Signed by: MelGi
GPG key ID: E790C1211F6DEE5E
5 changed files with 95 additions and 7 deletions

View file

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

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

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

View file

@ -70,4 +70,12 @@ 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);
}
}