33 lines
1002 B
TypeScript
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 };
|
|
});
|