This commit is contained in:
39
src/routes/projects/+page.server.ts
Normal file
39
src/routes/projects/+page.server.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import type { Actions, PageServerLoad } from './$types';
|
||||
import { db } from '$lib/server/db';
|
||||
import { project } from '$lib/server/db/schema';
|
||||
import { desc, eq } from 'drizzle-orm';
|
||||
import { publish } from '$lib/server/events';
|
||||
|
||||
export const load: PageServerLoad = async (event) => {
|
||||
if (!event.locals.user) throw redirect(302, '/auth/login');
|
||||
|
||||
const projects = await db
|
||||
.select()
|
||||
.from(project)
|
||||
.where(eq(project.ownerId, event.locals.user.id))
|
||||
.orderBy(desc(project.updatedAt));
|
||||
|
||||
return { projects };
|
||||
};
|
||||
|
||||
export const actions: Actions = {
|
||||
create: async (event) => {
|
||||
if (!event.locals.user) throw redirect(302, '/auth/login');
|
||||
const formData = await event.request.formData();
|
||||
const name = formData.get('name')?.toString().trim() ?? '';
|
||||
const description = formData.get('description')?.toString().trim() ?? '';
|
||||
|
||||
if (!name) return { error: 'Project name is required' };
|
||||
|
||||
await db.insert(project).values({
|
||||
ownerId: event.locals.user.id,
|
||||
name,
|
||||
description
|
||||
});
|
||||
|
||||
publish('project-created');
|
||||
|
||||
throw redirect(303, '/projects');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user