groups api route

This commit is contained in:
2024-03-12 23:07:07 +01:00
parent 3b1e627356
commit d62cc6f6a4
21 changed files with 488 additions and 15 deletions

View File

@@ -0,0 +1,46 @@
import { db, groupsTable } from "$lib/db"
import { generateId } from "$lib/utils/auth.utils";
import { json } from "@sveltejs/kit";
import { eq } from "drizzle-orm";
/**
* @type {import("@sveltejs/kit").RequestHandler}
*/
export async function GET(event) {
console.log(event.locals.user.id)
const groups = await db.select().from(groupsTable).where(eq(groupsTable.userId, event.locals.user.id));
return new Response(JSON.stringify({
success: true,
groups
}));
}
/**
* @type {import("@sveltejs/kit").RequestHandler}
*/
export async function POST(event) {
const body = await event.request.json();
if (!body.name) return json({
success: false
});
const values = {
name: body.name,
email: `${generateId(7)}@smtp.omersabic.com`,
userId: event.locals.user.id
};
const groups = await db.insert(groupsTable).values(values).returning();
console.log(groups);
if (groups.length === 0) return json({
succcess: false
});
return json({
success: true,
group: groups[0]
});
}