0
0
Fork 0
mirror of https://github.com/verdigado/groupfolder_tags.git synced 2024-12-06 11:42:42 +01:00

added Tag entity and mapper

This commit is contained in:
Jonathan Treffler 2024-07-31 15:15:27 +00:00
parent acf1298b47
commit bffbb74da4
2 changed files with 82 additions and 0 deletions

28
lib/Db/Tag.php Normal file
View file

@ -0,0 +1,28 @@
<?php
namespace OCA\GroupfolderTags\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class Tag extends Entity implements JsonSerializable {
protected $groupFolderId;
protected $tagKey;
protected $tagValue;
protected $lastUpdatedTimestamp;
public function __construct() {
$this->addType('groupFolderId','integer');
$this->addType('lastUpdatedTimestamp','integer');
}
public function jsonSerialize(): array {
return [
'groupFolderId' => $this->groupFolderId,
'tagKey' => $this->tagKey,
'tagValue' => $this->tagValue,
'lastUpdatedTimestamp' => $this->lastUpdatedTimestamp,
];
}
}

54
lib/Db/TagMapper.php Normal file
View file

@ -0,0 +1,54 @@
<?php
namespace OCA\GroupfolderTags\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
class TagMapper extends QBMapper {
public const TABLENAME = 'groupfolder_tags';
public function __construct(IDBConnection $db) {
parent::__construct($db, self::TABLENAME, Tag::class);
}
/**
* @param int $groupFolderId
* @param string $tagKey
* @return Entity|Tag
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws DoesNotExistException
*/
public function find(int $groupFolderId, string $tagKey): Tag {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->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);
}
/**
* @param string $tagKey
* @param string|null $tagValue
* @return array
*/
public function findAll(string $tagKey, ?string $tagValue): array {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->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 $this->findEntities($qb);
}
}