refactor: move scaler out of $lib/server so it can run in browser
RecipeView needs scaleIngredients on the client for live portion scaling. Moved scaler.ts from $lib/server/recipes/ to $lib/recipes/. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { scaleIngredients } from '$lib/server/recipes/scaler';
|
import { scaleIngredients } from '$lib/recipes/scaler';
|
||||||
import type { Recipe } from '$lib/types';
|
import type { Recipe } from '$lib/types';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
|
|||||||
23
src/routes/recipes/[id]/print/+page.server.ts
Normal file
23
src/routes/recipes/[id]/print/+page.server.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import type { PageServerLoad } from './$types';
|
||||||
|
import { error } from '@sveltejs/kit';
|
||||||
|
import { getDb } from '$lib/server/db';
|
||||||
|
import { getRecipeById } from '$lib/server/recipes/repository';
|
||||||
|
import { scaleIngredients } from '$lib/recipes/scaler';
|
||||||
|
|
||||||
|
export const load: PageServerLoad = async ({ params, url }) => {
|
||||||
|
const id = Number(params.id);
|
||||||
|
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
|
||||||
|
const recipe = getRecipeById(getDb(), id);
|
||||||
|
if (!recipe) error(404, { message: 'Nicht gefunden' });
|
||||||
|
|
||||||
|
const servingsParam = Number(url.searchParams.get('servings') ?? '');
|
||||||
|
const servings =
|
||||||
|
Number.isInteger(servingsParam) && servingsParam > 0
|
||||||
|
? servingsParam
|
||||||
|
: (recipe.servings_default ?? 4);
|
||||||
|
const defaultServings = recipe.servings_default ?? 4;
|
||||||
|
const factor = servings / defaultServings;
|
||||||
|
const scaled = scaleIngredients(recipe.ingredients, factor);
|
||||||
|
|
||||||
|
return { recipe, ingredients: scaled, servings };
|
||||||
|
};
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect } from 'vitest';
|
||||||
import { scaleIngredients, roundQuantity } from '../../src/lib/server/recipes/scaler';
|
import { scaleIngredients, roundQuantity } from '../../src/lib/recipes/scaler';
|
||||||
import type { Ingredient } from '../../src/lib/types';
|
import type { Ingredient } from '../../src/lib/types';
|
||||||
|
|
||||||
const mk = (q: number | null, unit: string | null, name: string): Ingredient => ({
|
const mk = (q: number | null, unit: string | null, name: string): Ingredient => ({
|
||||||
|
|||||||
Reference in New Issue
Block a user