feat(wishlist): per-user Wünsche + Header-Badge mit Gesamtzahl
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m16s
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 1m16s
Schema-Änderung (Migration 005):
- Tabelle wishlist umgestellt auf PK (recipe_id, profile_id)
- wishlist_like-Tabelle zusammengelegt — Liken WAR schon "will ich auch",
also werden alle bestehenden Likes Memberships auf der neuen Tabelle.
- Alt-Einträge mit added_by_profile_id werden migriert, anonyme gehen
verloren (war inkonsistent, jetzt erzwingen wir profile_id NOT NULL).
Repository:
- listWishlist aggregiert pro Rezept: wanted_by_count, wanted_by_names
(kommagetrennt), on_my_wishlist für das aktive Profil
- listWishlistProfileIds(recipeId) für den Recipe-Page-Loader
- countWishlistRecipes für das Header-Badge (DISTINCT recipe_id)
- addToWishlist/removeFromWishlist/isOnMyWishlist alle mit profile_id
als Pflicht
API:
- POST /api/wishlist: profile_id jetzt Pflicht (nullable raus)
- DELETE /api/wishlist/[recipe_id]?profile_id=X (nur eigenes Entry)
- /api/wishlist/[recipe_id]/like komplett entfernt (Konzept obsolet)
- Neu: GET /api/wishlist/count → { count: <distinct recipes> }
UI:
- Header-Heart bekommt rotes Badge mit Zahl der Wunschliste-Rezepte.
wishlistStore in $lib/client/wishlist.svelte.ts hält den Count reaktiv;
Refresh auf Mount, nach Add/Remove, beim Öffnen der Wunschliste.
- Recipe-Detail: Loader liefert wishlist_profile_ids; onMyWishlist ist
ein $derived. Toggle fragt aktives Profil (alertAction sonst), mutiert
die lokale Liste + ruft wishlistStore.refresh.
- Wunschliste-Seite: Heart toggelt eigenen Wunsch, Count zeigt Gesamt-
wünsche, kommagetrennte Namen zeigen "wer will". Trash-Button
entfernt — Heart-off reicht jetzt.
Tests (99 → 99, 8 neu geschrieben):
- Per-User-Add/Remove, aggregierte Counts, on_my_wishlist, Cascades bei
Recipe/Profile-Delete, countWishlistRecipes = DISTINCT.
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
import { goto, afterNavigate } from '$app/navigation';
|
||||
import { Heart, Settings, CookingPot, Globe, Utensils } from 'lucide-svelte';
|
||||
import { profileStore } from '$lib/client/profile.svelte';
|
||||
import { wishlistStore } from '$lib/client/wishlist.svelte';
|
||||
import ProfileSwitcher from '$lib/components/ProfileSwitcher.svelte';
|
||||
import ConfirmDialog from '$lib/components/ConfirmDialog.svelte';
|
||||
import SearchLoader from '$lib/components/SearchLoader.svelte';
|
||||
@@ -105,6 +106,7 @@
|
||||
|
||||
onMount(() => {
|
||||
profileStore.load();
|
||||
void wishlistStore.refresh();
|
||||
document.addEventListener('click', handleClickOutside);
|
||||
document.addEventListener('keydown', handleKey);
|
||||
return () => {
|
||||
@@ -211,8 +213,17 @@
|
||||
</div>
|
||||
{/if}
|
||||
<div class="bar-right">
|
||||
<a href="/wishlist" class="nav-link" aria-label="Wunschliste">
|
||||
<a
|
||||
href="/wishlist"
|
||||
class="nav-link wishlist-link"
|
||||
aria-label={wishlistStore.count > 0
|
||||
? `Wunschliste (${wishlistStore.count})`
|
||||
: 'Wunschliste'}
|
||||
>
|
||||
<Heart size={20} strokeWidth={2} />
|
||||
{#if wishlistStore.count > 0}
|
||||
<span class="badge">{wishlistStore.count}</span>
|
||||
{/if}
|
||||
</a>
|
||||
<a href="/admin" class="nav-link" aria-label="Einstellungen">
|
||||
<Settings size={20} strokeWidth={2} />
|
||||
@@ -399,10 +410,28 @@
|
||||
border-radius: 999px;
|
||||
text-decoration: none;
|
||||
font-size: 1.15rem;
|
||||
position: relative;
|
||||
}
|
||||
.nav-link:hover {
|
||||
background: #f4f8f5;
|
||||
}
|
||||
.badge {
|
||||
position: absolute;
|
||||
top: -2px;
|
||||
right: -2px;
|
||||
min-width: 18px;
|
||||
height: 18px;
|
||||
padding: 0 5px;
|
||||
border-radius: 999px;
|
||||
background: #c53030;
|
||||
color: white;
|
||||
font-size: 0.7rem;
|
||||
font-weight: 700;
|
||||
line-height: 18px;
|
||||
text-align: center;
|
||||
box-shadow: 0 0 0 2px white;
|
||||
pointer-events: none;
|
||||
}
|
||||
main {
|
||||
padding: 0 1rem 4rem;
|
||||
max-width: 760px;
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
|
||||
const AddSchema = z.object({
|
||||
recipe_id: z.number().int().positive(),
|
||||
profile_id: z.number().int().positive().nullable().optional()
|
||||
profile_id: z.number().int().positive()
|
||||
});
|
||||
|
||||
const VALID_SORTS: readonly SortKey[] = ['popular', 'newest', 'oldest'] as const;
|
||||
@@ -34,7 +34,7 @@ export const GET: RequestHandler = async ({ url }) => {
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
const body = await request.json().catch(() => null);
|
||||
const parsed = AddSchema.safeParse(body);
|
||||
if (!parsed.success) error(400, { message: 'Invalid body' });
|
||||
addToWishlist(getDb(), parsed.data.recipe_id, parsed.data.profile_id ?? null);
|
||||
if (!parsed.success) error(400, { message: 'recipe_id and profile_id required' });
|
||||
addToWishlist(getDb(), parsed.data.recipe_id, parsed.data.profile_id);
|
||||
return json({ ok: true }, { status: 201 });
|
||||
};
|
||||
|
||||
@@ -3,14 +3,15 @@ import { json, error } from '@sveltejs/kit';
|
||||
import { getDb } from '$lib/server/db';
|
||||
import { removeFromWishlist } from '$lib/server/wishlist/repository';
|
||||
|
||||
function parseId(raw: string): number {
|
||||
const id = Number(raw);
|
||||
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid recipe_id' });
|
||||
return id;
|
||||
function parsePositiveInt(raw: string | null, field: string): number {
|
||||
const n = raw === null ? NaN : Number(raw);
|
||||
if (!Number.isInteger(n) || n <= 0) error(400, { message: `Invalid ${field}` });
|
||||
return n;
|
||||
}
|
||||
|
||||
export const DELETE: RequestHandler = async ({ params }) => {
|
||||
const id = parseId(params.recipe_id!);
|
||||
removeFromWishlist(getDb(), id);
|
||||
export const DELETE: RequestHandler = async ({ params, url }) => {
|
||||
const id = parsePositiveInt(params.recipe_id!, 'recipe_id');
|
||||
const profileId = parsePositiveInt(url.searchParams.get('profile_id'), 'profile_id');
|
||||
removeFromWishlist(getDb(), id, profileId);
|
||||
return json({ ok: true });
|
||||
};
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
import type { RequestHandler } from './$types';
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import { z } from 'zod';
|
||||
import { getDb } from '$lib/server/db';
|
||||
import { likeWish, unlikeWish } from '$lib/server/wishlist/repository';
|
||||
|
||||
const Schema = z.object({ profile_id: z.number().int().positive() });
|
||||
|
||||
function parseId(raw: string): number {
|
||||
const id = Number(raw);
|
||||
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid recipe_id' });
|
||||
return id;
|
||||
}
|
||||
|
||||
export const PUT: RequestHandler = async ({ params, request }) => {
|
||||
const id = parseId(params.recipe_id!);
|
||||
const body = await request.json().catch(() => null);
|
||||
const parsed = Schema.safeParse(body);
|
||||
if (!parsed.success) error(400, { message: 'Invalid body' });
|
||||
likeWish(getDb(), id, parsed.data.profile_id);
|
||||
return json({ ok: true });
|
||||
};
|
||||
|
||||
export const DELETE: RequestHandler = async ({ params, request }) => {
|
||||
const id = parseId(params.recipe_id!);
|
||||
const body = await request.json().catch(() => null);
|
||||
const parsed = Schema.safeParse(body);
|
||||
if (!parsed.success) error(400, { message: 'Invalid body' });
|
||||
unlikeWish(getDb(), id, parsed.data.profile_id);
|
||||
return json({ ok: true });
|
||||
};
|
||||
8
src/routes/api/wishlist/count/+server.ts
Normal file
8
src/routes/api/wishlist/count/+server.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import type { RequestHandler } from './$types';
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { getDb } from '$lib/server/db';
|
||||
import { countWishlistRecipes } from '$lib/server/wishlist/repository';
|
||||
|
||||
export const GET: RequestHandler = async () => {
|
||||
return json({ count: countWishlistRecipes(getDb()) });
|
||||
};
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
listFavoriteProfiles,
|
||||
listRatings
|
||||
} from '$lib/server/recipes/actions';
|
||||
import { listWishlistProfileIds } from '$lib/server/wishlist/repository';
|
||||
|
||||
export const load: PageServerLoad = async ({ params }) => {
|
||||
const id = Number(params.id);
|
||||
@@ -19,7 +20,16 @@ export const load: PageServerLoad = async ({ params }) => {
|
||||
const comments = listComments(db, id);
|
||||
const cooking_log = listCookingLog(db, id);
|
||||
const favorite_profile_ids = listFavoriteProfiles(db, id);
|
||||
const wishlist_profile_ids = listWishlistProfileIds(db, id);
|
||||
const avg_stars =
|
||||
ratings.length === 0 ? null : ratings.reduce((s, r) => s + r.stars, 0) / ratings.length;
|
||||
return { recipe, ratings, comments, cooking_log, favorite_profile_ids, avg_stars };
|
||||
return {
|
||||
recipe,
|
||||
ratings,
|
||||
comments,
|
||||
cooking_log,
|
||||
favorite_profile_ids,
|
||||
wishlist_profile_ids,
|
||||
avg_stars
|
||||
};
|
||||
};
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
import RecipeView from '$lib/components/RecipeView.svelte';
|
||||
import StarRating from '$lib/components/StarRating.svelte';
|
||||
import { profileStore } from '$lib/client/profile.svelte';
|
||||
import { wishlistStore } from '$lib/client/wishlist.svelte';
|
||||
import { confirmAction, alertAction } from '$lib/client/confirm.svelte';
|
||||
import type { CommentRow } from '$lib/server/recipes/actions';
|
||||
|
||||
@@ -24,7 +25,7 @@
|
||||
let comments = $state<CommentRow[]>([]);
|
||||
let cookingLog = $state<typeof data.cooking_log>([]);
|
||||
let favoriteProfileIds = $state<number[]>([]);
|
||||
let onWishlist = $state(false);
|
||||
let wishlistProfileIds = $state<number[]>([]);
|
||||
let newComment = $state('');
|
||||
|
||||
let title = $state('');
|
||||
@@ -37,6 +38,7 @@
|
||||
comments = [...data.comments];
|
||||
cookingLog = [...data.cooking_log];
|
||||
favoriteProfileIds = [...data.favorite_profile_ids];
|
||||
wishlistProfileIds = [...data.wishlist_profile_ids];
|
||||
title = data.recipe.title;
|
||||
});
|
||||
|
||||
@@ -50,6 +52,10 @@
|
||||
profileStore.active ? favoriteProfileIds.includes(profileStore.active.id) : false
|
||||
);
|
||||
|
||||
const onMyWishlist = $derived(
|
||||
profileStore.active ? wishlistProfileIds.includes(profileStore.active.id) : false
|
||||
);
|
||||
|
||||
async function setRating(stars: number) {
|
||||
if (!profileStore.active) {
|
||||
await alertAction({
|
||||
@@ -195,27 +201,28 @@
|
||||
}
|
||||
|
||||
async function toggleWishlist() {
|
||||
if (onWishlist) {
|
||||
await fetch(`/api/wishlist/${data.recipe.id}`, { method: 'DELETE' });
|
||||
onWishlist = false;
|
||||
if (!profileStore.active) {
|
||||
await alertAction({
|
||||
title: 'Kein Profil gewählt',
|
||||
message: 'Tippe oben rechts auf „Profil wählen", dann klappt die Aktion.'
|
||||
});
|
||||
return;
|
||||
}
|
||||
const profileId = profileStore.active.id;
|
||||
if (onMyWishlist) {
|
||||
await fetch(`/api/wishlist/${data.recipe.id}?profile_id=${profileId}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
wishlistProfileIds = wishlistProfileIds.filter((id) => id !== profileId);
|
||||
} else {
|
||||
await fetch('/api/wishlist', {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
recipe_id: data.recipe.id,
|
||||
profile_id: profileStore.active?.id ?? null
|
||||
})
|
||||
body: JSON.stringify({ recipe_id: data.recipe.id, profile_id: profileId })
|
||||
});
|
||||
onWishlist = true;
|
||||
wishlistProfileIds = [...wishlistProfileIds, profileId];
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshWishlistState() {
|
||||
const res = await fetch('/api/wishlist?sort=newest');
|
||||
if (!res.ok) return;
|
||||
const body = await res.json();
|
||||
onWishlist = body.entries.some((e: { recipe_id: number }) => e.recipe_id === data.recipe.id);
|
||||
void wishlistStore.refresh();
|
||||
}
|
||||
|
||||
// Wake-Lock
|
||||
@@ -232,7 +239,6 @@
|
||||
|
||||
onMount(() => {
|
||||
void requestWakeLock();
|
||||
void refreshWishlistState();
|
||||
const onVisibility = () => {
|
||||
if (document.visibilityState === 'visible' && !wakeLock) void requestWakeLock();
|
||||
};
|
||||
@@ -286,8 +292,8 @@
|
||||
<Heart size={18} strokeWidth={2} fill={isFav ? 'currentColor' : 'none'} />
|
||||
<span>Favorit</span>
|
||||
</button>
|
||||
<button class="btn" class:wish={onWishlist} onclick={toggleWishlist}>
|
||||
{#if onWishlist}
|
||||
<button class="btn" class:wish={onMyWishlist} onclick={toggleWishlist}>
|
||||
{#if onMyWishlist}
|
||||
<Check size={18} strokeWidth={2.5} />
|
||||
<span>Auf Wunschliste</span>
|
||||
{:else}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { Heart, Trash2, CookingPot } from 'lucide-svelte';
|
||||
import { Heart, CookingPot } from 'lucide-svelte';
|
||||
import { profileStore } from '$lib/client/profile.svelte';
|
||||
import { confirmAction, alertAction } from '$lib/client/confirm.svelte';
|
||||
import { wishlistStore } from '$lib/client/wishlist.svelte';
|
||||
import { alertAction } from '$lib/client/confirm.svelte';
|
||||
import type { WishlistEntry, SortKey } from '$lib/server/wishlist/repository';
|
||||
|
||||
let entries = $state<WishlistEntry[]>([]);
|
||||
@@ -26,36 +27,34 @@
|
||||
void load();
|
||||
});
|
||||
|
||||
async function toggleLike(entry: WishlistEntry) {
|
||||
async function toggleMine(entry: WishlistEntry) {
|
||||
if (!profileStore.active) {
|
||||
await alertAction({
|
||||
title: 'Kein Profil gewählt',
|
||||
message: 'Tippe oben rechts auf „Profil wählen", um zu liken.'
|
||||
message: 'Tippe oben rechts auf „Profil wählen", um mitzuwünschen.'
|
||||
});
|
||||
return;
|
||||
}
|
||||
const method = entry.liked_by_me ? 'DELETE' : 'PUT';
|
||||
await fetch(`/api/wishlist/${entry.recipe_id}/like`, {
|
||||
method,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify({ profile_id: profileStore.active.id })
|
||||
});
|
||||
const profileId = profileStore.active.id;
|
||||
if (entry.on_my_wishlist) {
|
||||
await fetch(`/api/wishlist/${entry.recipe_id}?profile_id=${profileId}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
} else {
|
||||
await fetch('/api/wishlist', {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify({ recipe_id: entry.recipe_id, profile_id: profileId })
|
||||
});
|
||||
}
|
||||
await load();
|
||||
void wishlistStore.refresh();
|
||||
}
|
||||
|
||||
async function remove(entry: WishlistEntry) {
|
||||
const ok = await confirmAction({
|
||||
title: 'Von der Wunschliste entfernen?',
|
||||
message: `„${entry.title}" wird aus der Wunschliste entfernt. Das Rezept selbst bleibt gespeichert.`,
|
||||
confirmLabel: 'Entfernen',
|
||||
destructive: true
|
||||
});
|
||||
if (!ok) return;
|
||||
await fetch(`/api/wishlist/${entry.recipe_id}`, { method: 'DELETE' });
|
||||
await load();
|
||||
}
|
||||
|
||||
onMount(load);
|
||||
onMount(() => {
|
||||
void load();
|
||||
void wishlistStore.refresh();
|
||||
});
|
||||
|
||||
function resolveImage(p: string | null): string | null {
|
||||
if (!p) return null;
|
||||
@@ -100,11 +99,11 @@
|
||||
<div class="text">
|
||||
<div class="title">{e.title}</div>
|
||||
<div class="meta">
|
||||
{#if e.added_by_name}
|
||||
<span>von {e.added_by_name}</span>
|
||||
{#if e.wanted_by_names}
|
||||
<span class="wanted-by">{e.wanted_by_names}</span>
|
||||
{/if}
|
||||
{#if e.source_domain}
|
||||
<span>· {e.source_domain}</span>
|
||||
<span class="src">· {e.source_domain}</span>
|
||||
{/if}
|
||||
{#if e.avg_stars !== null}
|
||||
<span>· ★ {e.avg_stars.toFixed(1)}</span>
|
||||
@@ -115,18 +114,15 @@
|
||||
<div class="actions">
|
||||
<button
|
||||
class="like"
|
||||
class:active={e.liked_by_me}
|
||||
aria-label={e.liked_by_me ? 'Unlike' : 'Like'}
|
||||
onclick={() => toggleLike(e)}
|
||||
class:active={e.on_my_wishlist}
|
||||
aria-label={e.on_my_wishlist ? 'Ich will das nicht mehr' : 'Ich will das auch'}
|
||||
onclick={() => toggleMine(e)}
|
||||
>
|
||||
<Heart size={18} strokeWidth={2} fill={e.liked_by_me ? 'currentColor' : 'none'} />
|
||||
{#if e.like_count > 0}
|
||||
<span class="count">{e.like_count}</span>
|
||||
<Heart size={18} strokeWidth={2} fill={e.on_my_wishlist ? 'currentColor' : 'none'} />
|
||||
{#if e.wanted_by_count > 0}
|
||||
<span class="count">{e.wanted_by_count}</span>
|
||||
{/if}
|
||||
</button>
|
||||
<button class="del" aria-label="Entfernen" onclick={() => remove(e)}>
|
||||
<Trash2 size={18} strokeWidth={2} />
|
||||
</button>
|
||||
</div>
|
||||
</li>
|
||||
{/each}
|
||||
@@ -175,7 +171,8 @@
|
||||
padding: 3rem 1rem;
|
||||
}
|
||||
.big {
|
||||
font-size: 3rem;
|
||||
color: #8fb097;
|
||||
display: inline-flex;
|
||||
margin: 0 0 0.5rem;
|
||||
}
|
||||
.hint {
|
||||
@@ -215,7 +212,7 @@
|
||||
background: #eef3ef;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
font-size: 2rem;
|
||||
color: #8fb097;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.text {
|
||||
@@ -239,15 +236,16 @@
|
||||
font-size: 0.82rem;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.wanted-by {
|
||||
color: #2b6a3d;
|
||||
font-weight: 500;
|
||||
}
|
||||
.actions {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.4rem;
|
||||
align-items: center;
|
||||
padding: 0.5rem 0.6rem 0.5rem 0;
|
||||
justify-content: center;
|
||||
}
|
||||
.like,
|
||||
.del {
|
||||
.like {
|
||||
min-width: 48px;
|
||||
min-height: 44px;
|
||||
border-radius: 10px;
|
||||
@@ -259,6 +257,7 @@
|
||||
justify-content: center;
|
||||
gap: 0.3rem;
|
||||
font-size: 1.05rem;
|
||||
color: #444;
|
||||
}
|
||||
.like.active {
|
||||
color: #c53030;
|
||||
|
||||
Reference in New Issue
Block a user