diff --git a/assets/controllers/download_list_controller.js b/assets/controllers/download_list_controller.js index 5e1c867..09889e0 100644 --- a/assets/controllers/download_list_controller.js +++ b/assets/controllers/download_list_controller.js @@ -29,6 +29,12 @@ export default class extends Controller { } } + deleteDownload(data) { + fetch(`/api/download/${data.params.id}`, {method: 'DELETE'}) + .then(res => res.json()) + .then(json => console.debug(json)); + } + // Add custom controller actions here // fooBar() { this.fooTarget.classList.toggle(this.bazClass) } diff --git a/assets/icons/ic/twotone-cancel.svg b/assets/icons/ic/twotone-cancel.svg new file mode 100644 index 0000000..a045a2d --- /dev/null +++ b/assets/icons/ic/twotone-cancel.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/Download/Action/Command/DeleteDownloadCommand.php b/src/Download/Action/Command/DeleteDownloadCommand.php new file mode 100644 index 0000000..123095d --- /dev/null +++ b/src/Download/Action/Command/DeleteDownloadCommand.php @@ -0,0 +1,15 @@ + + */ +class DeleteDownloadCommand implements CommandInterface +{ + public function __construct( + public int $downloadId, + ) {} +} \ No newline at end of file diff --git a/src/Download/Action/Handler/DeleteDownloadHandler.php b/src/Download/Action/Handler/DeleteDownloadHandler.php new file mode 100644 index 0000000..98c7e6b --- /dev/null +++ b/src/Download/Action/Handler/DeleteDownloadHandler.php @@ -0,0 +1,26 @@ + */ +readonly class DeleteDownloadHandler implements HandlerInterface +{ + public function __construct( + private DownloadRepository $downloadRepository, + ) {} + + public function handle(CommandInterface $command): ResultInterface + { + $download = $this->downloadRepository->find($command->downloadId); + $this->downloadRepository->delete($command->downloadId); + + return new DeleteDownloadResult(200, 'Success', $download); + } +} diff --git a/src/Download/Action/Input/DeleteDownloadInput.php b/src/Download/Action/Input/DeleteDownloadInput.php new file mode 100644 index 0000000..432f7e8 --- /dev/null +++ b/src/Download/Action/Input/DeleteDownloadInput.php @@ -0,0 +1,24 @@ + */ +class DeleteDownloadInput implements InputInterface +{ + public function __construct( + #[SourceRoute('downloadId')] + public int $downloadId, + ) {} + + public function toCommand(): CommandInterface + { + return new DeleteDownloadCommand( + $this->downloadId, + ); + } +} \ No newline at end of file diff --git a/src/Download/Action/Result/DeleteDownloadResult.php b/src/Download/Action/Result/DeleteDownloadResult.php new file mode 100644 index 0000000..9a445ef --- /dev/null +++ b/src/Download/Action/Result/DeleteDownloadResult.php @@ -0,0 +1,16 @@ + */ +class DeleteDownloadResult implements ResultInterface +{ + public function __construct( + public int $status, + public string $message, + public Download $download, + ) {} +} diff --git a/src/Download/Framework/Controller/ApiController.php b/src/Download/Framework/Controller/ApiController.php index 0e551ec..3ee6651 100644 --- a/src/Download/Framework/Controller/ApiController.php +++ b/src/Download/Framework/Controller/ApiController.php @@ -2,6 +2,8 @@ namespace App\Download\Framework\Controller; +use App\Download\Action\Handler\DeleteDownloadHandler; +use App\Download\Action\Input\DeleteDownloadInput; use App\Download\Action\Input\DownloadMediaInput; use App\Download\Framework\Repository\DownloadRepository; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -56,4 +58,23 @@ class ApiController extends AbstractController return $this->json(['status' => 200, 'message' => 'Added to Queue']); } + + #[Route('/api/download/{downloadId}', name: 'api_download_delete', methods: ['DELETE'])] + public function deleteDownload( + Request $request, + DeleteDownloadInput $input, + DeleteDownloadHandler $handler, + ): Response { + $result = $handler->handle($input->toCommand()); + $this->hub->publish(new Update( + $request->getSession()->get('mercure_alert_topic'), + $this->renderView('broadcast/Alert.stream.html.twig', [ + 'alert_id' => uniqid(), + 'title' => 'Success', + 'message' => '"' . $result->download->getTitle() . '" has been deleted.', + ]) + )); + + return $this->json(['status' => 200, 'message' => 'Download Deleted']); + } } diff --git a/templates/components/DownloadList.html.twig b/templates/components/DownloadList.html.twig index 2c87bca..23d241d 100644 --- a/templates/components/DownloadList.html.twig +++ b/templates/components/DownloadList.html.twig @@ -23,6 +23,9 @@ class="px-6 py-3 text-start text-xs font-medium text-gray-500 uppercase dark:text-stone-800"> Progress + + @@ -45,15 +48,24 @@ {% if download.progress < 100 %}
-
{{ download.progress }}%
+
+ {{ download.progress }}% +
-{#
#} -{# {{ download.progress }}#} -{#
#} {% else %} - + {% endif %} + + + {% endfor %} {% if this.isWidget == true and this.downloads.items|length > this.perPage %}