From e7156641fc9757c590bce6363982c7aa202ced5d Mon Sep 17 00:00:00 2001 From: overflowerror Date: Wed, 29 Nov 2023 22:40:15 +0100 Subject: [PATCH] feat: Add logic for redirect --- context.php | 2 ++ controllers/slug/GET.php | 11 ++++++++++- persistence/models/URL.php | 6 +++--- persistence/repositories/URLs.php | 9 ++++++++- router/Router.php | 2 +- utils/error.php | 4 ++++ 6 files changed, 28 insertions(+), 6 deletions(-) diff --git a/context.php b/context.php index 397539e..9eb847d 100644 --- a/context.php +++ b/context.php @@ -2,3 +2,5 @@ const DB_CONNECTION = "DB_CONNECTION"; const REPOSITORIES = "REPOSITORIES"; + +const REQUEST_PATH = "REQUEST_PATH"; \ No newline at end of file diff --git a/controllers/slug/GET.php b/controllers/slug/GET.php index ec210b7..8b676aa 100644 --- a/controllers/slug/GET.php +++ b/controllers/slug/GET.php @@ -1,5 +1,14 @@ urls->getBySlug($path); + + if (!$url) { + redirect("/"); + } else { + redirect($url->url); + } }; diff --git a/persistence/models/URL.php b/persistence/models/URL.php index 2dafa4f..de978fc 100644 --- a/persistence/models/URL.php +++ b/persistence/models/URL.php @@ -4,15 +4,15 @@ class URL { public int $id; public DateTime $created; public DateTime $updated; - public DateTime $deleted; + public ?DateTime $deleted; public string $slug; public string $url; - public string $accessKey; + public ?string $accessKey; public function __construct( string $slug, string $url, - string $accessKey + ?string $accessKey ) { $this->slug = $slug; $this->url = $url; diff --git a/persistence/repositories/URLs.php b/persistence/repositories/URLs.php index 65c9bbe..233b9df 100644 --- a/persistence/repositories/URLs.php +++ b/persistence/repositories/URLs.php @@ -34,7 +34,14 @@ class URLs { if ($statement->rowCount() == 0) { return null; } else { - return $statement->fetch(); + $row = $statement->fetch(); + $url = new URL($row["slug"], $row["url"], $row["access_key"]); + $url->id = $row["id"]; + $url->created = new DateTime($row["created"]); + $url->updated = new DateTime($row["updated"]); + $url->deleted = ($row["deleted"]) ? new DateTime($row["deleted"]) : null; + + return $url; } } } diff --git a/router/Router.php b/router/Router.php index 9c73126..0cdace9 100644 --- a/router/Router.php +++ b/router/Router.php @@ -64,7 +64,7 @@ class Router { $route = $this->notFoundHandler; } - $context["REQUEST_PATH"] = $path; + $context[REQUEST_PATH] = $path; return $route($context); } diff --git a/utils/error.php b/utils/error.php index bb7ffbd..454d8f3 100644 --- a/utils/error.php +++ b/utils/error.php @@ -1,5 +1,9 @@