From 887058591cd6e34d99fd08616dd35b284f3ec9df Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH] feat(files_reminders): create or update Signed-off-by: Christopher Ng --- apps/files_reminders/appinfo/routes.php | 4 ++-- .../lib/Controller/ApiController.php | 4 ++-- .../files_reminders/lib/Db/ReminderMapper.php | 12 +++++----- .../lib/Service/ReminderService.php | 24 +++++++++++++------ 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/apps/files_reminders/appinfo/routes.php b/apps/files_reminders/appinfo/routes.php index f36d6cf6391..e912efe0284 100644 --- a/apps/files_reminders/appinfo/routes.php +++ b/apps/files_reminders/appinfo/routes.php @@ -30,7 +30,7 @@ $requirements = [ return [ 'ocs' => [ - ['name' => 'Api#get', 'url' => '/api/v{version}/get', 'verb' => 'GET', 'requirements' => $requirements], - ['name' => 'Api#create', 'url' => '/api/v{version}/create', 'verb' => 'POST', 'requirements' => $requirements], + ['name' => 'Api#get', 'url' => '/api/v{version}/get/{fileId}', 'verb' => 'GET', 'requirements' => $requirements], + ['name' => 'Api#set', 'url' => '/api/v{version}/set/{fileId}', 'verb' => 'PUT', 'requirements' => $requirements], ], ]; 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); + } } /** -- 2.39.5