Files
peworkshop/app/api/teams/[teamId]/route.js
T

48 lines
1.2 KiB
JavaScript

import { query, queryOne } from "@/lib/db";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../../auth/[...nextauth]/route";
export async function GET(_, context) {
// Changed 'request' to '_' to indicate unused parameter
const session = await getServerSession(authOptions);
if (!session) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}
try {
const teamId = context.params.teamId;
// Validate teamId
if (!teamId || isNaN(parseInt(teamId, 10))) {
return Response.json({ error: "Invalid team ID" }, { status: 400 });
}
const team = await queryOne("SELECT * FROM teams WHERE id = ?", [teamId]);
if (!team) {
return Response.json({ error: "Team not found" }, { status: 404 });
}
const members = await query(
"SELECT id, username FROM users WHERE team_id = ?",
[teamId]
);
return Response.json(
{
success: true,
team,
members,
},
{ status: 200 }
);
} catch (error) {
console.error("Team fetch error:", error);
return Response.json(
{ error: "Failed to fetch team data" },
{ status: 500 }
);
}
}