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) {
/**
* Get user wishlists
*/
$user = $user;
if (!$user->isLoggedIn()) {
$this->response(403);
}
$wishlists = array(); $wishlists = array();
$wishlists_items = array(); $wishlistsItems = array();
foreach ($user->getWishlists() as $wishlist_result) { foreach ($user->getWishlists() as $wishlistData) {
$wishlist = new Wishlist($wishlist_result['id']); $wishlist = new Wishlist($wishlistData);
$wishlistId = $wishlist->getId();
$wishlistName = $wishlist->getName();
$wishlists[$wishlist->id] = $wishlist; $wishlists[$wishlistId] = $wishlist;
$wishlists_items[$wishlist->id] = array( $wishlistsItems[$wishlistId] = array(
'name' => $wishlist->name, 'name' => $wishlistName,
'value' => $wishlist->id, 'value' => $wishlistId,
'text' => $wishlist->name, 'text' => $wishlistName,
); );
} }
$response['wishlists'] = $wishlists; $response['wishlists'] = $wishlists;
$response['wishlists_items'] = $wishlists_items; $response['wishlists_items'] = $wishlistsItems;
} }
break; break;