From cee4f9944e6802f4000c18d264dc3bc608937113 Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Tue, 31 Dec 2024 16:32:00 +0000 Subject: [PATCH] Add ability to remove feeds --- src/Controllers/FeedController.php | 12 ++++++++++++ src/Controllers/LoginController.php | 4 ++-- src/Models/Repositories/FeedRepository.php | 6 ++++++ src/app.php | 9 +++++---- .../index.twig.html => account/login.twig.html} | 0 views/feed/index.twig.html | 2 +- 6 files changed, 26 insertions(+), 7 deletions(-) rename views/{login/index.twig.html => account/login.twig.html} (100%) diff --git a/src/Controllers/FeedController.php b/src/Controllers/FeedController.php index b0419d0..49ac2d6 100644 --- a/src/Controllers/FeedController.php +++ b/src/Controllers/FeedController.php @@ -54,4 +54,16 @@ class FeedController return $this->view->render($response, 'create.twig.html'); } + + public function delete(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface + { + $this->logger->info("FeedController::delete() called"); + $feed = $this->feedRepository->find($request->getAttribute('id')); + if (empty($feed)) { + return $response->withStatus(404); + } + + $this->feedRepository->delete($feed); + return $response->withStatus(201)->withHeader('Location', '/feed'); + } } \ No newline at end of file diff --git a/src/Controllers/LoginController.php b/src/Controllers/LoginController.php index bc71f4a..34913b9 100644 --- a/src/Controllers/LoginController.php +++ b/src/Controllers/LoginController.php @@ -16,7 +16,7 @@ class LoginController { } public function index(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { - return $this->view->render($response, 'login/index.twig.html', []); + return $this->view->render($response, 'account/login.twig.html', []); } public function login(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { @@ -27,7 +27,7 @@ class LoginController { $user = $this->users->validateCredentials($email, $password); if (!$user) { - return $this->view->render($response, 'login/index.twig.html', ['error' => 'Invalid email or password']); + return $this->view->render($response, 'account/login.twig.html', ['error' => 'Invalid email or password']); } $_SESSION['user'] = $user->id; diff --git a/src/Models/Repositories/FeedRepository.php b/src/Models/Repositories/FeedRepository.php index 6fb0f14..f1233b6 100644 --- a/src/Models/Repositories/FeedRepository.php +++ b/src/Models/Repositories/FeedRepository.php @@ -21,4 +21,10 @@ class FeedRepository extends EntityRepository $this->_em->persist($feed); $this->_em->flush(); } + + public function delete(Feed $feed): void + { + $this->_em->remove($feed); + $this->_em->flush(); + } } \ No newline at end of file diff --git a/src/app.php b/src/app.php index 6f2541c..b9f8cd6 100644 --- a/src/app.php +++ b/src/app.php @@ -24,10 +24,11 @@ $app->add('csrf'); $app->get("/", [HomeController::class, 'get']); -$app->get('/feed', [FeedController::class, 'get']) - ->add($container->get(\Lewisdale\App\Session\LoginMiddleware::class)); - -$app->get('/feed/{id}', [FeedController::class, 'get_feed']); +$app->group('/feed', function (\Slim\Routing\RouteCollectorProxy $group) use ($app) { + $group->get('[/]', [FeedController::class, 'get'])->add(\Lewisdale\App\Session\LoginMiddleware::class); + $group->get('/{id}', [FeedController::class, 'get_feed']); + $group->get('/{id}/delete', [FeedController::class, 'delete'])->add(\Lewisdale\App\Session\LoginMiddleware::class); +}); $app->group('/account', function (\Slim\Routing\RouteCollectorProxy $group) use ($app, $container) { $group->get('/login', [LoginController::class, 'index']); diff --git a/views/login/index.twig.html b/views/account/login.twig.html similarity index 100% rename from views/login/index.twig.html rename to views/account/login.twig.html diff --git a/views/feed/index.twig.html b/views/feed/index.twig.html index 083a8bd..e546d62 100644 --- a/views/feed/index.twig.html +++ b/views/feed/index.twig.html @@ -9,7 +9,7 @@ {% for feed in feeds %} {{ feed.title }} - Link to feed + Link to feedDelete {% endfor %}