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