TaskMapper.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 OC\TextProcessing\Db;
  8. use OCP\AppFramework\Db\DoesNotExistException;
  9. use OCP\AppFramework\Db\Entity;
  10. use OCP\AppFramework\Db\MultipleObjectsReturnedException;
  11. use OCP\AppFramework\Db\QBMapper;
  12. use OCP\AppFramework\Utility\ITimeFactory;
  13. use OCP\DB\Exception;
  14. use OCP\IDBConnection;
  15. /**
  16. * @extends QBMapper<Task>
  17. */
  18. class TaskMapper extends QBMapper {
  19. public function __construct(
  20. IDBConnection $db,
  21. private ITimeFactory $timeFactory,
  22. ) {
  23. parent::__construct($db, 'textprocessing_tasks', Task::class);
  24. }
  25. /**
  26. * @param int $id
  27. * @return Task
  28. * @throws Exception
  29. * @throws DoesNotExistException
  30. * @throws MultipleObjectsReturnedException
  31. */
  32. public function find(int $id): Task {
  33. $qb = $this->db->getQueryBuilder();
  34. $qb->select(Task::$columns)
  35. ->from($this->tableName)
  36. ->where($qb->expr()->eq('id', $qb->createPositionalParameter($id)));
  37. return $this->findEntity($qb);
  38. }
  39. /**
  40. * @param int $id
  41. * @param string|null $userId
  42. * @return Task
  43. * @throws DoesNotExistException
  44. * @throws Exception
  45. * @throws MultipleObjectsReturnedException
  46. */
  47. public function findByIdAndUser(int $id, ?string $userId): Task {
  48. $qb = $this->db->getQueryBuilder();
  49. $qb->select(Task::$columns)
  50. ->from($this->tableName)
  51. ->where($qb->expr()->eq('id', $qb->createPositionalParameter($id)));
  52. if ($userId === null) {
  53. $qb->andWhere($qb->expr()->isNull('user_id'));
  54. } else {
  55. $qb->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
  56. }
  57. return $this->findEntity($qb);
  58. }
  59. /**
  60. * @param string $userId
  61. * @param string $appId
  62. * @param string|null $identifier
  63. * @return array
  64. * @throws Exception
  65. */
  66. public function findUserTasksByApp(string $userId, string $appId, ?string $identifier = null): array {
  67. $qb = $this->db->getQueryBuilder();
  68. $qb->select(Task::$columns)
  69. ->from($this->tableName)
  70. ->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)))
  71. ->andWhere($qb->expr()->eq('app_id', $qb->createPositionalParameter($appId)));
  72. if ($identifier !== null) {
  73. $qb->andWhere($qb->expr()->eq('identifier', $qb->createPositionalParameter($identifier)));
  74. }
  75. return $this->findEntities($qb);
  76. }
  77. /**
  78. * @param int $timeout
  79. * @return int the number of deleted tasks
  80. * @throws Exception
  81. */
  82. public function deleteOlderThan(int $timeout): int {
  83. $qb = $this->db->getQueryBuilder();
  84. $qb->delete($this->tableName)
  85. ->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter(time() - $timeout)));
  86. return $qb->executeStatement();
  87. }
  88. public function update(Entity $entity): Entity {
  89. $entity->setLastUpdated($this->timeFactory->now()->getTimestamp());
  90. return parent::update($entity);
  91. }
  92. }