diff options
Diffstat (limited to 'public/delete')
| -rw-r--r-- | public/delete/confirm/index.php | 32 | ||||
| -rw-r--r-- | public/delete/index.php | 37 |
2 files changed, 69 insertions, 0 deletions
diff --git a/public/delete/confirm/index.php b/public/delete/confirm/index.php new file mode 100644 index 0000000..9903f14 --- /dev/null +++ b/public/delete/confirm/index.php @@ -0,0 +1,32 @@ +<?php + +declare(strict_types=1); + +use DTS\TodoRepository; +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'); + +$todos = new TodoRepository($config['path_to_repository']); + +$template = new Template($config['path_to_templates']); + +$todo = $todos->find($id); + +if ($todo === null) { + respondAndExit(404, 'Not Found'); +} + +$html = $template->render('confirm_deletion', compact('todo')); + +respondAndExit(200, 'OK', $html); diff --git a/public/delete/index.php b/public/delete/index.php new file mode 100644 index 0000000..a19f568 --- /dev/null +++ b/public/delete/index.php @@ -0,0 +1,37 @@ +<?php + +declare(strict_types=1); + +use DTS\TodoRepository; +use DTS\Session; + +use function DTS\Functions\respondAndExit; +use function DTS\Functions\redirectAndExit; + +require_once(__DIR__.'/../../autoload.php'); + +$config = require_once(__DIR__.'/../../config.php'); + +$session = Session::getInstance(); + +if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') { + respondAndExit(405, 'Method Not Allowed'); +} + +$id = filter_input(INPUT_POST, 'id'); + +$todos = new TodoRepository($config['path_to_repository']); + +$todo = $todos->find($id); + +if ($todo === null) { + respondAndExit(404, 'Not Found'); +} + +if (!$todos->delete($todo)) { + respondAndExit(500, 'Internal Server Error'); +} + +$session->set('message', 'Todo Deleted'); + +redirectAndExit('/'); |
