40 lines
946 B
TypeScript
40 lines
946 B
TypeScript
import { json } from '@sveltejs/kit';
|
|
import type { RequestHandler } from './$types';
|
|
import { db } from '$lib/server/db';
|
|
import { wishlists } from '$lib/server/schema';
|
|
import { createId } from '@paralleldrive/cuid2';
|
|
|
|
export const POST: RequestHandler = async ({ request, locals }) => {
|
|
const { title, description, color } = await request.json();
|
|
|
|
if (!title?.trim()) {
|
|
return json({ error: 'Title is required' }, { status: 400 });
|
|
}
|
|
|
|
const session = await locals.auth();
|
|
const userId = session?.user?.id || null;
|
|
|
|
const ownerToken = createId();
|
|
const publicToken = createId();
|
|
|
|
const [wishlist] = await db
|
|
.insert(wishlists)
|
|
.values({
|
|
title: title.trim(),
|
|
description: description?.trim() || null,
|
|
color: color?.trim() || null,
|
|
ownerToken,
|
|
publicToken,
|
|
userId
|
|
})
|
|
.returning();
|
|
|
|
return json({
|
|
ownerToken,
|
|
publicToken,
|
|
id: wishlist.id,
|
|
title: wishlist.title,
|
|
createdAt: wishlist.createdAt
|
|
});
|
|
};
|