mirror of
https://github.com/JanGross/quicknotes.git
synced 2025-12-01 23:57:18 +01:00
On the other hand, it implements consistent urls, which allow you to mark a note, tag or color as a favorite in the browser to easily access the notes.
99 lines
2.8 KiB
PHP
99 lines
2.8 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace OCA\QuickNotes\Db;
|
|
|
|
use OCP\IDBConnection;
|
|
use OCP\AppFramework\Db\QBMapper;
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
class NoteMapper extends QBMapper {
|
|
|
|
public function __construct(IDBConnection $db) {
|
|
parent::__construct($db, 'quicknotes_notes');
|
|
}
|
|
|
|
/**
|
|
* @param int $id
|
|
* @param string $userId
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
|
|
* @return Note
|
|
*/
|
|
public function find($id, $userId) {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->tableName)
|
|
->where(
|
|
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)),
|
|
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
|
|
);
|
|
return $this->findEntity($qb);
|
|
}
|
|
|
|
/**
|
|
* @param string $userId
|
|
* @param string $queryStr
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
|
|
* @return Note[]
|
|
*/
|
|
public function findLike($userId, $queryStr, $offset = null, $limit = null) {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->tableName)
|
|
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
|
|
->andWhere(
|
|
$qb->expr()->orX(
|
|
$qb->expr()->like($qb->func()->lower('title'), $qb->createParameter('query')),
|
|
$qb->expr()->like($qb->func()->lower('content'), $qb->createParameter('query'))
|
|
)
|
|
);
|
|
|
|
$query = '%' . $this->db->escapeLikeParameter(strtolower($queryStr)) . '%';
|
|
$qb->setParameter('query', $query);
|
|
|
|
$qb->setFirstResult($offset);
|
|
$qb->setMaxResults($limit);
|
|
|
|
return $this->findEntities($qb);
|
|
}
|
|
|
|
/**
|
|
* @param int $id
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
|
|
* @return Note
|
|
*/
|
|
public function findShared($id) {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->tableName)
|
|
->where(
|
|
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
|
|
);
|
|
return $this->findEntity($qb);
|
|
}
|
|
|
|
public function findAll($userId) {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('*')
|
|
->from($this->tableName)
|
|
->where(
|
|
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
|
|
);
|
|
return $this->findEntities($qb);
|
|
}
|
|
|
|
public function colorIdCount($colorid) {
|
|
$qb = $this->db->getQueryBuilder();
|
|
$qb->select('id')
|
|
->from($this->tableName)
|
|
->where(
|
|
$qb->expr()->eq('color_id', $qb->createNamedParameter($colorid, IQueryBuilder::PARAM_INT))
|
|
);
|
|
return count($this->findEntities($qb));
|
|
}
|
|
|
|
}
|