diff options
| author | David T. Sadler <davidtsadler@googlemail.com> | 2021-11-13 09:22:25 +0000 |
|---|---|---|
| committer | David T. Sadler <davidtsadler@googlemail.com> | 2021-11-13 09:22:25 +0000 |
| commit | 40997195b7ee07cb1bda978186c1804371e1f16e (patch) | |
| tree | 17a6e4bd6eaca795cfa8d57b9aa6ea8ad7066593 /public/store/index.php | |
| parent | 35dc6c245d93560d7ac81df6323e8b22e571aa95 (diff) | |
Create site
Diffstat (limited to 'public/store/index.php')
| -rw-r--r-- | public/store/index.php | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/public/store/index.php b/public/store/index.php new file mode 100644 index 0000000..c4d24fa --- /dev/null +++ b/public/store/index.php @@ -0,0 +1,52 @@ +<?php + +declare(strict_types=1); + +use DTS\Todo; +use DTS\TodoRepository; +use DTS\Old; +use DTS\Session; +use DTS\Validator; + +use function DTS\Functions\redirectAndExit; +use function DTS\Functions\respondAndExit; + +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'); +} + +$old = new Old($_REQUEST); + +$session->set('old', $old); + +$validator = new Validator($_REQUEST); + +if ($validator->errors->count()) { + $session->set('errors', $validator->errors); + + redirectAndExit('/create'); +} + +$validated = $validator->validated; + +$todos = new TodoRepository($config['path_to_repository']); + +$todo = new Todo(); + +$todo->task = $validated->task; +$todo->tag = $validated->tag; +$todo->addedAt = date('Y-m-d H:i:s'); + +if (!$todos->add($todo)) { + respondAndExit(500, 'Internal Server Error'); +} + +$session->set('message', 'Todo Added'); + +redirectAndExit('/'); |
