72bf365285
persist and list with database create episodeAction list reponse (with mocked timestamp, started and total) create episodeActions with received values update existing episodeActions by unique episode link receive and store subscription changes deal with multiple subscription changes in single request split database into subdirectories only return subscription changes younger then passed parameter since parse passed timestamp parse passed timestamp for episode_actions listing only return list of urls for subscription changes align list endpoint naming schema store userId with episode actions and subscriptions return json object on application root route
103 lines
3.0 KiB
PHP
103 lines
3.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace OCA\GPodderSync\Controller;
|
|
|
|
use DateTime;
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
|
use GuzzleHttp\Psr7\Response;
|
|
use OCA\GPodderSync\Core\EpisodeAction\EpisodeActionReader;
|
|
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionEntity;
|
|
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
|
|
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionWriter;
|
|
use OCP\AppFramework\Controller;
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
use OCP\IRequest;
|
|
|
|
class EpisodeActionController extends Controller {
|
|
|
|
/**
|
|
* @var EpisodeActionRepository
|
|
*/
|
|
private EpisodeActionRepository $episodeActionRepository;
|
|
/**
|
|
* @var EpisodeActionWriter
|
|
*/
|
|
private EpisodeActionWriter $episodeActionWriter;
|
|
/**
|
|
* @var EpisodeActionReader
|
|
*/
|
|
private EpisodeActionReader $episodeActionReader;
|
|
private $userId;
|
|
|
|
public function __construct(
|
|
string $AppName,
|
|
IRequest $request,
|
|
$UserId,
|
|
EpisodeActionRepository $episodeActionRepository,
|
|
EpisodeActionWriter $episodeActionWriter,
|
|
EpisodeActionReader $episodeActionReader
|
|
) {
|
|
parent::__construct($AppName, $request);
|
|
$this->episodeActionRepository = $episodeActionRepository;
|
|
$this->episodeActionWriter = $episodeActionWriter;
|
|
$this->episodeActionReader = $episodeActionReader;
|
|
$this->userId = $UserId;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @NoAdminRequired
|
|
* @NoCSRFRequired
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function create($data) {
|
|
$episodeAction = $this->episodeActionReader->fromString($data);
|
|
$episodeActionEntity = new EpisodeActionEntity();
|
|
$episodeActionEntity->setPodcast($episodeAction->getPodcast());
|
|
$episodeActionEntity->setEpisode($episodeAction->getEpisode());
|
|
$episodeActionEntity->setAction($episodeAction->getAction());
|
|
$episodeActionEntity->setPosition($episodeAction->getPosition());
|
|
$episodeActionEntity->setStarted($episodeAction->getStarted());
|
|
$episodeActionEntity->setTotal($episodeAction->getTotal());
|
|
$episodeActionEntity->setTimestamp($episodeAction->getTimestamp());
|
|
$episodeActionEntity->setUserId($this->userId);
|
|
|
|
try {
|
|
return $this->episodeActionWriter->save($episodeActionEntity);
|
|
} catch (UniqueConstraintViolationException $ex) {
|
|
$IdEpisodeActionEntityToUpdate = $this->episodeActionRepository->findByEpisode($episodeAction->getEpisode(), $this->userId)->getId();
|
|
$episodeActionEntity->setId($IdEpisodeActionEntityToUpdate);
|
|
return $this->episodeActionWriter->update($episodeActionEntity);
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @NoAdminRequired
|
|
* @NoCSRFRequired
|
|
*
|
|
* @param int $since
|
|
* @return JSONResponse
|
|
*/
|
|
public function list(int $since): JSONResponse {
|
|
$sinceDatetime = $this->createDateTimeFromTimestamp($since);
|
|
return new JSONResponse([
|
|
"actions" => $this->episodeActionRepository->findAll($sinceDatetime, $this->userId),
|
|
"timestamp" => time()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param int|null $since
|
|
*
|
|
* @return DateTime
|
|
*/
|
|
private function createDateTimeFromTimestamp(?int $since): DateTime {
|
|
return ($since)
|
|
? (new \DateTime)->setTimestamp($since)
|
|
: (new \DateTime('-1 week'));
|
|
}
|
|
}
|