summaryrefslogtreecommitdiff
path: root/public/update
diff options
context:
space:
mode:
Diffstat (limited to 'public/update')
-rw-r--r--public/update/index.php56
1 files changed, 56 insertions, 0 deletions
diff --git a/public/update/index.php b/public/update/index.php
new file mode 100644
index 0000000..82b3c3f
--- /dev/null
+++ b/public/update/index.php
@@ -0,0 +1,56 @@
+<?php
+
+declare(strict_types=1);
+
+use DTS\TodoRepository;
+use DTS\Old;
+use DTS\Session;
+use DTS\Validator;
+
+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');
+}
+
+$old = new Old($_REQUEST);
+
+$session->set('old', $old);
+
+$validator = new Validator($_REQUEST);
+
+if ($validator->errors->count()) {
+ $session->set('errors', $validator->errors);
+
+ redirectAndExit("/edit?id=$todo->id");
+}
+
+$validated = $validator->validated;
+
+$todo->task = $validated->task;
+$todo->tag = $validated->tag;
+
+if (!$todos->update($todo)) {
+ respondAndExit(500, 'Internal Server Error');
+}
+
+$session->set('message', 'Todo Updated');
+
+redirectAndExit('/');