68 lines
2.1 KiB
PHP
68 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Monitor\Framework\Controller;
|
|
|
|
use App\Download\Action\Input\DeleteDownloadInput;
|
|
use App\Monitor\Action\Handler\AddMonitorHandler;
|
|
use App\Monitor\Action\Handler\DeleteMonitorHandler;
|
|
use App\Monitor\Action\Input\AddMonitorInput;
|
|
use App\Monitor\Action\Input\DeleteMonitorInput;
|
|
use App\Util\Broadcaster;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
|
use Symfony\Component\Mercure\HubInterface;
|
|
use Symfony\Component\Mercure\Update;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
class ApiController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private readonly Broadcaster $broadcaster,
|
|
) {}
|
|
|
|
#[Route('/api/monitor', name: 'api_monitor', methods: ['POST'])]
|
|
public function addMonitor(
|
|
AddMonitorInput $input,
|
|
AddMonitorHandler $handler,
|
|
HubInterface $hub,
|
|
) {
|
|
$command = $input->toCommand();
|
|
$command->userId = $this->getUser()->getId();
|
|
$response = $handler->handle($command);
|
|
|
|
$this->broadcaster->alert(
|
|
title: 'Success',
|
|
message: "New monitor added for {$input->title}",
|
|
);
|
|
|
|
return $this->json([
|
|
'status' => 200,
|
|
'message' => $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
|
|
]);
|
|
}
|
|
}
|