Files
peworkshop/app/(main)/team/[teamId]/route.js
T
2025-03-12 01:46:45 +00:00

42 lines
1.1 KiB
JavaScript

import { redirect } from "next/navigation";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import { queryOne, query } from "@/lib/db";
export async function GET(_, context) {
const session = await getServerSession(authOptions);
if (!session) {
return redirect("/login");
}
try {
const params = await context.params;
const teamId = params.teamId;
const team = await queryOne("SELECT * FROM teams WHERE id = ?", [teamId]);
if (!team) {
return new Response(
JSON.stringify({ success: false, error: "Team not found" }),
{ status: 404 }
);
}
const members = await query(
"SELECT id, username FROM users WHERE team_id = ?",
[teamId]
);
return new Response(JSON.stringify({ success: true, team, members }), {
status: 200,
});
} catch (error) {
console.error("Team fetch error:", error);
return new Response(
JSON.stringify({ success: false, error: "Failed to fetch team" }),
{ status: 500 }
);
}
}