diff --git a/assets/controllers/monitor_controller.js b/assets/controllers/monitor_controller.js deleted file mode 100644 index ee6bb8c..0000000 --- a/assets/controllers/monitor_controller.js +++ /dev/null @@ -1,24 +0,0 @@ -import { Controller } from '@hotwired/stimulus'; - -/* -* The following line makes this controller "lazy": it won't be downloaded until needed -* See https://github.com/symfony/stimulus-bridge#lazy-controllers -*/ -/* stimulusFetch: 'lazy' */ -export default class extends Controller { - static values = { - title: String, - tmdbId: String, - imdbId: String, - mediaType: String, - } - - addMovieMonitor() { - console.log(`/monitor/movies/${this.tmdbIdValue}/${this.imdbIdValue}/${encodeURI(this.titleValue)}`) - fetch(`/monitor/movies/${this.tmdbIdValue}/${this.imdbIdValue}/${encodeURI(this.titleValue)}`) - .then(res => res.json()) - .then(json => { - console.log(json) - }) - } -} diff --git a/assets/controllers/monitor_list_controller.js b/assets/controllers/monitor_list_controller.js new file mode 100644 index 0000000..8de6b07 --- /dev/null +++ b/assets/controllers/monitor_list_controller.js @@ -0,0 +1,14 @@ +import { Controller } from '@hotwired/stimulus'; + +/* +* The following line makes this controller "lazy": it won't be downloaded until needed +* See https://github.com/symfony/stimulus-bridge#lazy-controllers +*/ +/* stimulusFetch: 'lazy' */ +export default class extends Controller { + deleteMonitor(data) { + fetch(`/api/monitor/${data.params.id}`, {method: 'DELETE'}) + .then(res => res.json()) + .then(json => console.debug(json)); + } +} diff --git a/src/Monitor/Action/Command/DeleteMonitorCommand.php b/src/Monitor/Action/Command/DeleteMonitorCommand.php new file mode 100644 index 0000000..8a2ffaf --- /dev/null +++ b/src/Monitor/Action/Command/DeleteMonitorCommand.php @@ -0,0 +1,13 @@ + */ +class DeleteMonitorCommand implements CommandInterface +{ + public function __construct( + public string $monitorId, + ) {} +} diff --git a/src/Monitor/Action/Handler/DeleteMonitorHandler.php b/src/Monitor/Action/Handler/DeleteMonitorHandler.php new file mode 100644 index 0000000..78258e7 --- /dev/null +++ b/src/Monitor/Action/Handler/DeleteMonitorHandler.php @@ -0,0 +1,36 @@ + */ +readonly class DeleteMonitorHandler implements HandlerInterface +{ + public function __construct( + private MonitorRepository $monitorRepository, + ) {} + + public function handle(CommandInterface $command): ResultInterface + { + $monitor = $this->monitorRepository->find($command->monitorId); + $this->monitorRepository->getEntityManager()->remove($monitor); + $this->monitorRepository->getEntityManager()->flush(); + + return new DeleteMonitorResult( + status: 'OK', + result: [], + monitor: $monitor + ); + } +} diff --git a/src/Monitor/Action/Input/DeleteMonitorInput.php b/src/Monitor/Action/Input/DeleteMonitorInput.php new file mode 100644 index 0000000..b8e3456 --- /dev/null +++ b/src/Monitor/Action/Input/DeleteMonitorInput.php @@ -0,0 +1,24 @@ + */ +class DeleteMonitorInput implements InputInterface +{ + public function __construct( + #[SourceRoute('monitorId')] + public int $monitorId, + ) {} + + public function toCommand(): CommandInterface + { + return new DeleteMonitorCommand( + $this->monitorId + ); + } +} diff --git a/src/Monitor/Action/Result/DeleteMonitorResult.php b/src/Monitor/Action/Result/DeleteMonitorResult.php new file mode 100644 index 0000000..2d74baf --- /dev/null +++ b/src/Monitor/Action/Result/DeleteMonitorResult.php @@ -0,0 +1,15 @@ + $response ]); } + + #[Route('/api/monitor/{monitorId}', name: 'api_monitor_delete', methods: ['DELETE'])] + public function deleteMonitor( + DeleteMonitorInput $input, + DeleteMonitorHandler $handler, + HubInterface $hub, + ) { + $response = $handler->handle($input->toCommand()); + + $hub->publish(new Update( + 'alerts', + $this->renderer->render('broadcast/Alert.stream.html.twig', [ + 'alert_id' => uniqid(), + 'title' => 'Success', + 'message' => "New monitor added for {$response->monitor->getTitle()}", + ]) + )); + + return $this->json([ + 'status' => 200, + 'message' => $response + ]); + } } diff --git a/src/Monitor/Framework/Entity/Monitor.php b/src/Monitor/Framework/Entity/Monitor.php index 9880eb9..f03af3f 100644 --- a/src/Monitor/Framework/Entity/Monitor.php +++ b/src/Monitor/Framework/Entity/Monitor.php @@ -7,7 +7,9 @@ use App\User\Framework\Entity\User; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Attribute\Ignore; +use Symfony\UX\Turbo\Attribute\Broadcast; +#[Broadcast(template: 'broadcast/Monitor.stream.html.twig')] #[ORM\Entity(repositoryClass: MonitorRepository::class)] class Monitor { diff --git a/templates/broadcast/Monitor.stream.html.twig b/templates/broadcast/Monitor.stream.html.twig new file mode 100644 index 0000000..706d277 --- /dev/null +++ b/templates/broadcast/Monitor.stream.html.twig @@ -0,0 +1,85 @@ +{# Learn how to use Turbo Streams: https://github.com/symfony/ux-turbo#broadcast-doctrine-entities-update #} +{% block create %} + + + + + + +{% endblock %} + +{% block update %} + + + +{% endblock %} + +{% block remove %} + +{% endblock %} diff --git a/templates/components/DownloadList.html.twig b/templates/components/DownloadList.html.twig index 23d241d..df821fd 100644 --- a/templates/components/DownloadList.html.twig +++ b/templates/components/DownloadList.html.twig @@ -62,7 +62,7 @@ diff --git a/templates/components/MonitorList.html.twig b/templates/components/MonitorList.html.twig index 8691174..4dc5d05 100644 --- a/templates/components/MonitorList.html.twig +++ b/templates/components/MonitorList.html.twig @@ -1,6 +1,5 @@ - -

The items you're currently monitoring to automatically download.

- + +
+ - + {% if this.userMonitors.items|length > 0 %} {% for monitor in this.userMonitors.items %} @@ -50,18 +50,27 @@ {% endif %} + {% endfor %} {% if this.userMonitors.items|length > 5 %} - {% endif %} {% else %} - - +
Status
+ +
+ View All Monitors
+
No monitors