127 lines
3.6 KiB
TypeScript
127 lines
3.6 KiB
TypeScript
import { auth } from "@/auth";
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
import { getLevelTracks, createLevelTrack, updateLevelTrack, deleteLevelTrack } from "@/lib/discord";
|
|
|
|
// GET - Fetch level tracks for a guild
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ guildId: string }> }
|
|
) {
|
|
const session = await auth();
|
|
const { guildId } = await params;
|
|
|
|
if (!session?.accessToken) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
const tracks = await getLevelTracks(guildId, session.accessToken as string);
|
|
return NextResponse.json(tracks);
|
|
} catch (error) {
|
|
console.error("Error fetching tracks:", error);
|
|
return NextResponse.json(
|
|
{ error: "Internal server error" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// POST - Create a new track
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ guildId: string }> }
|
|
) {
|
|
const session = await auth();
|
|
const { guildId } = await params;
|
|
|
|
if (!session?.accessToken) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
const body = await request.json();
|
|
const result = await createLevelTrack(guildId, body, session.accessToken as string);
|
|
|
|
if (!result.success) {
|
|
return NextResponse.json(
|
|
{ error: result.error || "Failed to create track" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error("Error creating track:", error);
|
|
return NextResponse.json(
|
|
{ error: "Internal server error" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// PATCH - Update an existing track
|
|
export async function PATCH(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ guildId: string }> }
|
|
) {
|
|
const session = await auth();
|
|
const { guildId } = await params;
|
|
|
|
if (!session?.accessToken) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
const body = await request.json();
|
|
const result = await updateLevelTrack(guildId, body, session.accessToken as string);
|
|
|
|
if (!result.success) {
|
|
return NextResponse.json(
|
|
{ error: result.error || "Failed to update track" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error("Error updating track:", error);
|
|
return NextResponse.json(
|
|
{ error: "Internal server error" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// DELETE - Delete a track
|
|
export async function DELETE(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ guildId: string }> }
|
|
) {
|
|
const session = await auth();
|
|
const { guildId } = await params;
|
|
|
|
if (!session?.accessToken) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
const body = await request.json();
|
|
const result = await deleteLevelTrack(guildId, body.track_name, session.accessToken as string);
|
|
|
|
if (!result.success) {
|
|
return NextResponse.json(
|
|
{ error: result.error || "Failed to delete track" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error("Error deleting track:", error);
|
|
return NextResponse.json(
|
|
{ error: "Internal server error" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|