From 9ec9b423671b2f90429f54b8cb4dcc8b674cc4c0 Mon Sep 17 00:00:00 2001 From: Hamza Mahjoubi Date: Tue, 13 Aug 2024 12:19:20 +0200 Subject: [PATCH] fixup! feat: add internal addresses Signed-off-by: Hamza Mahjoubi --- lib/Controller/InternalAddressController.php | 12 ++++++++-- lib/Controller/PageController.php | 1 + lib/Db/InternalAddressMapper.php | 25 ++++++++++++-------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lib/Controller/InternalAddressController.php b/lib/Controller/InternalAddressController.php index 132a7ff15b..dab6fb930b 100644 --- a/lib/Controller/InternalAddressController.php +++ b/lib/Controller/InternalAddressController.php @@ -15,18 +15,19 @@ use OCA\Mail\Service\InternalAddressService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\Attribute\NoAdminRequired; use OCP\IRequest; class InternalAddressController extends Controller { private ?string $uid; public function __construct(IRequest $request, - ?string $UserId, + ?string $userId, private InternalAddressService $internalAddressService) { parent::__construct(Application::APP_ID, $request); $this->internalAddressService = $internalAddressService; - $this->uid = $UserId; + $this->uid = $userId; } /** @@ -56,6 +57,10 @@ public function setAddress(string $address, string $type): JsonResponse { */ #[TrapError] public function removeAddress(string $address, string $type): JsonResponse { + if($this->uid === null) { + return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); + } + $this->internalAddressService->add( $this->uid, $address, @@ -73,6 +78,9 @@ public function removeAddress(string $address, string $type): JsonResponse { */ #[TrapError] public function list(): JsonResponse { + if($this->uid === null) { + return JsonResponse::error('User not found', Http::STATUS_UNAUTHORIZED); + } $list = $this->internalAddressService->getInternalAddresses( $this->uid ); diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index 9e71a83a0a..a6ef25677d 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -71,6 +71,7 @@ class PageController extends Controller { private IUserManager $userManager; private ?IAvailabilityCoordinator $availabilityCoordinator; private ClassificationSettingsService $classificationSettingsService; + private InternalAddressService $internalAddressService; public function __construct(string $appName, IRequest $request, diff --git a/lib/Db/InternalAddressMapper.php b/lib/Db/InternalAddressMapper.php index 2a7637ffff..aee9cd2f07 100644 --- a/lib/Db/InternalAddressMapper.php +++ b/lib/Db/InternalAddressMapper.php @@ -9,6 +9,7 @@ namespace OCA\Mail\Db; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\QBMapper; use OCP\IDBConnection; @@ -47,17 +48,17 @@ public function exists(string $uid, string $address): bool { return $rows !== []; } - public function create(string $uid, string $address, string $type): void { - $qb = $this->db->getQueryBuilder(); + public function create(string $uid, string $address, string $type): int { - $insert = $qb->insert($this->getTableName()) - ->values([ - 'user_id' => $qb->createNamedParameter($uid), - 'address' => $qb->createNamedParameter($address), - 'type' => $qb->createNamedParameter($type), - ]); + $address = InternalAddress::fromParams([ + 'userId' => $uid, + 'address' => $address, + 'type' => $type, + ]); - $insert->executeStatement(); + $result = $this->insert($address); + + return $result->getId(); } public function remove(string $uid, string $address, string $type): void { @@ -93,6 +94,10 @@ public function find(string $uid, string $address): ?InternalAddress { $qb->expr()->eq('user_id', $qb->createNamedParameter($uid)), $qb->expr()->eq('address', $qb->createNamedParameter($address)) ); - return $this->findEntity($select); + try { + return $this->findEntity($select); + } catch (DoesNotExistException $e) { + return null; + } } }