Convert NoteTag to QBMapper

This commit is contained in:
Matias De lellis
2021-09-20 20:44:58 -03:00
parent 8bbe17fa87
commit 57e73959b8

View File

@@ -2,39 +2,37 @@
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\NoteTag; use OCA\QuickNotes\Db\NoteTag;
class NoteTagMapper extends Mapper { class NoteTagMapper extends QBMapper {
public function __construct(IDBConnection $db) { public function __construct(IDBConnection $db) {
parent::__construct($db, 'quicknotes_note_tags', '\OCA\QuickNotes\Db\NoteTag'); parent::__construct($db, 'quicknotes_note_tags', NoteTag::class);
}
public function find($id, $userId): NoteTag {
$sql = 'SELECT * FROM *PREFIX*quicknotes_note_tags WHERE id = ? AND user_id = ?';
return $this->findEntity($sql, [$id, $userId]);
}
public function findAll($userId): array {
$sql = 'SELECT * FROM *PREFIX*quicknotes_note_tags WHERE user_id = ?';
return $this->findEntities($sql, [$userId]);
} }
public function findNoteTag(string $userId, int $noteId, $tagId): NoteTag { public function findNoteTag(string $userId, int $noteId, $tagId): NoteTag {
$sql = 'SELECT * FROM *PREFIX*quicknotes_note_tags WHERE user_id = ? AND note_id = ? AND tag_id = ?'; $qb = $this->db->getQueryBuilder();
return $this->findEntity($sql, [$userId, $noteId, $tagId]); $qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)),
$qb->expr()->eq('note_id', $qb->createNamedParameter($note_id, IQueryBuilder::PARAM_INT)),
$qb->expr()->eq('tag_id', $qb->createNamedParameter($tag_id, IQueryBuilder::PARAM_INT))
);
return $this->findEntity($qb);
} }
/** /**
* @return bool * @return bool
*/ */
public function noteTagExists(string $userId, int $noteId, int $tagId): bool { public function noteTagExists(string $userId, int $noteId, int $tagId): bool {
$sql = 'SELECT * FROM *PREFIX*quicknotes_note_tags WHERE user_id = ? AND note_id = ? AND tag_id = ?';
try { try {
$this->findEntities($sql, [$userId, $noteId, $tagId]); $this->findNoteTag($userId, $noteId, $tagId);
} catch (DoesNotExistException $e) { } catch (DoesNotExistException $e) {
return false; return false;
} }