mirror of https://github.com/TZGyn/shortener
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
import { db } from '$lib/db'
|
|
import { message, superValidate } from 'sveltekit-superforms'
|
|
import type { PageServerLoad } from './$types'
|
|
import { zod } from 'sveltekit-superforms/adapters'
|
|
import { formSchema } from './schema'
|
|
import { fail, type Actions } from '@sveltejs/kit'
|
|
import { project } from '$lib/db/schema'
|
|
import { generateId } from 'lucia'
|
|
|
|
export const load = (async (event) => {
|
|
const user = event.locals.user
|
|
|
|
const projects = await db.query.project.findMany({
|
|
with: {
|
|
shortener: true,
|
|
},
|
|
where: (project, { eq }) => eq(project.userId, user.id),
|
|
})
|
|
|
|
return { projects, form: await superValidate(zod(formSchema)) }
|
|
}) satisfies PageServerLoad
|
|
|
|
export const actions: Actions = {
|
|
default: async (event) => {
|
|
const form = await superValidate(event, zod(formSchema))
|
|
if (!form.valid) {
|
|
return fail(400, {
|
|
form,
|
|
})
|
|
}
|
|
|
|
const user = event.locals.user
|
|
|
|
await db.insert(project).values({
|
|
id: generateId(8),
|
|
name: form.data.name,
|
|
userId: user.id,
|
|
})
|
|
|
|
return message(form, 'Project created')
|
|
},
|
|
}
|