diff options
Diffstat (limited to 'lib/Db/ItemMapperV2.php')
-rw-r--r-- | lib/Db/ItemMapperV2.php | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/lib/Db/ItemMapperV2.php b/lib/Db/ItemMapperV2.php new file mode 100644 index 000000000..ba840fda7 --- /dev/null +++ b/lib/Db/ItemMapperV2.php @@ -0,0 +1,124 @@ +<?php +/** + * Nextcloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Alessandro Cosentino <cosenal@gmail.com> + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @copyright 2020 Sean Molenaar + */ + +namespace OCA\News\Db; + +use Doctrine\DBAL\FetchMode; +use OCA\News\Utility\Time; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\Entity; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; + +/** + * Class ItemMapper + * + * @package OCA\News\Db + */ +class ItemMapperV2 extends NewsMapperV2 +{ + const TABLE_NAME = 'news_items'; + + /** + * ItemMapper constructor. + * + * @param IDBConnection $db + * @param Time $time + */ + public function __construct(IDBConnection $db, Time $time) + { + parent::__construct($db, $time, Item::class); + } + + /** + * Find all feeds for a user. + * + * @param string $userId The user identifier + * + * @return Entity[] + */ + public function findAllFromUser($userId): array + { + $builder = $this->db->getQueryBuilder(); + $builder->select('items.*') + ->from($this->tableName) + ->innerJoin('items', FeedMapperV2::TABLE_NAME, 'feeds', 'items.feed_id = feeds.id') + ->where('feeds.user_id = :user_id') + ->andWhere('deleted_at = 0') + ->setParameter(':user_id', $userId, IQueryBuilder::PARAM_STR); + + return $this->findEntities($builder); + } + + /** + * Find all items + * + * @return Entity[] + */ + public function findAll(): array + { + $builder = $this->db->getQueryBuilder(); + $builder->addSelect('*') + ->from($this->tableName) + ->andWhere('deleted_at = 0'); + + return $this->findEntities($builder); + } + + public function findFromUser(string $userId, int $id): Entity + { + $builder = $this->db->getQueryBuilder(); + $builder->select('items.*') + ->from($this->tableName) + ->innerJoin('items', FeedMapperV2::TABLE_NAME, 'feeds', 'items.feed_id = feeds.id') + ->where('feeds.user_id = :user_id') + ->andWhere('items.id = :item_id') + ->andWhere('deleted_at = 0') + ->setParameter(':user_id', $userId, IQueryBuilder::PARAM_STR) + ->setParameter(':item_id', $id, IQueryBuilder::PARAM_STR); + + return $this->findEntity($builder); + } + + public function findByGuidHash(string $getGuidHash): Item + { + throw new DoesNotExistException('fasi'); + } + + public function findAllForFeed(int $feedId) + { + $builder = $this->db->getQueryBuilder(); + $builder->addSelect('*') + ->from($this->tableName) + ->andWhere('feed_id = :feed_id') + ->setParameter(':feed_id', $feedId, IQueryBuilder::PARAM_INT); + + return $this->findEntities($builder); + } + + /** + * Delete items from feed that are over the max item threshold + * + * @param int $threshold Deletion threshold + */ + public function deleteOverThreshold($threshold) + { + $builder = $this->db->getQueryBuilder(); + + $query = $builder->addSelect('COUNT(*)') + ->from($this->tableName) + ->groupBy('feed_id') + ->where(''); + + return $this->db->executeQuery($query)->fetch(FetchMode::ASSOCIATIVE); + } +} |