1
0

AbsenceMapper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
  5. *
  6. * @author Richard Steinmetz <richard@steinmetz.cloud>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (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 General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Db;
  25. use OCP\AppFramework\Db\DoesNotExistException;
  26. use OCP\AppFramework\Db\MultipleObjectsReturnedException;
  27. use OCP\AppFramework\Db\QBMapper;
  28. use OCP\DB\QueryBuilder\IQueryBuilder;
  29. use OCP\IDBConnection;
  30. /**
  31. * @template-extends QBMapper<Absence>
  32. */
  33. class AbsenceMapper extends QBMapper {
  34. public function __construct(IDBConnection $db) {
  35. parent::__construct($db, 'dav_absence', Absence::class);
  36. }
  37. /**
  38. * @throws DoesNotExistException
  39. * @throws \OCP\DB\Exception
  40. */
  41. public function findById(int $id): Absence {
  42. $qb = $this->db->getQueryBuilder();
  43. $qb->select('*')
  44. ->from($this->getTableName())
  45. ->where($qb->expr()->eq(
  46. 'id',
  47. $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
  48. IQueryBuilder::PARAM_INT),
  49. );
  50. try {
  51. return $this->findEntity($qb);
  52. } catch (MultipleObjectsReturnedException $e) {
  53. // Won't happen as id is the primary key
  54. throw new \RuntimeException(
  55. 'The impossible has happened! The query returned multiple absence settings for one user.',
  56. 0,
  57. $e,
  58. );
  59. }
  60. }
  61. /**
  62. * @throws DoesNotExistException
  63. * @throws \OCP\DB\Exception
  64. */
  65. public function findByUserId(string $userId): Absence {
  66. $qb = $this->db->getQueryBuilder();
  67. $qb->select('*')
  68. ->from($this->getTableName())
  69. ->where($qb->expr()->eq(
  70. 'user_id',
  71. $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR),
  72. IQueryBuilder::PARAM_STR),
  73. );
  74. try {
  75. return $this->findEntity($qb);
  76. } catch (MultipleObjectsReturnedException $e) {
  77. // Won't happen as there is a unique index on user_id
  78. throw new \RuntimeException(
  79. 'The impossible has happened! The query returned multiple absence settings for one user.',
  80. 0,
  81. $e,
  82. );
  83. }
  84. }
  85. /**
  86. * @throws \OCP\DB\Exception
  87. */
  88. public function deleteByUserId(string $userId): void {
  89. $qb = $this->db->getQueryBuilder();
  90. $qb->delete($this->getTableName())
  91. ->where($qb->expr()->eq(
  92. 'user_id',
  93. $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR),
  94. IQueryBuilder::PARAM_STR),
  95. );
  96. $qb->executeStatement();
  97. }
  98. }