Convert colorMapper to QBMapper

This commit is contained in:
Matias De lellis
2021-09-20 20:14:17 -03:00
parent 443f6bf144
commit 3365a914c7
3 changed files with 33 additions and 16 deletions

View File

@@ -5,6 +5,10 @@ use JsonSerializable;
use OCP\AppFramework\Db\Entity; use OCP\AppFramework\Db\Entity;
/**
* @method void setColor(string $color)
* @method strinf getColor()
*/
class Color extends Entity implements JsonSerializable { class Color extends Entity implements JsonSerializable {
protected $color; protected $color;

View File

@@ -2,36 +2,42 @@
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\Color; use OCA\QuickNotes\Db\Color;
class ColorMapper extends Mapper { class ColorMapper extends QBMapper {
public function __construct(IDBConnection $db) { public function __construct(IDBConnection $db) {
parent::__construct($db, 'quicknotes_colors', '\OCA\QuickNotes\Db\Color'); parent::__construct($db, 'quicknotes_colors', Color::class);
} }
public function find($id): Color { public function find(int $id): Color {
$sql = 'SELECT * FROM *PREFIX*quicknotes_colors WHERE id = ?'; $qb = $this->db->getQueryBuilder();
return $this->findEntity($sql, [$id]); $qb->select('*')
} ->from($this->tableName)
->where(
public function findAll(): array { $qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
$sql = 'SELECT * FROM *PREFIX*quicknotes_colors'; );
return $this->findEntities($sql, []); return $this->findEntity($qb);
} }
public function findByColor(string $color): Color { public function findByColor(string $color): Color {
$sql = 'SELECT * FROM *PREFIX*quicknotes_colors WHERE color = ?'; $qb = $this->db->getQueryBuilder();
return $this->findEntity($sql, [$color]); $qb->select('*')
->from($this->tableName)
->where(
$qb->expr()->eq('color', $qb->createNamedParameter($color, IQueryBuilder::PARAM_STRING))
);
return $this->findEntity($qb);
} }
public function colorExists(string $color): bool { public function colorExists(string $color): bool {
$sql = 'SELECT * FROM *PREFIX*quicknotes_colors WHERE color = ?';
try { try {
$this->findEntity($sql, [$color]); $this->findByColor($color);
} catch (DoesNotExistException $e) { } catch (DoesNotExistException $e) {
return false; return false;
} }

View File

@@ -19,12 +19,19 @@ class NoteShare extends Entity implements JsonSerializable {
protected $sharedUser; protected $sharedUser;
protected $sharedGroup; protected $sharedGroup;
protected $userId;
public function setUserId (string $userId): void {
$this->userId = $userId;
}
public function jsonSerialize() { public function jsonSerialize() {
return [ return [
'id' => $this->id, 'id' => $this->id,
'note_id' => $this->noteId, 'note_id' => $this->noteId,
'shared_user' => $this->sharedUser, 'shared_user' => $this->sharedUser,
'shared_group' => $this->sharedGroup 'shared_group' => $this->sharedGroup,
'user_id' => $this->userId
]; ];
} }