feat: adding teams in PG

This commit is contained in:
Julien Froidefond
2025-08-21 09:44:29 +02:00
parent 4e82a6d860
commit 345ff5baa0
8 changed files with 478 additions and 14 deletions

View File

@@ -1,20 +1,11 @@
import { NextResponse } from "next/server";
import fs from "fs";
import path from "path";
import { TeamsService } from "@/services";
import { Team } from "@/lib/types";
export async function GET() {
try {
const filePath = path.join(process.cwd(), "data", "teams.json");
if (!fs.existsSync(filePath)) {
return NextResponse.json({ teams: [] });
}
const fileContent = fs.readFileSync(filePath, "utf-8");
const data = JSON.parse(fileContent);
return NextResponse.json(data.teams as Team[]);
const teams = await TeamsService.getTeams();
return NextResponse.json(teams);
} catch (error) {
console.error("Error loading teams:", error);
return NextResponse.json(
@@ -23,3 +14,27 @@ export async function GET() {
);
}
}
export async function POST(request: Request) {
try {
const teamData: Omit<Team, "created_at" | "updated_at"> =
await request.json();
// Validate required fields
if (!teamData.id || !teamData.name || !teamData.direction) {
return NextResponse.json(
{ error: "Missing required fields: id, name, direction" },
{ status: 400 }
);
}
const team = await TeamsService.createTeam(teamData);
return NextResponse.json(team, { status: 201 });
} catch (error) {
console.error("Error creating team:", error);
return NextResponse.json(
{ error: "Failed to create team" },
{ status: 500 }
);
}
}