From c58f33003ac2d9e068277555cae0d38b0271bc10 Mon Sep 17 00:00:00 2001 From: Buster Neece Date: Mon, 22 Jul 2024 08:59:56 -0500 Subject: [PATCH] #21 -- Make spotlighted worlds manageable via admin UI. --- backend/config/routes.php | 22 ++++- ...ddWorldAction.php => WorldsController.php} | 75 ++++++++++++-- .../templates/dashboard/admin/add_world.twig | 29 ------ backend/templates/dashboard/admin/users.twig | 2 +- .../dashboard/admin/worlds/create.twig | 37 +++++++ .../dashboard/admin/worlds/list.twig | 97 +++++++++++++++++++ backend/templates/dashboard/index.twig | 4 +- .../layouts/sections/dashboard_sidebar.twig | 3 +- 8 files changed, 223 insertions(+), 46 deletions(-) rename backend/src/Controller/Dashboard/Admin/{AddWorldAction.php => WorldsController.php} (59%) delete mode 100644 backend/templates/dashboard/admin/add_world.twig create mode 100644 backend/templates/dashboard/admin/worlds/create.twig create mode 100644 backend/templates/dashboard/admin/worlds/list.twig diff --git a/backend/config/routes.php b/backend/config/routes.php index caba747..b6348ab 100644 --- a/backend/config/routes.php +++ b/backend/config/routes.php @@ -37,11 +37,23 @@ ->setName('dashboard'); $group->group('/admin', function (RouteCollectorProxy $group) { - $group->map( - ['GET', 'POST'], - '/add_world', - App\Controller\Dashboard\Admin\AddWorldAction::class - )->setName('dashboard:admin:add_world'); + $group->group('/worlds', function (RouteCollectorProxy $group) { + $group->get( + '', + App\Controller\Dashboard\Admin\WorldsController::class . ':listAction' + )->setName('dashboard:admin:worlds'); + + $group->map( + ['GET', 'POST'], + '/create', + App\Controller\Dashboard\Admin\WorldsController::class . ':createAction' + )->setName('dashboard:admin:worlds:create'); + + $group->get( + '/delete[/{id}]', + App\Controller\Dashboard\Admin\WorldsController::class . ':deleteAction' + )->setName('dashboard:admin:worlds:delete'); + }); $group->get( '/users', diff --git a/backend/src/Controller/Dashboard/Admin/AddWorldAction.php b/backend/src/Controller/Dashboard/Admin/WorldsController.php similarity index 59% rename from backend/src/Controller/Dashboard/Admin/AddWorldAction.php rename to backend/src/Controller/Dashboard/Admin/WorldsController.php index bb7c49d..f2af8db 100644 --- a/backend/src/Controller/Dashboard/Admin/AddWorldAction.php +++ b/backend/src/Controller/Dashboard/Admin/WorldsController.php @@ -2,16 +2,20 @@ namespace App\Controller\Dashboard\Admin; +use App\Exception\NotFoundException; use App\Http\Response; use App\Http\ServerRequest; use App\Media; use App\Service\VrcApi; +use Doctrine\DBAL\ArrayParameterType; use Doctrine\DBAL\Connection; use GuzzleHttp\Client; use Intervention\Image\ImageManager; +use League\Flysystem\UnableToDeleteFile; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\UploadedFileInterface; -final readonly class AddWorldAction +final readonly class WorldsController { private Client $vrcApiClient; @@ -24,7 +28,29 @@ public function __construct( $this->vrcApiClient = $vrcApi->getHttpClient(); } - public function __invoke( + public function listAction( + ServerRequest $request, + Response $response, + array $params + ): ResponseInterface { + $worlds = $this->db->fetchAllAssociative( + <<<'SQL' + SELECT w.* + FROM web_worlds AS w + ORDER BY id DESC + SQL + ); + + return $request->getView()->renderToResponse( + $response, + 'dashboard/admin/worlds/list', + [ + 'worlds' => $worlds, + ] + ); + } + + public function createAction( ServerRequest $request, Response $response, array $params @@ -72,12 +98,9 @@ public function __invoke( ] ); - $worldDbId = $this->db->lastInsertId(); - $request->getFlash()->success('World successfully imported!'); - return $response->withRedirect( - $request->getRouter()->urlFor('world', ['id' => $worldDbId]) + $request->getRouter()->urlFor('dashboard:admin:worlds') ); } catch (\Throwable $e) { $error = $e->getMessage(); @@ -86,10 +109,48 @@ public function __invoke( return $request->getView()->renderToResponse( $response, - 'dashboard/admin/add_world', + 'dashboard/admin/worlds/create', [ 'error' => $error, ] ); } + + public function deleteAction( + ServerRequest $request, + Response $response, + array $params + ): ResponseInterface { + $id = $params['id']; + + $world = $this->db->fetchAssociative( + <<<'SQL' + SELECT id, image + FROM web_worlds + WHERE id = :id + SQL, + [ + 'id' => $id, + ] + ); + + if ($world === false) { + throw NotFoundException::world($request); + } + + $fs = Media::getFilesystem(); + $fs->delete($world['image']); + + $this->db->delete( + 'web_worlds', + [ + 'id' => $world['id'], + ] + ); + + $request->getFlash()->success('World removed.'); + return $response->withRedirect( + $request->getRouter()->urlFor('dashboard:admin:worlds') + ); + } } diff --git a/backend/templates/dashboard/admin/add_world.twig b/backend/templates/dashboard/admin/add_world.twig deleted file mode 100644 index f7c9f4a..0000000 --- a/backend/templates/dashboard/admin/add_world.twig +++ /dev/null @@ -1,29 +0,0 @@ -{% extends "layouts/dashboard.twig" %} - -{% import "macros/breadcrumbs.twig" as breadcrumbs %} - -{% block content %} - {{ breadcrumbs.body( - { - 'dashboard': 'My Dashboard' - }, - 'Add VRChat World' - ) }} - -

Add VRChat World

- - {% if error %} -
- Error: {{ error }} -
- {% endif %} - -
-
- - -
- - -
-{% endblock %} diff --git a/backend/templates/dashboard/admin/users.twig b/backend/templates/dashboard/admin/users.twig index f8f1d7d..02ba827 100644 --- a/backend/templates/dashboard/admin/users.twig +++ b/backend/templates/dashboard/admin/users.twig @@ -67,7 +67,7 @@ Team? DJ? Banned? - Action + Actions diff --git a/backend/templates/dashboard/admin/worlds/create.twig b/backend/templates/dashboard/admin/worlds/create.twig new file mode 100644 index 0000000..927678b --- /dev/null +++ b/backend/templates/dashboard/admin/worlds/create.twig @@ -0,0 +1,37 @@ +{% extends "layouts/dashboard.twig" %} + +{% import "macros/breadcrumbs.twig" as breadcrumbs %} + +{% block content %} + {{ breadcrumbs.body( + { + 'dashboard': 'My Dashboard', + 'dashboard:admin:worlds': 'Spotlighted Worlds' + }, + 'Add New Spotlighted World' + ) }} + +

Spotlighted Worlds

+ +
+

+ Add New Spotlighted World +

+
+ {% if error %} +
+ Error: {{ error }} +
+ {% endif %} + +
+
+ + +
+ + +
+
+
+{% endblock %} diff --git a/backend/templates/dashboard/admin/worlds/list.twig b/backend/templates/dashboard/admin/worlds/list.twig new file mode 100644 index 0000000..f658e0a --- /dev/null +++ b/backend/templates/dashboard/admin/worlds/list.twig @@ -0,0 +1,97 @@ +{% extends "layouts/dashboard.twig" %} + +{% import "macros/breadcrumbs.twig" as breadcrumbs %} + +{% block head %} + {{ parent() }} + + +{% endblock %} + +{% block content %} + {{ breadcrumbs.body( + { + 'dashboard': 'My Dashboard' + }, + 'Spotlighted Worlds' + ) }} + +

Spotlighted Worlds

+ +
+
+ + + + + + + + + + + + {% for row in worlds %} + + + + + + {% endfor %} + +
ImageWorld NameActions
+ + {{ row.title }} + + View + + + Remove + +
+
+
+{% endblock %} diff --git a/backend/templates/dashboard/index.twig b/backend/templates/dashboard/index.twig index f3f0676..2fef771 100644 --- a/backend/templates/dashboard/index.twig +++ b/backend/templates/dashboard/index.twig @@ -82,8 +82,8 @@ User Management - Add VRChat World + href="{{ urlFor('dashboard:admin:worlds') }}"> + Spotlighted Worlds diff --git a/backend/templates/layouts/sections/dashboard_sidebar.twig b/backend/templates/layouts/sections/dashboard_sidebar.twig index 99ff608..527f1dd 100644 --- a/backend/templates/layouts/sections/dashboard_sidebar.twig +++ b/backend/templates/layouts/sections/dashboard_sidebar.twig @@ -51,8 +51,7 @@ Management