groups api route
This commit is contained in:
46
src/routes/api/groups/+server.js
Normal file
46
src/routes/api/groups/+server.js
Normal 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]
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user