diff options
| author | David T. Sadler <davidtsadler@googlemail.com> | 2021-10-13 21:51:07 +0100 |
|---|---|---|
| committer | David T. Sadler <davidtsadler@googlemail.com> | 2021-10-13 21:51:07 +0100 |
| commit | cbaedbc5251f3b127bd81242d1344c0cd3e56e0c (patch) | |
| tree | f76273742992d8f06983fd5d6fef37efc85a2c07 /public/delete | |
| parent | 2ab5661583d74b03c86bb4f437616bb634d9c4fc (diff) | |
Implemente redirects
Diffstat (limited to 'public/delete')
| -rw-r--r-- | public/delete/confirm/index.php | 31 | ||||
| -rw-r--r-- | public/delete/index.php | 20 |
2 files changed, 43 insertions, 8 deletions
diff --git a/public/delete/confirm/index.php b/public/delete/confirm/index.php new file mode 100644 index 0000000..6636fe9 --- /dev/null +++ b/public/delete/confirm/index.php @@ -0,0 +1,31 @@ +<?php + +declare(strict_types=1); + +use DTS\BookmarkRepository; +use DTS\Template; +use function DTS\Functions\respondAndExit; + +require_once(__DIR__.'/../../../autoload.php'); + +$config = require_once(__DIR__.'/../../../config.php'); + +if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'GET') { + respondAndExit(405, 'Method Not Allowed'); +} + +$id = filter_input(INPUT_GET, 'id'); + +$bookmarks = new BookmarkRepository($config['path_to_repository']); + +$template = new Template($config['path_to_templates']); + +$bookmark = $bookmarks->find($id); + +if ($bookmark === null) { + respondAndExit(404, 'Not Found'); +} + +$html = $template->render('confirm_deletion', compact('bookmark')); + +respondAndExit(200, 'OK', $html); diff --git a/public/delete/index.php b/public/delete/index.php index 1b2484d..e07129a 100644 --- a/public/delete/index.php +++ b/public/delete/index.php @@ -3,25 +3,29 @@ declare(strict_types=1); use DTS\BookmarkRepository; -use DTS\Template; use function DTS\Functions\respondAndExit; +use function DTS\Functions\redirectAndExit; require_once(__DIR__.'/../../autoload.php'); $config = require_once(__DIR__.'/../../config.php'); -$bookmarks = new BookmarkRepository($config['path_to_repository']); +if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') { + respondAndExit(405, 'Method Not Allowed'); +} -$template = new Template($config['path_to_templates']); +$id = filter_input(INPUT_POST, 'id'); -$id = filter_input(INPUT_GET, 'id') ?? filter_input(INPUT_POST, 'id') ?? null; +$bookmarks = new BookmarkRepository($config['path_to_repository']); $bookmark = $bookmarks->find($id); -if ('POST' === filter_input(INPUT_SERVER, 'REQUEST_METHOD')) { - $bookmarks->delete($bookmark); +if ($bookmark === null) { + respondAndExit(404, 'Not Found'); } -$html = $template->render('delete', compact('bookmark')); +if (!$bookmarks->delete($bookmark)) { + respondAndExit(500, 'Internal Server Error'); +} -respondAndExit(200, 'OK', $html); +redirectAndExit('/'); |
