diff --git a/frontend/src/routes/(app)/shopping/+page.server.ts b/frontend/src/routes/(app)/shopping/+page.server.ts new file mode 100644 index 0000000..99c1efc --- /dev/null +++ b/frontend/src/routes/(app)/shopping/+page.server.ts @@ -0,0 +1,86 @@ +import type { PageServerLoad, Actions } from './$types'; +import { apiClient } from '$lib/server/api'; +import { getWeekStart } from '$lib/planner/week'; + +export const load: PageServerLoad = async ({ fetch, url }) => { + const weekParam = url.searchParams.get('week'); + const weekStart = weekParam ?? getWeekStart(new Date()); + + const api = apiClient(fetch); + + const [shoppingResult, weekPlanResult] = await Promise.all([ + api.GET('/v1/shopping-list', { + params: { query: { weekStart } } + }), + api.GET('/v1/week-plans', { + params: { query: { weekStart } } + }) + ]); + + return { + shoppingList: shoppingResult.data ?? null, + weekPlan: weekPlanResult.data ?? null, + weekStart + }; +}; + +export const actions: Actions = { + check: async ({ fetch, request }) => { + const formData = await request.formData(); + const listId = formData.get('listId') as string; + const itemId = formData.get('itemId') as string; + const isChecked = formData.get('isChecked') === 'true'; + + const api = apiClient(fetch); + const { data, error } = await api.PATCH('/v1/shopping-lists/{listId}/items/{itemId}', { + params: { path: { listId, itemId } }, + body: { isChecked } + }); + + if (error || !data) { + return { success: false, error: 'Status konnte nicht geändert werden.' }; + } + + return { success: true, item: data }; + }, + + addItem: async ({ fetch, request }) => { + const formData = await request.formData(); + const listId = formData.get('listId') as string; + const customName = formData.get('customName') as string; + const quantity = parseFloat(formData.get('quantity') as string) || 1; + const unit = (formData.get('unit') as string) ?? ''; + + const api = apiClient(fetch); + const { data, error } = await api.POST('/v1/shopping-lists/{id}/items', { + params: { path: { id: listId } }, + body: { customName, quantity, unit } + }); + + if (error || !data) { + return { success: false, error: 'Artikel konnte nicht hinzugefügt werden.' }; + } + + return { success: true, item: data }; + }, + + generate: async ({ fetch, request, locals }) => { + if (locals.benutzer?.rolle !== 'planer') { + return { success: false, error: 'Keine Berechtigung.' }; + } + + const formData = await request.formData(); + const weekPlanId = formData.get('weekPlanId') as string; + + const api = apiClient(fetch); + const { data, error } = await api.POST('/v1/week-plans/{id}/shopping-list', { + params: { path: { id: weekPlanId } } + }); + + if (error || !data) { + return { success: false, error: 'Einkaufsliste konnte nicht erstellt werden.' }; + } + + return { success: true }; + } +};