Files
nextcloud-jukebox/lib/Db/RadioStationMapper.php
2025-06-15 02:15:38 +03:00

126 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
// SPDX-FileCopyrightText: Chen Asraf <contact@casraf.dev>
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OCA\Jukebox\Db;
use OCA\Jukebox\AppInfo\Application;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/**
* @template-extends QBMapper<RadioStation>
*/
class RadioStationMapper extends QBMapper {
public function __construct(
IDBConnection $db,
) {
parent::__construct($db, Application::tableName('radio_stations'), RadioStation::class);
}
/**
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws DoesNotExistException
*/
public function find(int $id): RadioStation {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
return $this->findEntity($qb);
}
/**
* @return array<RadioStation>
*/
public function findAll(): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')->from($this->getTableName());
return $this->findEntities($qb);
}
/**
* @param string $remoteUuid
* @return RadioStation|null
*/
public function findByRemoteUuid(string $userId, string $remoteUuid): ?RadioStation {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->eq('remote_uuid', $qb->createNamedParameter($remoteUuid)))
->setMaxResults(1);
try {
return $this->findEntity($qb);
} catch (DoesNotExistException) {
return null;
}
}
/**
* @param string $userId
* @return array<RadioStation>
*/
public function findByUserId(string $userId): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)));
return $this->findEntities($qb);
}
/**
* @param string $userId
* @return array<RadioStation>
*/
public function findFavoritesByUserId(string $userId): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->eq('favorited', $qb->createNamedParameter(true)));
return $this->findEntities($qb);
}
/**
* Count how many radio stations exist for a user
*
* @param string $userId
* @return int
*/
public function countForUser(string $userId): int {
$qb = $this->db->getQueryBuilder();
$qb->select($qb->createFunction('COUNT(*)'))
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)));
return (int)$qb->executeQuery()->fetchOne();
}
/**
* Find radio stations for a user with pagination support
*
* @param string $userId
* @param int $offset
* @param int $limit
* @return array<RadioStation>
*/
public function findPaginatedByUserId(string $userId, int $offset, int $limit): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
->setFirstResult($offset)
->setMaxResults($limit)
->orderBy('name', 'ASC');
return $this->findEntities($qb);
}
}