diff options
author | Christopher Ng <chrng8@gmail.com> | 2023-07-31 12:10:50 -0700 |
---|---|---|
committer | Christopher Ng <chrng8@gmail.com> | 2023-08-03 15:30:11 -0700 |
commit | 887058591cd6e34d99fd08616dd35b284f3ec9df (patch) | |
tree | 56be3109e37f9e43149439c0f4199f9907185c31 /apps/files_reminders/lib | |
parent | 5ff178a753c335d2ab6d10cee531490f1026ec42 (diff) | |
download | nextcloud-server-887058591cd6e34d99fd08616dd35b284f3ec9df.tar.gz nextcloud-server-887058591cd6e34d99fd08616dd35b284f3ec9df.zip |
feat(files_reminders): create or update
Signed-off-by: Christopher Ng <chrng8@gmail.com>
Diffstat (limited to 'apps/files_reminders/lib')
-rw-r--r-- | apps/files_reminders/lib/Controller/ApiController.php | 4 | ||||
-rw-r--r-- | apps/files_reminders/lib/Db/ReminderMapper.php | 12 | ||||
-rw-r--r-- | apps/files_reminders/lib/Service/ReminderService.php | 24 |
3 files changed, 25 insertions, 15 deletions
diff --git a/apps/files_reminders/lib/Controller/ApiController.php b/apps/files_reminders/lib/Controller/ApiController.php index 10a6654d671..de69997f616 100644 --- a/apps/files_reminders/lib/Controller/ApiController.php +++ b/apps/files_reminders/lib/Controller/ApiController.php @@ -83,7 +83,7 @@ class ApiController extends OCSController { * * @param string $dueDate ISO 8601 formatted date time string */ - public function create(int $fileId, string $dueDate): JSONResponse { + public function set(int $fileId, string $dueDate): JSONResponse { try { $dueDate = (new DateTime($dueDate))->setTimezone(new DateTimeZone('UTC')); } catch (Exception $e) { @@ -97,7 +97,7 @@ class ApiController extends OCSController { } try { - $this->reminderService->create($user, $fileId, $dueDate); + $this->reminderService->createOrUpdate($user, $fileId, $dueDate); return new JSONResponse([], Http::STATUS_OK); } catch (Throwable $th) { $this->logger->error($th->getMessage(), ['exception' => $th]); diff --git a/apps/files_reminders/lib/Db/ReminderMapper.php b/apps/files_reminders/lib/Db/ReminderMapper.php index 4ddea3b303c..db753a50b5c 100644 --- a/apps/files_reminders/lib/Db/ReminderMapper.php +++ b/apps/files_reminders/lib/Db/ReminderMapper.php @@ -56,7 +56,7 @@ class ReminderMapper extends QBMapper { public function find(int $id): Reminder { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') + $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); @@ -69,7 +69,7 @@ class ReminderMapper extends QBMapper { public function findDueForUser(IUser $user, int $fileId): Reminder { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') + $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR))) ->andWhere($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) @@ -86,7 +86,7 @@ class ReminderMapper extends QBMapper { public function findAll() { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') + $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->orderBy('due_date', 'ASC'); @@ -99,7 +99,7 @@ class ReminderMapper extends QBMapper { public function findAllForUser(IUser $user) { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') + $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR))) ->orderBy('due_date', 'ASC'); @@ -113,7 +113,7 @@ class ReminderMapper extends QBMapper { public function findOverdue() { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') + $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->lt('due_date', $qb->createFunction('NOW()'))) ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) @@ -128,7 +128,7 @@ class ReminderMapper extends QBMapper { public function findNotified(?int $limit = null) { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') + $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('notified', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))) ->orderBy('due_date', 'ASC') diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php index ad230474923..0142802f3f1 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -78,13 +78,23 @@ class ReminderService { ); } - public function create(IUser $user, int $fileId, DateTime $dueDate): void { - $reminder = new Reminder(); - $reminder->setUserId($user->getUID()); - $reminder->setFileId($fileId); - $reminder->setDueDate($dueDate); - $reminder->setCreatedAt(new DateTime('now', new DateTimeZone('UTC'))); - $this->reminderMapper->insert($reminder); + public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): void { + $now = new DateTime('now', new DateTimeZone('UTC')); + try { + $reminder = $this->reminderMapper->findDueForUser($user, $fileId); + $reminder->setDueDate($dueDate); + $reminder->setUpdatedAt($now); + $this->reminderMapper->update($reminder); + } catch (DoesNotExistException $e) { + // Create new reminder if no reminder is found + $reminder = new Reminder(); + $reminder->setUserId($user->getUID()); + $reminder->setFileId($fileId); + $reminder->setDueDate($dueDate); + $reminder->setUpdatedAt($now); + $reminder->setCreatedAt($now); + $this->reminderMapper->insert($reminder); + } } /** |