From 2eb5a432d2229788ce2fdb09f36c6f4bebdea813 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Fri, 7 Feb 2020 09:44:59 +0100 Subject: Initial commit --- src/Handlers/ApiTagsHandler.php | 16 +++++++++++++ src/Handlers/Index.php | 20 ++++++++++++++++ src/Handlers/JsonAPIHandler.php | 18 ++++++++++++++ src/Handlers/JsonAPIResult.php | 24 +++++++++++++++++++ src/Handlers/MethodNotAllowedHandler.php | 14 +++++++++++ src/Handlers/NewNote.php | 40 ++++++++++++++++++++++++++++++++ src/Handlers/NotFoundHandler.php | 15 ++++++++++++ src/Handlers/NoteHandler.php | 39 +++++++++++++++++++++++++++++++ src/Handlers/Search.php | 33 ++++++++++++++++++++++++++ 9 files changed, 219 insertions(+) create mode 100644 src/Handlers/ApiTagsHandler.php create mode 100644 src/Handlers/Index.php create mode 100644 src/Handlers/JsonAPIHandler.php create mode 100644 src/Handlers/JsonAPIResult.php create mode 100644 src/Handlers/MethodNotAllowedHandler.php create mode 100644 src/Handlers/NewNote.php create mode 100644 src/Handlers/NotFoundHandler.php create mode 100644 src/Handlers/NoteHandler.php create mode 100644 src/Handlers/Search.php (limited to 'src/Handlers') diff --git a/src/Handlers/ApiTagsHandler.php b/src/Handlers/ApiTagsHandler.php new file mode 100644 index 0000000..af9fb7b --- /dev/null +++ b/src/Handlers/ApiTagsHandler.php @@ -0,0 +1,16 @@ +db()))); + } +} \ No newline at end of file diff --git a/src/Handlers/Index.php b/src/Handlers/Index.php new file mode 100644 index 0000000..8d0896b --- /dev/null +++ b/src/Handlers/Index.php @@ -0,0 +1,20 @@ +twig()->render("/index.twig", [ + "title" => "hello", + "msg" => "Johoo ", + "tagcloud" => Tag::calcTagCloud(Tag::getTagCounts($env->db())), + ]); + } +} diff --git a/src/Handlers/JsonAPIHandler.php b/src/Handlers/JsonAPIHandler.php new file mode 100644 index 0000000..cc6aa61 --- /dev/null +++ b/src/Handlers/JsonAPIHandler.php @@ -0,0 +1,18 @@ +handleAPIRequest($env, $variables)->send(); + } +} \ No newline at end of file diff --git a/src/Handlers/JsonAPIResult.php b/src/Handlers/JsonAPIResult.php new file mode 100644 index 0000000..905599c --- /dev/null +++ b/src/Handlers/JsonAPIResult.php @@ -0,0 +1,24 @@ +data = $data; + $this->statuscode = $statuscode; + } + + public function send(): void + { + http_response_code($this->statuscode); + header("Content-Type: application/json; charset=UTF-8"); + echo json_encode($this->data); + } +} \ No newline at end of file diff --git a/src/Handlers/MethodNotAllowedHandler.php b/src/Handlers/MethodNotAllowedHandler.php new file mode 100644 index 0000000..53ddb0e --- /dev/null +++ b/src/Handlers/MethodNotAllowedHandler.php @@ -0,0 +1,14 @@ +setContent($content); + $note->setTags($_POST["tag"]); + $note->save($env->db()); + + $url = $env->documentRoot() . "n/" . $note->getId(); + http_response_code(303); + header("Location: {$url}"); + echo 'Note created: '; + } + + echo $env->twig()->render("/new_note.twig", []); + } +} \ No newline at end of file diff --git a/src/Handlers/NotFoundHandler.php b/src/Handlers/NotFoundHandler.php new file mode 100644 index 0000000..1827995 --- /dev/null +++ b/src/Handlers/NotFoundHandler.php @@ -0,0 +1,15 @@ +db(); + + $note = Note::byId($db, $variables["id"]); + if ($note === null) { + (new NotFoundHandler())->handle($env, []); + return; + } + + if ($_SERVER["REQUEST_METHOD"] === "POST") { + if ($_POST["delete"] === "delete") { + $note->delete($db); + http_response_code(303); + $url = $env->documentRoot(); + header("Location: {$url}"); + return; + } + + $note->setContent($_POST["content"]); + $note->setTags($_POST["tag"]); + $note->save($db); + } + + echo $env->twig()->render("/note.twig", ["note" => $note]); + } +} \ No newline at end of file diff --git a/src/Handlers/Search.php b/src/Handlers/Search.php new file mode 100644 index 0000000..30311ea --- /dev/null +++ b/src/Handlers/Search.php @@ -0,0 +1,33 @@ + $_GET["q"] ?? ""]; + + try { + $expr = isset($_GET["q"]) + ? (Parser::parse($_GET["q"]) ?? new TrueExpr()) + : new TrueExpr(); + + $results = SearchResult::search($env->db(), $expr); + $vars["results"] = $results; + } catch (ParseError $e) { + $vars["error"] = $e->getMessage(); + } + + echo $env->twig()->render("/search.twig", $vars); + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2