From f449570627a67bf0120687c8e69b4003df900311 Mon Sep 17 00:00:00 2001 From: grandeljay Date: Thu, 31 Aug 2023 15:03:48 +0200 Subject: [PATCH] fix: unable to fetch saved wishlists --- src/api/wishes.php | 2 +- src/api/wishlists.php | 6 ++-- src/classes/wishthis/Wishlist.php | 46 +++++++++++++++++++++++++++++++ src/pages/wishlist.php | 2 +- src/pages/wishlists-saved.php | 2 +- 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/api/wishes.php b/src/api/wishes.php index 304fde56..2ed1c5e0 100644 --- a/src/api/wishes.php +++ b/src/api/wishes.php @@ -45,7 +45,7 @@ switch ($_SERVER['REQUEST_METHOD']) { /** * Get wishes by priority */ - $wishlist = new Wishlist($_GET['wishlist_id']); + $wishlist = Wishlist::getFromId($_GET['wishlist_id']); $options = array( 'style' => $_GET['wishlist_style'], ); diff --git a/src/api/wishlists.php b/src/api/wishlists.php index 1ebe0b5d..4beaa747 100644 --- a/src/api/wishlists.php +++ b/src/api/wishlists.php @@ -111,7 +111,7 @@ switch ($_SERVER['REQUEST_METHOD']) { /** * Get wishlist cards with priority */ - $wishlist = new Wishlist($_GET['wishlist_id']); + $wishlist = Wishlist::getFromId($_GET['wishlist_id']); $options = array( 'style' => $_GET['style'], 'placeholders' => array(), @@ -135,7 +135,7 @@ switch ($_SERVER['REQUEST_METHOD']) { /** * Get wishlist by id */ - $wishlist = new Wishlist($_GET['wishlist_id']); + $wishlist = Wishlist::getFromId($_GET['wishlist_id']); if ($wishlist->exists) { /** Determine if user is allowed to access wishlist */ @@ -151,7 +151,7 @@ switch ($_SERVER['REQUEST_METHOD']) { /** * Get wishlist by hash */ - $wishlist = new Wishlist($_GET['wishlist_hash']); + $wishlist = Wishlist::getFromHash($_GET['wishlist_hash']); if ($wishlist->exists) { $response['results'] = $wishlist; diff --git a/src/classes/wishthis/Wishlist.php b/src/classes/wishthis/Wishlist.php index cb85c307..ef2ed6dc 100644 --- a/src/classes/wishthis/Wishlist.php +++ b/src/classes/wishthis/Wishlist.php @@ -10,6 +10,52 @@ namespace wishthis; class Wishlist { + public static function getFromId(int $id): self|false + { + global $database; + + $wishlistQuery = $database->query( + 'SELECT * + FROM `wishlists` + WHERE `wishlists`.`id` = :wishlist_id', + array( + 'wishlist_id' => $id, + ) + ); + + if (false === $wishlistQuery) { + return false; + } + + $wishlistData = $wishlistQuery->fetch(); + $wishlist = new Wishlist($wishlistData); + + return $wishlist; + } + + public static function getFromHash(string $hash): self + { + global $database; + + $wishlistQuery = $database->query( + 'SELECT * + FROM `wishlists` + WHERE `wishlists`.`hash` = :wishlist_hash', + array( + 'wishlist_hash' => $hash, + ) + ); + + if (false === $wishlistQuery) { + return false; + } + + $wishlistData = $wishlistQuery->fetch(); + $wishlist = new Wishlist($wishlistData); + + return $wishlist; + } + /** * The unique wishlist id. * diff --git a/src/pages/wishlist.php b/src/pages/wishlist.php index 624751bb..c17ed04c 100644 --- a/src/pages/wishlist.php +++ b/src/pages/wishlist.php @@ -8,7 +8,7 @@ namespace wishthis; -$wishlist = new Wishlist($_GET['hash']); +$wishlist = Wishlist::getFromHash($_GET['hash']); $wishlist_user = User::getFromID($wishlist->user); $page = new Page(__FILE__, $wishlist->getTitle()); $page->stylesheets['wish'] = 'src/assets/css/wish.css'; diff --git a/src/pages/wishlists-saved.php b/src/pages/wishlists-saved.php index fe6d4109..13d27efb 100644 --- a/src/pages/wishlists-saved.php +++ b/src/pages/wishlists-saved.php @@ -37,7 +37,7 @@ foreach ($wishlists as $wishlist_saved) {
hash; ?>