mirror of
https://github.com/SkalaraAI/skalara-web.git
synced 2025-04-07 05:50:18 -04:00
83 lines
2.1 KiB
TypeScript
83 lines
2.1 KiB
TypeScript
import { createRouteHandlerClient } from "@supabase/auth-helpers-nextjs";
|
|
import { Database } from "@/types/supabase";
|
|
import prisma from "@/lib/prisma";
|
|
import { NextResponse, NextRequest } from "next/server";
|
|
import { revalidatePath } from "next/cache";
|
|
import { cookies } from "next/headers";
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const supabase = createRouteHandlerClient<Database>({ cookies });
|
|
|
|
const {
|
|
data: { session },
|
|
} = await supabase.auth.getSession();
|
|
|
|
if (!session) {
|
|
throw new Error("Unauthorized");
|
|
}
|
|
|
|
const userProjects = await prisma.userProfile.findUnique({
|
|
where: { id: session.user.id },
|
|
include: { UserProfile_Project: { include: { Project: true } } },
|
|
});
|
|
|
|
const res = userProjects?.UserProfile_Project.map((relation) => ({
|
|
...relation.Project,
|
|
id: String(relation.Project.id),
|
|
}));
|
|
|
|
const path = req.nextUrl.searchParams.get("path") || "/";
|
|
revalidatePath(path);
|
|
|
|
return NextResponse.json(res, { status: 200 });
|
|
} catch (err: any) {
|
|
return NextResponse.json({ message: err.message }, { status: 401 });
|
|
} finally {
|
|
await prisma.$disconnect();
|
|
}
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const supabase = createRouteHandlerClient<Database>({ cookies });
|
|
|
|
const {
|
|
data: { session },
|
|
} = await supabase.auth.getSession();
|
|
|
|
if (!session) {
|
|
throw new Error("Unauthorized");
|
|
}
|
|
|
|
const { title, description, github, stack } = await req.json();
|
|
|
|
const newProject = await prisma.project.create({
|
|
data: {
|
|
title,
|
|
description,
|
|
github,
|
|
stack,
|
|
},
|
|
});
|
|
|
|
await prisma.userProfile_Project.create({
|
|
data: {
|
|
userProfileId: session.user.id,
|
|
projectId: newProject.id,
|
|
},
|
|
});
|
|
|
|
const res = {
|
|
...newProject,
|
|
id: String(newProject.id),
|
|
};
|
|
|
|
return NextResponse.json(res, { status: 201 });
|
|
} catch (err: any) {
|
|
return NextResponse.json({ message: err.message }, { status: 500 });
|
|
} finally {
|
|
await prisma.$disconnect();
|
|
}
|
|
}
|