Skip to content

Commit

Permalink
fixup! feat: add internal addresses
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza Mahjoubi <[email protected]>
  • Loading branch information
hamza221 committed Aug 13, 2024
1 parent 17af694 commit 9ec9b42
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
12 changes: 10 additions & 2 deletions lib/Controller/InternalAddressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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,
Expand All @@ -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
);
Expand Down
1 change: 1 addition & 0 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 15 additions & 10 deletions lib/Db/InternalAddressMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\Mail\Db;

use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\QBMapper;
use OCP\IDBConnection;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit 9ec9b42

Please sign in to comment.