Allow viewing saved wishlists

This commit is contained in:
Jay Trees 2022-04-12 09:09:10 +02:00
parent ff4c5bed2d
commit cd909de857
4 changed files with 48 additions and 10 deletions

View file

@ -18,14 +18,7 @@ switch ($_SERVER['REQUEST_METHOD']) {
/**
* Get
*/
$wishlists = $database
->query('SELECT *
FROM `wishlists_saved` `ws`
JOIN `wishlists` `w` ON `w`.`id` = `ws`.`wishlist`
WHERE `ws`.`user` = ' . $user->id . ';')
->fetchAll();
$response['data'] = $wishlists;
$response['data'] = $user->getSavedWishlists();
break;
case 'POST':

View file

@ -108,7 +108,6 @@ $(function() {
});
});
/** Determine if list is saved */
fetch('/src/api/wishlists-saved.php', {
method : 'GET',

View file

@ -44,7 +44,8 @@ class User
global $database;
$user = $database
->query('SELECT * FROM `users`
->query('SELECT *
FROM `users`
WHERE `id` = ' . $this->id . ';')
->fetch();
@ -81,4 +82,33 @@ class User
return $wishlists;
}
public function getSavedWishlists(): array
{
global $database;
$wishlists = array();
$result = $database
->query('SELECT `ws`.`wishlist`,
`w`.`user`,
`w`.`hash`
FROM `wishlists_saved` `ws`
JOIN `wishlists` `w` ON `w`.`id` = `ws`.`wishlist`
WHERE `ws`.`user` = ' . $this->id . ';')
->fetchAll();
if ($result) {
$wishlists = $result;
}
return $wishlists;
}
public function getDisplayName(): string
{
return $this->name_nick
?: $this->name_first
?: $this->email;
}
}

View file

@ -18,7 +18,23 @@ $page->navigation();
<h1 class="ui header"><?= $page->title ?></h1>
<div class="ui segment">
<div class="ui relaxed divided list">
<?php foreach ($user->getSavedWishlists() as $wishlist_saved) { ?>
<?php
$wishlist = new Wishlist($wishlist_saved['wishlist']);
$wishlist_user = new User($wishlist_saved['user']);
?>
<div class="item">
<i class="large heart middle aligned icon"></i>
<div class="content">
<a class="header" href="/?wishlist=<?= $wishlist->hash ?>"><?= $wishlist->getTitle(); ?></a>
<div class="description"><?= $wishlist_user->getDisplayName(); ?></div>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
</main>