Files
peworkshop/app/api/teams/[teamId]/route.js
T
2025-03-11 23:09:05 +00:00

53 lines
1.3 KiB
JavaScript

// app/api/teams/[teamId]/route.js
import { queryOne, query } from "../../../../lib/db";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../../auth/[...nextauth]/route";
export async function GET(request, { params }) {
const session = await getServerSession(authOptions);
if (!session) {
return new Response(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
});
}
try {
const teamId = parseInt(params.teamId, 10);
if (isNaN(teamId)) {
return new Response(JSON.stringify({ error: "Invalid team ID" }), {
status: 400,
});
}
const team = await queryOne(
"SELECT id, name, code FROM teams WHERE id = ?",
[teamId]
);
if (!team) {
return new Response(JSON.stringify({ error: "Team not found" }), {
status: 404,
});
}
const members = await query(
"SELECT id, username FROM users WHERE team_id = ?",
[teamId]
);
return new Response(JSON.stringify({ team, members }), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
} catch (error) {
console.error("Team fetch error:", error);
return new Response(
JSON.stringify({ error: "Failed to fetch team details" }),
{ status: 500 }
);
}
}