diff --git a/assets/controllers/monitor_button_controller.js b/assets/controllers/monitor_button_controller.js new file mode 100644 index 0000000..d07e547 --- /dev/null +++ b/assets/controllers/monitor_button_controller.js @@ -0,0 +1,93 @@ +import { Controller } from '@hotwired/stimulus'; + +/* +* The following line makes this controller "lazy": it won't be downloaded until needed +* See https://symfony.com/bundles/StimulusBundle/current/index.html#lazy-stimulus-controllers +*/ + +/* stimulusFetch: 'lazy' */ +export default class extends Controller { + static targets = ['button', 'options'] + static outlets = ['result-filter'] + static values = { + tmdbId: String, + imdbId: String, + title: String, + } + + initialize() { + // Called once when the controller is first instantiated (per element) + + // Here you can initialize variables, create scoped callables for event + // listeners, instantiate external libraries, etc. + // this._fooBar = this.fooBar.bind(this) + } + + connect() { + // Called every time the controller is connected to the DOM + // (on page load, when it's added to the DOM, moved in the DOM, etc.) + + // Here you can add event listeners on the element or target elements, + // add or remove classes, attributes, dispatch custom events, etc. + // this.fooTarget.addEventListener('click', this._fooBar) + } + + // Add custom controller actions here + // fooBar() { this.fooTarget.classList.toggle(this.bazClass) } + + disconnect() { + // Called anytime its element is disconnected from the DOM + // (on page change, when it's removed from or moved in the DOM, etc.) + + // Here you should remove all event listeners added in "connect()" + // this.fooTarget.removeEventListener('click', this._fooBar) + } + + toggle() { + this.optionsTarget.classList.toggle('hidden'); + } + + async monitorSeries() { + await this.makeMonitor({ + tmdbId: this.tmdbIdValue, + imdbId: this.imdbIdValue, + title: this.titleValue, + monitorType: 'tvshows', + }); + } + + async monitorSeason() { + await this.makeMonitor({ + tmdbId: this.tmdbIdValue, + imdbId: this.imdbIdValue, + title: this.titleValue, + monitorType: 'tvseason', + season: this.resultFilterOutlet.activeFilter['season'], + }); + } + + async monitorEpisode() { + // ToDo: figure out how to set episode + await this.makeMonitor({ + tmdbId: this.tmdbIdValue, + imdbId: this.imdbIdValue, + title: this.titleValue, + monitorType: 'tvepisode', + season: this.resultFilterOutlet.activeFilter['season'], + episode: '', + }); + } + + async makeMonitor(body) { + const response = await fetch('/api/monitor', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }, + body: JSON.stringify(body) + }); + + return await response.json(); + } +} diff --git a/config/packages/framework.yaml b/config/packages/framework.yaml index 681efda..0de15e9 100644 --- a/config/packages/framework.yaml +++ b/config/packages/framework.yaml @@ -2,6 +2,10 @@ framework: secret: '%env(APP_SECRET)%' + serializer: + default_context: + enable_max_depth: true + trusted_proxies: 'private_ranges' # trust *all* "X-Forwarded-*" headers trusted_headers: [ 'x-forwarded-for', 'x-forwarded-host', 'x-forwarded-proto', 'x-forwarded-port', 'x-forwarded-prefix' ] diff --git a/config/routes.yaml b/config/routes.yaml index 29a70ce..9e306f7 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -13,3 +13,11 @@ controllersUser: type: attribute defaults: schemes: ['https'] + +controllersDownload: + resource: + path: ../src/Download/Framework/Controller + namespace: App\Download\Framework\Controller + type: attribute + defaults: + schemes: ['https'] diff --git a/src/Download/Framework/Controller/ApiController.php b/src/Download/Framework/Controller/ApiController.php index 9f87343..e70b8f5 100644 --- a/src/Download/Framework/Controller/ApiController.php +++ b/src/Download/Framework/Controller/ApiController.php @@ -5,16 +5,37 @@ namespace App\Download\Framework\Controller; use App\Download\Action\Handler\AddMonitorHandler; use App\Download\Action\Input\AddMonitorInput; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\DependencyInjection\Attribute\Autowire; +use Symfony\Component\Mercure\HubInterface; +use Symfony\Component\Mercure\Update; use Symfony\Component\Routing\Attribute\Route; +use Twig\Environment; class ApiController extends AbstractController { - #[Route('/monitor', name: 'app_add_movie_monitor', methods: ['POST'])] + public function __construct( + #[Autowire(service: 'twig')] + private readonly Environment $renderer, + private readonly HubInterface $hub, + ) {} + + #[Route('/api/monitor', name: 'api_monitor', methods: ['POST'])] public function addMonitor( AddMonitorInput $input, AddMonitorHandler $handler, + HubInterface $hub, ) { $response = $handler->handle($input->toCommand()); + + $hub->publish(new Update( + 'alerts', + $this->renderer->render('broadcast/Alert.html.twig', [ + 'alert_id' => uniqid(), + 'title' => 'Success', + 'message' => "New monitor added for {$input->title}", + ]) + )); + return $this->json([ 'status' => 200, 'message' => $response diff --git a/src/Download/Framework/Entity/Monitor.php b/src/Download/Framework/Entity/Monitor.php index 9eee59b..663015c 100644 --- a/src/Download/Framework/Entity/Monitor.php +++ b/src/Download/Framework/Entity/Monitor.php @@ -6,6 +6,8 @@ use App\Download\Framework\Repository\MonitorRepository; 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\Component\Serializer\Attribute\MaxDepth; #[ORM\Entity(repositoryClass: MonitorRepository::class)] class Monitor @@ -15,6 +17,7 @@ class Monitor #[ORM\Column] private ?int $id = null; + #[Ignore] #[ORM\ManyToOne(inversedBy: 'yes')] #[ORM\JoinColumn(nullable: false)] private ?User $user = null; diff --git a/templates/components/Filter.html.twig b/templates/components/Filter.html.twig index bbdec63..81e78e2 100644 --- a/templates/components/Filter.html.twig +++ b/templates/components/Filter.html.twig @@ -81,7 +81,7 @@ {% if results.media.mediaType == "tvshows" %}
- diff --git a/templates/search/result.html.twig b/templates/search/result.html.twig index 9490bdf..1aafd93 100644 --- a/templates/search/result.html.twig +++ b/templates/search/result.html.twig @@ -14,18 +14,45 @@

{{ results.media.title }} - {{ results.media.year }}

- + + + + +
+ +

{{ results.media.description }}