23 lines
920 B
TypeScript
23 lines
920 B
TypeScript
import { generateSlug } from '../../shared/utils/slug'
|
|
import { useDb } from '../database/client'
|
|
import { participants, pots } from '../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()
|
|
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 }
|
|
})
|