ReminderMapper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2023 Christopher Ng <chrng8@gmail.com>
  5. *
  6. * @author Christopher Ng <chrng8@gmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\FilesReminders\Db;
  25. use DateTime;
  26. use OCP\AppFramework\Db\DoesNotExistException;
  27. use OCP\AppFramework\Db\QBMapper;
  28. use OCP\DB\QueryBuilder\IQueryBuilder;
  29. use OCP\Files\Node;
  30. use OCP\Files\NotFoundException;
  31. use OCP\IDBConnection;
  32. use OCP\IUser;
  33. /**
  34. * @template-extends QBMapper<Reminder>
  35. */
  36. class ReminderMapper extends QBMapper {
  37. public const TABLE_NAME = 'files_reminders';
  38. public function __construct(IDBConnection $db) {
  39. parent::__construct(
  40. $db,
  41. static::TABLE_NAME,
  42. Reminder::class,
  43. );
  44. }
  45. public function markNotified(Reminder $reminder): Reminder {
  46. $reminderUpdate = new Reminder();
  47. $reminderUpdate->setId($reminder->getId());
  48. $reminderUpdate->setNotified(true);
  49. return $this->update($reminderUpdate);
  50. }
  51. public function find(int $id): Reminder {
  52. $qb = $this->db->getQueryBuilder();
  53. $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
  54. ->from($this->getTableName())
  55. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
  56. return $this->findEntity($qb);
  57. }
  58. /**
  59. * @throws DoesNotExistException
  60. */
  61. public function findDueForUser(IUser $user, int $fileId): Reminder {
  62. $qb = $this->db->getQueryBuilder();
  63. $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
  64. ->from($this->getTableName())
  65. ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR)))
  66. ->andWhere($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  67. ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)));
  68. return $this->findEntity($qb);
  69. }
  70. /**
  71. * @return Reminder[]
  72. */
  73. public function findAll() {
  74. $qb = $this->db->getQueryBuilder();
  75. $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
  76. ->from($this->getTableName())
  77. ->orderBy('due_date', 'ASC');
  78. return $this->findEntities($qb);
  79. }
  80. /**
  81. * @return Reminder[]
  82. */
  83. public function findAllForUser(IUser $user) {
  84. $qb = $this->db->getQueryBuilder();
  85. $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
  86. ->from($this->getTableName())
  87. ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR)))
  88. ->orderBy('due_date', 'ASC');
  89. return $this->findEntities($qb);
  90. }
  91. /**
  92. * @return Reminder[]
  93. */
  94. public function findAllForNode(Node $node) {
  95. try {
  96. $nodeId = $node->getId();
  97. } catch (NotFoundException $e) {
  98. return [];
  99. }
  100. $qb = $this->db->getQueryBuilder();
  101. $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
  102. ->from($this->getTableName())
  103. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT)))
  104. ->orderBy('due_date', 'ASC');
  105. return $this->findEntities($qb);
  106. }
  107. /**
  108. * @return Reminder[]
  109. */
  110. public function findOverdue() {
  111. $qb = $this->db->getQueryBuilder();
  112. $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
  113. ->from($this->getTableName())
  114. ->where($qb->expr()->lt('due_date', $qb->createFunction('NOW()')))
  115. ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)))
  116. ->orderBy('due_date', 'ASC');
  117. return $this->findEntities($qb);
  118. }
  119. /**
  120. * @return Reminder[]
  121. */
  122. public function findNotified(DateTime $buffer, ?int $limit = null) {
  123. $qb = $this->db->getQueryBuilder();
  124. $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
  125. ->from($this->getTableName())
  126. ->where($qb->expr()->eq('notified', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)))
  127. ->andWhere($qb->expr()->lt('due_date', $qb->createNamedParameter($buffer, IQueryBuilder::PARAM_DATE)))
  128. ->orderBy('due_date', 'ASC')
  129. ->setMaxResults($limit);
  130. return $this->findEntities($qb);
  131. }
  132. }