-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9403668
commit 106a9ff
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |