1
0
Fork 0

add entity, mapper and service for subscriptions_webpush

This commit is contained in:
Jonathan Treffler 2024-07-23 02:05:38 +02:00
parent b36b13703b
commit f994a182d3
5 changed files with 142 additions and 2 deletions

View file

@ -10,7 +10,6 @@ class Subscription extends Entity implements JsonSerializable {
protected $userId;
protected $collectionName;
protected $transport;
protected $data;
protected $creationTimestamp;
protected $expirationTimestamp;
@ -25,7 +24,6 @@ class Subscription extends Entity implements JsonSerializable {
'userId' => $this->userId,
'collectionName' => $this->collectionName,
'transport' => $this->transport,
'data' => $this->data,
'creationTimestamp' => $this->creationTimestamp,
'expirationTimestamp' => $this->expirationTimestamp
];

View file

@ -0,0 +1,23 @@
<?php
namespace OCA\DavPush\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class WebPushSubscription extends Entity implements JsonSerializable {
protected $subscriptionId;
protected $pushResource;
public function __construct() {
$this->addType('subscriptionId','integer');
}
public function jsonSerialize(): array {
return [
'subscriptionId' => $this->subscriptionId,
'pushResource' => $this->pushResource,
];
}
}

View file

@ -0,0 +1,49 @@
<?php
namespace OCA\DavPush\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
class WebPushSubscriptionMapper extends QBMapper {
public const TABLENAME = 'dav_push_subscriptions_webpush';
public function __construct(IDBConnection $db) {
parent::__construct($db, self::TABLENAME, WebPushSubscription::class);
}
/**
* @param int $subscriptionId
* @return Entity|WebPushSubscription
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws DoesNotExistException
*/
public function findBySubscriptionId(int $subscriptionId): Subscription {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from(self::TABLENAME)
->where($qb->expr()->eq('subscription_id', $qb->createNamedParameter($subscriptionId, IQueryBuilder::PARAM_INT)));
return $this->findEntity($qb);
}
/**
* @param string $pushResource
* @return Entity|WebPushSubscription
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
* @throws DoesNotExistException
*/
public function findByPushResource(string $pushResource): Subscription {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from(self::TABLENAME)
->where($qb->expr()->eq('push_resource', $qb->createNamedParameter($pushResource)));
return $this->findEntity($qb);
}
}

View file

@ -0,0 +1,6 @@
<?php
namespace OCA\DavPush\Errors;
class WebPushSubscriptionNotFound extends NotFoundException {
}

View file

@ -0,0 +1,64 @@
<?php
namespace OCA\DavPush\Service;
use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCA\DavPush\Errors\WebPushSubscriptionNotFound;
use OCA\DavPush\Db\WebPushSubscription;
use OCA\DavPush\Db\WebPushSubscriptionMapper;
class WebPushSubscriptionService {
public function __construct(
private WebPushSubscriptionMapper $mapper
) {
}
private function handleException(Exception $e): void {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new WebPushSubscriptionNotFound($e->getMessage());
} else {
throw $e;
}
}
public function findBySubscriptionId(int $subscriptionId): ?WebPushSubscription {
try {
return $this->mapper->findBySubscriptionId($subscriptionId);
} catch (Exception $e) {
$this->handleException($e);
}
}
public function findByPushResource(string $pushResource): ?WebPushSubscription {
try {
return $this->mapper->findByPushResource($pushResource);
} catch (Exception $e) {
$this->handleException($e);
}
}
public function create(int $subscriptionId, string $pushResource): WebPushSubscription {
$webPushSubscription = new WebPushSubscription();
$webPushSubscription->setSubscriptionId($subscriptionId);
$webPushSubscription->setPushResource($pushResource);
$webPushSubscription = $this->mapper->insert($webPushSubscription);
return $webPushSubscription;
}
public function deleteBySubscriptionId(int $subscriptionId): ?WebPushSubscription {
try {
$webPushSubscription = $this->mapper->findBySubscriptionId($subscriptionId);
$this->mapper->delete($webPushSubscription);
return $webPushSubscription;
} catch (Exception $e) {
$this->handleException($e);
}
}
}