Convert NoteShare to QBMapper

This commit is contained in:
Matias De lellis
2021-09-20 20:37:11 -03:00
parent 3365a914c7
commit 8bbe17fa87

View File

@@ -2,59 +2,83 @@
namespace OCA\QuickNotes\Db;
use OCP\IDBConnection;
use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Db\QBMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCA\QuickNotes\Db\NoteShare;
class NoteShareMapper extends Mapper {
class NoteShareMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'quicknotes_shares', '\OCA\QuickNotes\Db\NoteShare');
parent::__construct($db, 'quicknotes_shares', NoteShare::class);
}
/*public function find($id, $userId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_shares WHERE id = ? AND user_id = ?';
return $this->findEntity($sql, [$id, $userId]);
}*/
public function findForUser(string $userId): array {
$sql = 'SELECT * FROM *PREFIX*quicknotes_shares WHERE shared_user = ?';
return $this->findEntities($sql, [$userId]);
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('shared_user', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
);
return $this->findEntities($qb);
}
public function findForGroup($groupId): array {
$sql = 'SELECT * FROM *PREFIX*quicknotes_shares WHERE shared_group = ?';
return $this->findEntities($sql, [$groupId]);
public function findForGroup(string $groupId): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('shared_group', $qb->createNamedParameter($groupId, IQueryBuilder::PARAM_STR))
);
return $this->findEntities($qb);
}
public function findByNoteAndUser($noteId, $userId): NoteShare {
$sql = 'SELECT * FROM *PREFIX*quicknotes_shares WHERE shared_user = ? AND note_id = ?';
return $this->findEntity($sql, [$userId, $noteId]);
public function findByNoteAndUser(int $noteId, string $userId): NoteShare {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('note_id', $qb->createNamedParameter($noteId, IQueryBuilder::PARAM_INT)),
$qb->expr()->eq('shared_user', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)),
);
return $this->findEntity($qb);
}
public function findByNoteAndGroup($noteId, $groupId): NoteShare {
$sql = 'SELECT * FROM *PREFIX*quicknotes_shares WHERE shared_group = ? AND note_id = ?';
return $this->findEntity($sql, [$groupId, $noteId]);
public function findByNoteAndGroup(int $noteId, string $groupId): NoteShare {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('note_id', $qb->createNamedParameter($noteId, IQueryBuilder::PARAM_INT)),
$qb->expr()->eq('shared_group', $qb->createNamedParameter($groupId, IQueryBuilder::PARAM_STR)),
);
return $this->findEntity($qb);
}
public function getSharesForNote(int $noteId): array {
$sql = 'SELECT * FROM *PREFIX*quicknotes_shares WHERE note_id = ?';
return $this->findEntities($sql, [$noteId]);
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('note_id', $qb->createNamedParameter($noteId, IQueryBuilder::PARAM_INT))
);
return $this->findEntities($qb);
}
public function deleteByNoteId(int $noteId): void {
$sql = 'DELETE FROM *PREFIX*quicknotes_shares WHERE note_id = ?';
$this->execute($sql, [$noteId]);
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where($qb->expr()->eq('note_id', $qb->createNamedParameter($noteId)))
->execute();
}
/**
* @return bool
*/
public function existsByNoteAndUser(int $noteId, $userId) {
$sql = 'SELECT * FROM *PREFIX*quicknotes_shares WHERE shared_user = ? AND note_id = ?';
public function existsByNoteAndUser(int $noteId, string $userId) {
try {
$this->findEntities($sql, [$userId, $noteId]);
$this->findByNoteAndUser($noteId, $userId);
} catch (DoesNotExistException $e) {
return false;
}