AbsenceMapper.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\DAV\Db;
  8. use OCP\AppFramework\Db\DoesNotExistException;
  9. use OCP\AppFramework\Db\MultipleObjectsReturnedException;
  10. use OCP\AppFramework\Db\QBMapper;
  11. use OCP\DB\QueryBuilder\IQueryBuilder;
  12. use OCP\IDBConnection;
  13. /**
  14. * @template-extends QBMapper<Absence>
  15. */
  16. class AbsenceMapper extends QBMapper {
  17. public function __construct(IDBConnection $db) {
  18. parent::__construct($db, 'dav_absence', Absence::class);
  19. }
  20. /**
  21. * @throws DoesNotExistException
  22. * @throws \OCP\DB\Exception
  23. */
  24. public function findById(int $id): Absence {
  25. $qb = $this->db->getQueryBuilder();
  26. $qb->select('*')
  27. ->from($this->getTableName())
  28. ->where($qb->expr()->eq(
  29. 'id',
  30. $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
  31. IQueryBuilder::PARAM_INT),
  32. );
  33. try {
  34. return $this->findEntity($qb);
  35. } catch (MultipleObjectsReturnedException $e) {
  36. // Won't happen as id is the primary key
  37. throw new \RuntimeException(
  38. 'The impossible has happened! The query returned multiple absence settings for one user.',
  39. 0,
  40. $e,
  41. );
  42. }
  43. }
  44. /**
  45. * @throws DoesNotExistException
  46. * @throws \OCP\DB\Exception
  47. */
  48. public function findByUserId(string $userId): Absence {
  49. $qb = $this->db->getQueryBuilder();
  50. $qb->select('*')
  51. ->from($this->getTableName())
  52. ->where($qb->expr()->eq(
  53. 'user_id',
  54. $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR),
  55. IQueryBuilder::PARAM_STR),
  56. );
  57. try {
  58. return $this->findEntity($qb);
  59. } catch (MultipleObjectsReturnedException $e) {
  60. // Won't happen as there is a unique index on user_id
  61. throw new \RuntimeException(
  62. 'The impossible has happened! The query returned multiple absence settings for one user.',
  63. 0,
  64. $e,
  65. );
  66. }
  67. }
  68. /**
  69. * @throws \OCP\DB\Exception
  70. */
  71. public function deleteByUserId(string $userId): void {
  72. $qb = $this->db->getQueryBuilder();
  73. $qb->delete($this->getTableName())
  74. ->where($qb->expr()->eq(
  75. 'user_id',
  76. $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR),
  77. IQueryBuilder::PARAM_STR),
  78. );
  79. $qb->executeStatement();
  80. }
  81. }