Files
splitt-the-bill/server/api/pots.post.ts
T
anbraten cbe1b42f6e
tinyedge/deploy Deployed by TinyEdge
🐛 add env to useDb and apply prettier
2026-08-07 13:28:26 +02:00

33 lines
1002 B
TypeScript

import { generateSlug } from '~~/shared/utils/slug';
import { useDb } from '~~/server/database/client';
import { participants, pots } from '~~/server/database/schema';
export default defineEventHandler(async (event) => {
const body = await readBody(event);
const name = typeof body?.name === 'string' ? body.name.trim() : '';
const currency =
typeof body?.currency === 'string'
? body.currency.trim().toUpperCase()
: '';
const creatorName =
typeof body?.creatorName === 'string' ? body.creatorName.trim() : '';
if (!name || !currency || !creatorName) {
throw createError({
statusCode: 400,
statusMessage: 'name, currency and creatorName are required',
});
}
const db = useDb(event);
const slug = generateSlug();
await db.insert(pots).values({ slug, name, currency });
const [creator] = await db
.insert(participants)
.values({ potSlug: slug, name: creatorName })
.returning();
return { slug, name, currency, creator };
});