Files
cospend-nc/lib/Db/ProjectMapper.php
Julien Veyssier 5644fe025a add command to delete bills with filters
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
2023-05-15 02:03:17 +02:00

93 lines
2.2 KiB
PHP

<?php
/**
* Nextcloud - cospend
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Julien Veyssier <julien-nc@posteo.net>
* @copyright Julien Veyssier 2019
*/
namespace OCA\Cospend\Db;
use Exception;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
class ProjectMapper extends QBMapper {
const TABLENAME = 'cospend_projects';
public function __construct(IDBConnection $db) {
parent::__construct($db, self::TABLENAME, Project::class);
}
public function find(string $id): Project {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from(self::TABLENAME)
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_STR)));
$result = $qb->executeQuery();
$row = $result->fetch();
$result->closeCursor();
if ($row === false) {
throw new Exception('Project ' . $id . ' not found');
}
return $this->mapRowToEntity($row);
}
/**
* @param string $userId
* @return array
* @throws \OCP\DB\Exception
*/
public function getProjects(string $userId): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('userid', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
);
return $this->findEntities($qb);
}
/**
* @param string $projectId
* @return void
* @throws \OCP\DB\Exception
*/
public function deleteBillOwersOfProject(string $projectId): void {
// old style
/*
$query = 'DELETE FROM `*PREFIX*cospend_bill_owers`
WHERE `billid` IN (
SELECT `id` FROM `*PREFIX*cospend_bills` WHERE `projectid` = ?
)';
$this->db->executeQuery($query, [$projectId]);
*/
// inspired from the tables app
$qb = $this->db->getQueryBuilder();
$qb2 = $this->db->getQueryBuilder();
$qb2->select('id')
->from('cospend_bills')
->where(
$qb2->expr()->eq('projectid', $qb->createNamedParameter($projectId, IQueryBuilder::PARAM_STR))
);
$qb->delete('cospend_bill_owers')
->where(
$qb2->expr()->in('billid', $qb->createFunction($qb2->getSQL()), IQueryBuilder::PARAM_STR_ARRAY)
);
$qb->executeStatement();
$qb->resetQueryParts();
}
}