refactor(api): getting wishlists

This commit is contained in:
grandeljay 2023-08-31 11:30:04 +02:00
parent f61867b0d5
commit 3e58d186a9

View file

@ -103,7 +103,11 @@ switch ($_SERVER['REQUEST_METHOD']) {
break; break;
case 'GET': case 'GET':
if (isset($_GET['wishlist_id'], $_GET['priority'])) { $getWishlistCardsFromPriority = isset($_GET['wishlist_id'], $_GET['priority']);
$getWishlistFromHash = isset($_GET['wishlist_hash']);
$getOwnWishlists = $user->isLoggedIn();
if ($getWishlistCardsFromPriority) {
/** /**
* Get wishlist cards with priority * Get wishlist cards with priority
*/ */
@ -117,7 +121,7 @@ switch ($_SERVER['REQUEST_METHOD']) {
'priority' => '`priority` = ' . $_GET['priority'], 'priority' => '`priority` = ' . $_GET['priority'],
); );
if (-1 == $_GET['priority']) { if (-1 === $_GET['priority']) {
unset($where['priority']); unset($where['priority']);
} }
@ -128,7 +132,6 @@ switch ($_SERVER['REQUEST_METHOD']) {
$options['WHERE'] = '(' . implode(') AND (', $where) . ')'; $options['WHERE'] = '(' . implode(') AND (', $where) . ')';
$response['results'] = $wishlist->getCards($options); $response['results'] = $wishlist->getCards($options);
} elseif (isset($_GET['wishlist_id'])) {
/** /**
* Get wishlist by id * Get wishlist by id
*/ */
@ -144,7 +147,7 @@ switch ($_SERVER['REQUEST_METHOD']) {
} else { } else {
http_response_code(404); http_response_code(404);
} }
} elseif (isset($_GET['wishlist_hash'])) { } elseif ($getWishlistFromHash) {
/** /**
* Get wishlist by hash * Get wishlist by hash
*/ */
@ -155,32 +158,25 @@ switch ($_SERVER['REQUEST_METHOD']) {
} else { } else {
http_response_code(404); http_response_code(404);
} }
} elseif (isset($_GET['userid'])) { } elseif ($getOwnWishlists) {
/** $wishlists = array();
* Get user wishlists $wishlistsItems = array();
*/
$user = $user;
if (!$user->isLoggedIn()) { foreach ($user->getWishlists() as $wishlistData) {
$this->response(403); $wishlist = new Wishlist($wishlistData);
} $wishlistId = $wishlist->getId();
$wishlistName = $wishlist->getName();
$wishlists = array(); $wishlists[$wishlistId] = $wishlist;
$wishlists_items = array(); $wishlistsItems[$wishlistId] = array(
'name' => $wishlistName,
foreach ($user->getWishlists() as $wishlist_result) { 'value' => $wishlistId,
$wishlist = new Wishlist($wishlist_result['id']); 'text' => $wishlistName,
$wishlists[$wishlist->id] = $wishlist;
$wishlists_items[$wishlist->id] = array(
'name' => $wishlist->name,
'value' => $wishlist->id,
'text' => $wishlist->name,
); );
} }
$response['wishlists'] = $wishlists; $response['wishlists'] = $wishlists;
$response['wishlists_items'] = $wishlists_items; $response['wishlists_items'] = $wishlistsItems;
} }
break; break;