import { eq } from 'drizzle-orm' import { useDB, schema } from '../../../utils/db.js' import { requireAdmin } from '../../../utils/auth.js' export default defineEventHandler(async (event) => { requireAdmin(event) const id = Number(getRouterParam(event, 'id')) const body = await readBody(event) const db = useDB() const updateData = {} if (body.title !== undefined) updateData.title = body.title if (body.slug !== undefined) updateData.slug = body.slug if (body.subtitle !== undefined) updateData.subtitle = body.subtitle if (body.familyType !== undefined) updateData.familyType = body.familyType if (body.childrenAge !== undefined) updateData.childrenAge = body.childrenAge if (body.fromCity !== undefined) updateData.fromCity = body.fromCity if (body.tripProduct !== undefined) updateData.tripProduct = body.tripProduct if (body.travelDate !== undefined) updateData.travelDate = body.travelDate if (body.coverImage !== undefined) updateData.coverImage = body.coverImage if (body.summary !== undefined) updateData.summary = body.summary if (body.author !== undefined) updateData.author = body.author if (body.sourceUrl !== undefined) updateData.sourceUrl = body.sourceUrl if (body.tags !== undefined) updateData.tags = Array.isArray(body.tags) ? JSON.stringify(body.tags) : body.tags if (body.sections !== undefined) updateData.sections = JSON.stringify(body.sections) if (body.keyMoments !== undefined) updateData.keyMoments = JSON.stringify(body.keyMoments) if (body.seoTitle !== undefined) updateData.seoTitle = body.seoTitle if (body.seoDesc !== undefined) updateData.seoDesc = body.seoDesc if (body.published !== undefined) updateData.published = body.published ? 1 : 0 if (body.sortOrder !== undefined) updateData.sortOrder = body.sortOrder db.update(schema.stories).set(updateData).where(eq(schema.stories.id, id)).run() return { success: true } })