1
0

ApiController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\Controller;
  25. use DateTime;
  26. use DateTimeInterface;
  27. use DateTimeZone;
  28. use Exception;
  29. use OCA\FilesReminders\Exception\NodeNotFoundException;
  30. use OCA\FilesReminders\Service\ReminderService;
  31. use OCP\AppFramework\Db\DoesNotExistException;
  32. use OCP\AppFramework\Http;
  33. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  34. use OCP\AppFramework\Http\DataResponse;
  35. use OCP\AppFramework\OCSController;
  36. use OCP\IRequest;
  37. use OCP\IUserSession;
  38. use Psr\Log\LoggerInterface;
  39. class ApiController extends OCSController {
  40. public function __construct(
  41. string $appName,
  42. IRequest $request,
  43. protected ReminderService $reminderService,
  44. protected IUserSession $userSession,
  45. protected LoggerInterface $logger,
  46. ) {
  47. parent::__construct($appName, $request);
  48. }
  49. /**
  50. * Get a reminder
  51. *
  52. * @param int $fileId ID of the file
  53. * @return DataResponse<Http::STATUS_OK, array{dueDate: ?string}, array{}>|DataResponse<Http::STATUS_UNAUTHORIZED, array<empty>, array{}>
  54. *
  55. * 200: Reminder returned
  56. * 401: Account not found
  57. */
  58. #[NoAdminRequired]
  59. public function get(int $fileId): DataResponse {
  60. $user = $this->userSession->getUser();
  61. if ($user === null) {
  62. return new DataResponse([], Http::STATUS_UNAUTHORIZED);
  63. }
  64. try {
  65. $reminder = $this->reminderService->getDueForUser($user, $fileId);
  66. $reminderData = [
  67. 'dueDate' => $reminder->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601
  68. ];
  69. return new DataResponse($reminderData, Http::STATUS_OK);
  70. } catch (DoesNotExistException $e) {
  71. $reminderData = [
  72. 'dueDate' => null,
  73. ];
  74. return new DataResponse($reminderData, Http::STATUS_OK);
  75. }
  76. }
  77. /**
  78. * Set a reminder
  79. *
  80. * @param int $fileId ID of the file
  81. * @param string $dueDate ISO 8601 formatted date time string
  82. *
  83. * @return DataResponse<Http::STATUS_OK|Http::STATUS_CREATED|Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  84. *
  85. * 200: Reminder updated
  86. * 201: Reminder created successfully
  87. * 400: Creating reminder is not possible
  88. * 401: Account not found
  89. * 404: File not found
  90. */
  91. #[NoAdminRequired]
  92. public function set(int $fileId, string $dueDate): DataResponse {
  93. try {
  94. $dueDate = (new DateTime($dueDate))->setTimezone(new DateTimeZone('UTC'));
  95. } catch (Exception $e) {
  96. $this->logger->error($e->getMessage(), ['exception' => $e]);
  97. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  98. }
  99. $user = $this->userSession->getUser();
  100. if ($user === null) {
  101. return new DataResponse([], Http::STATUS_UNAUTHORIZED);
  102. }
  103. try {
  104. $created = $this->reminderService->createOrUpdate($user, $fileId, $dueDate);
  105. if ($created) {
  106. return new DataResponse([], Http::STATUS_CREATED);
  107. }
  108. return new DataResponse([], Http::STATUS_OK);
  109. } catch (NodeNotFoundException $e) {
  110. return new DataResponse([], Http::STATUS_NOT_FOUND);
  111. }
  112. }
  113. /**
  114. * Remove a reminder
  115. *
  116. * @param int $fileId ID of the file
  117. *
  118. * @return DataResponse<Http::STATUS_OK|Http::STATUS_UNAUTHORIZED|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  119. *
  120. * 200: Reminder deleted successfully
  121. * 401: Account not found
  122. * 404: Reminder not found
  123. */
  124. #[NoAdminRequired]
  125. public function remove(int $fileId): DataResponse {
  126. $user = $this->userSession->getUser();
  127. if ($user === null) {
  128. return new DataResponse([], Http::STATUS_UNAUTHORIZED);
  129. }
  130. try {
  131. $this->reminderService->remove($user, $fileId);
  132. return new DataResponse([], Http::STATUS_OK);
  133. } catch (DoesNotExistException $e) {
  134. return new DataResponse([], Http::STATUS_NOT_FOUND);
  135. }
  136. }
  137. }