ReminderMapper.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\FilesReminders\Db;
  8. use DateTime;
  9. use OCP\AppFramework\Db\DoesNotExistException;
  10. use OCP\AppFramework\Db\QBMapper;
  11. use OCP\DB\QueryBuilder\IQueryBuilder;
  12. use OCP\Files\Node;
  13. use OCP\Files\NotFoundException;
  14. use OCP\IDBConnection;
  15. use OCP\IUser;
  16. /**
  17. * @template-extends QBMapper<Reminder>
  18. */
  19. class ReminderMapper extends QBMapper {
  20. public const TABLE_NAME = 'files_reminders';
  21. public function __construct(IDBConnection $db) {
  22. parent::__construct(
  23. $db,
  24. static::TABLE_NAME,
  25. Reminder::class,
  26. );
  27. }
  28. public function markNotified(Reminder $reminder): Reminder {
  29. $reminderUpdate = new Reminder();
  30. $reminderUpdate->setId($reminder->getId());
  31. $reminderUpdate->setNotified(true);
  32. return $this->update($reminderUpdate);
  33. }
  34. public function find(int $id): Reminder {
  35. $qb = $this->db->getQueryBuilder();
  36. $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
  37. ->from($this->getTableName())
  38. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
  39. return $this->findEntity($qb);
  40. }
  41. /**
  42. * @throws DoesNotExistException
  43. */
  44. public function findDueForUser(IUser $user, int $fileId): Reminder {
  45. $qb = $this->db->getQueryBuilder();
  46. $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
  47. ->from($this->getTableName())
  48. ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR)))
  49. ->andWhere($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  50. ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)));
  51. return $this->findEntity($qb);
  52. }
  53. /**
  54. * @return Reminder[]
  55. */
  56. public function findAll() {
  57. $qb = $this->db->getQueryBuilder();
  58. $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
  59. ->from($this->getTableName())
  60. ->orderBy('due_date', 'ASC');
  61. return $this->findEntities($qb);
  62. }
  63. /**
  64. * @return Reminder[]
  65. */
  66. public function findAllForUser(IUser $user) {
  67. $qb = $this->db->getQueryBuilder();
  68. $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
  69. ->from($this->getTableName())
  70. ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR)))
  71. ->orderBy('due_date', 'ASC');
  72. return $this->findEntities($qb);
  73. }
  74. /**
  75. * @return Reminder[]
  76. */
  77. public function findAllForNode(Node $node) {
  78. try {
  79. $nodeId = $node->getId();
  80. } catch (NotFoundException $e) {
  81. return [];
  82. }
  83. $qb = $this->db->getQueryBuilder();
  84. $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
  85. ->from($this->getTableName())
  86. ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT)))
  87. ->orderBy('due_date', 'ASC');
  88. return $this->findEntities($qb);
  89. }
  90. /**
  91. * @return Reminder[]
  92. */
  93. public function findOverdue() {
  94. $qb = $this->db->getQueryBuilder();
  95. $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
  96. ->from($this->getTableName())
  97. ->where($qb->expr()->lt('due_date', $qb->createFunction('NOW()')))
  98. ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)))
  99. ->orderBy('due_date', 'ASC');
  100. return $this->findEntities($qb);
  101. }
  102. /**
  103. * @return Reminder[]
  104. */
  105. public function findNotified(DateTime $buffer, ?int $limit = null) {
  106. $qb = $this->db->getQueryBuilder();
  107. $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
  108. ->from($this->getTableName())
  109. ->where($qb->expr()->eq('notified', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)))
  110. ->andWhere($qb->expr()->lt('due_date', $qb->createNamedParameter($buffer, IQueryBuilder::PARAM_DATETIME_MUTABLE)))
  111. ->orderBy('due_date', 'ASC')
  112. ->setMaxResults($limit);
  113. return $this->findEntities($qb);
  114. }
  115. }