Skip to content

Commit

Permalink
feat: ✨ initial groups setup
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinWu098 committed Feb 8, 2024
1 parent 9403668 commit 106a9ff
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/routes/groups/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { fail, redirect } from "@sveltejs/kit";

import type { Actions, PageServerLoad } from "./$types";

import { prisma } from "$lib/server/prisma";

export const load: PageServerLoad = async (event) => {
const user = event.locals.user;

return {
groups: await prisma.groups.findMany({
where: {
members: {
some: {
userId: user.userId,
},
},
},
}),
};
};

export const actions: Actions = {
createGroup: createGroup,
};

async function createGroup({ request, locals }: { request: Request; locals: App.Locals }) {
const session = await locals.auth.validate();
if (!session) {
throw redirect(302, "/");
}

const { name, description } = Object.fromEntries(await request.formData()) as Record<
string,
string
>;

try {
await prisma.groups.create({
data: {
name,
description,
members: {
create: {
userId: session.user.userId,
},
},
},
});
} catch (err) {
console.error(err);
return fail(500, { message: "Could not create group." });
}

return {
status: 201,
};
}

0 comments on commit 106a9ff

Please sign in to comment.