Skip to content

Commit

Permalink
fix: create only if file exists
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Ng <chrng8@gmail.com>
  • Loading branch information
Pytal committed Aug 3, 2023
1 parent 777a791 commit d31302e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apps/files_reminders/lib/Controller/ApiController.php
Expand Up @@ -30,6 +30,7 @@
use DateTimeInterface;
use DateTimeZone;
use Exception;
use OCA\FilesReminders\Exception\NodeNotFoundException;
use OCA\FilesReminders\Service\ReminderService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
Expand Down Expand Up @@ -95,6 +96,8 @@ public function set(int $fileId, string $dueDate): JSONResponse {
try {
$this->reminderService->createOrUpdate($user, $fileId, $dueDate);
return new JSONResponse([], Http::STATUS_OK);
} catch (NodeNotFoundException $e) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
} catch (Throwable $th) {
$this->logger->error($th->getMessage(), ['exception' => $th]);
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
Expand Down
8 changes: 8 additions & 0 deletions apps/files_reminders/lib/Service/ReminderService.php
Expand Up @@ -31,6 +31,7 @@
use OCA\FilesReminders\AppInfo\Application;
use OCA\FilesReminders\Db\Reminder;
use OCA\FilesReminders\Db\ReminderMapper;
use OCA\FilesReminders\Exception\NodeNotFoundException;
use OCA\FilesReminders\Exception\UserNotFoundException;
use OCA\FilesReminders\Model\RichReminder;
use OCP\AppFramework\Db\DoesNotExistException;
Expand Down Expand Up @@ -78,6 +79,9 @@ public function getAll(?IUser $user = null) {
);
}

/**
* @throws NodeNotFoundException
*/
public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): void {
$now = new DateTime('now', new DateTimeZone('UTC'));
try {
Expand All @@ -86,6 +90,10 @@ public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): voi
$reminder->setUpdatedAt($now);
$this->reminderMapper->update($reminder);
} catch (DoesNotExistException $e) {
$nodes = $this->root->getUserFolder($user->getUID())->getById($fileId);
if (empty($nodes)) {
throw new NodeNotFoundException();
}
// Create new reminder if no reminder is found
$reminder = new Reminder();
$reminder->setUserId($user->getUID());
Expand Down

0 comments on commit d31302e

Please sign in to comment.