refactor(api): add wishlists get controller

This commit is contained in:
Jay Trees
2026-04-07 13:18:47 +02:00
parent 4f988aaee2
commit 00108bb083
3 changed files with 54 additions and 1 deletions

View File

@@ -145,6 +145,7 @@ $router = new Router();
$router->get('/', [PageControllerHome::class, 'default']);
$router->get('/api/blog', [PageControllerApiBlog::class, 'blog']);
$router->get('/api/statistics/all', [PageControllerApiStatistics::class, 'all']);
$router->get('/api/wishlists', [PageControllerApiWishlists::class, 'get']);
$router->get('/blog', [PageControllerBlog::class, 'default']);
$router->get('/blog/post/(?<slug>.+)', [PageControllerBlogPost::class, 'default']);
$router->get('/changelog', [PageControllerChangelog::class, 'default']);

View File

@@ -162,7 +162,7 @@ global $options, $locale;
*/
<?php
$api_urls = [
'get wishlists' => '/index.php?page=api&module=wishlists',
'get wishlists' => '/api/wishlists',
'get wishes by wishlist id' => '/index.php?page=api&module=wishlists&style={style}&priority={priority}&wishlist_id={wishlistid}',
'get wishes by wishlist hash' => '/index.php?page=api&module=wishlists&style={style}&priority={priority}&wishlist_hash={wishlisthash}',
'delete wishlist' => '/index.php?page=api&module=wishlists',

View File

@@ -0,0 +1,52 @@
<?php
namespace wishthis;
class PageControllerApiWishlists extends PageController
{
protected string $id = 'api-wishlists';
private int $wishlistId;
public function __construct(array $parameters = [])
{
$this->pageTitle = 'API';
$this->wishlistId = $parameters['id'];
parent::__construct();
}
public function get(): void
{
$user = User::getCurrent();
$wishlists = [];
$wishlistsItems = [];
foreach ($user->getWishlists() as $wishlistData) {
$wishlist = new Wishlist($wishlistData);
$wishlistId = $wishlist->getId();
$wishlistName = $wishlist->getName();
$wishlists[] = [
'id' => $wishlistId,
'hash' => $wishlist->getHash(),
'userId' => $wishlist->getUserId(),
];
$wishlistsItems[] = [
'name' => $wishlistName,
'value' => $wishlistId,
'text' => $wishlistName,
];
}
$response['wishlists'] = $wishlists;
$response['wishlistsItems'] = $wishlistsItems;
$response['warning'] = \ob_get_clean();
$response['success'] = true;
\header('Content-type: application/json; charset=utf-8');
echo \json_encode($response);
}